ui-counter.ts 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
import { Http } from './../http/http';
import { Subject } from 'rxjs';

/**
 * 计数器对象
 *
 * @export
 * @class UICounter
 */
export class UICounter {

    /**
     * 定时器时间
     * 
     * @private
     * @type {*}
     * @memberof UICounter
     */
    private timer: number;

    /**
     * 定时器
     * 
     * @private
     * @type {*}
     * @memberof UICounter
     */
    private timerTag: any = null;

    /**
     * 计数器id
     * 
     * @private
     * @type {*}
     * @memberof UICounter
     */
    private counterId: string = '';

    /**
     * 计数器交互数据
     *
     * @private
     * @type {*}
     * @memberof UICounter
     */
    private data: any = {};

    /**
     * url
     *
     * @type {string}
     * @memberof UICounter
     */
    public url: string = '';

    /**
     * 请求
     *
     * @private
     * @type {Http}
     * @memberof UICounter
     */
    private http: Http = Http.getInstance();

    /**
     * 计数器对象订阅对象
     *
     * @type {Subject<any>}
     * @memberof UICounter
     */
    public uiCounterData: Subject<any> = new Subject();

    /**
     * Creates an instance of UICounter.
     * 创建 UICounter 服务对象
     * 
     * @param {*} [config={}]
     * @memberof UICounter
     */
    constructor(config: any = {}) {
        this.counterId = config.counterId;
        this.timer = config.timer;
        this.url = config.url;
        this.load();
    }

    /**
     * 加载定时器
     * 
     * @memberof UICounter
     */
    private load(): void {
        if (this.timer > 1000) {
            this.timerTag = setInterval(() => {
                this.reload();
            }, this.timer);
        }
        this.reload();
    }

    /**
     * 刷新计数器
     * 
     * @private
     * @param {*} [arg={}] 
     * @memberof UICounter
     */
    public reload(arg: any = {}): void {
        this.http.get(this.url).then((response: any) => {
            if (!response || response.status !== 200) {
                return;
            }

            const { data: result }= response;
            this.setData(result);
        }).catch((response: any) => {
        });
    }

    /**
     * 处理数据
     * 
     * @private
     * @param {*} result 
     * @memberof UICounter
     */
    private setData(result: any): void {
        this.data = result;
        this.uiCounterData.next(this.data);
    }

    /**
     * 获取数据
     * 
     * @returns {*} 
     * @memberof UICounter
     */
    public getData(): any {
        return this.data;
    }

    /**
     * 关闭计数器
     * 
     * @memberof UICounter
     */
    public close(): void {
        if (this.timerTag !== undefined) {
            clearInterval(this.timerTag);
            delete this.timer;
        }
    }
}