All files / modules clock.ts

100% Statements 47/47
100% Branches 9/9
100% Functions 6/6
100% Lines 46/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1092x 2x                   2x 10x   10x 10x 10x 10x     10x     10x     10x     10x   10x           10x 291x         10x 10x   10x     10x       302x                       9x 5x   4x 4x 3x         10x 10x 10x     294x 294x 72x 72x 72x 72x     294x 7x 7x     294x 1x 1x     294x 294x 290x 290x 290x 290x          
import { Injectable, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';
 
/**
 * clockService is a service that provides ticking functionality at different time intervals
 * (1 second, 10 seconds, 1 minute), allowing components to subscribe to these events for
 * periodic actions. It also provides the ability to enable or disable the clock ticks temporarily.
 */
@Injectable({
    providedIn: 'root'
  })
  export class clockService implements OnDestroy {
    private _tickms = 250;
    
    private _refreshTick = new Subject<null>();
    private _onesecondTick = new Subject<null>();
    private _tensecondTick = new Subject<null>();
    private _oneminuteTick = new Subject<null>();
 
    /** Public observable for refresh tick events. */
    public OnRefreshTick = this._refreshTick.asObservable();
 
    /** Public observable for 1-second tick events. */
    public OnOneSecondTick = this._onesecondTick.asObservable();
 
    /** Public observable for 10-second tick events. */
    public OnTenSecondTick = this._tensecondTick.asObservable();
 
    /** Public observable for 1-minute tick events. */
    public OnOneMinuteTick = this._oneminuteTick.asObservable();
 
    private _clock_interval: any = null;
 
    /**
     * Initializes the clock service and starts the ticking process using setInterval.
     */
    constructor() {
        this._clock_interval = setInterval(() => {
            this._tick();
        }, this._tickms);
    }
 
    ngOnDestroy() {
        if (this._clock_interval) {
            clearInterval(this._clock_interval);
        }
        this._clock_interval = null;
    }
 
    private _enabled = new Set<string>();
 
    /** Returns true if no sources are disabling the clock. */
    get Enable() {
        return this._enabled.size === 0;
    }
 
    /**
     * Method to enable or disable the clock from a specific source.
     * If `value` is `false`, the source disables the clock.
     * If `value` is `true`, the source enables the clock, and if no sources are disabling it, the tick is triggered.
     * 
     * @param source The name of the source requesting the clock to be enabled or disabled.
     * @param value A boolean indicating whether to enable (`true`) or disable (`false`) the clock.
     */
    ClockEnable(source: string, value: boolean) {
        if (!value) {
            this._enabled.add(source); // Add source for clock disable
        } else {
            this._enabled.delete(source); // Remove source for clock enable
            if (this._enabled.size === 0) {
                this._tick();
            }
        }
    }
 
    private _second_count = 0;
    private _tensec_count = 0;
    private _minute_count = 0;
 
    private _tick() {
        this._second_count++;
        if (this._second_count >= 1000 / this._tickms) {
            this._second_count = 0;
            this._onesecondTick.next(null);
            this._tensec_count++;
            this._minute_count++;
        }
 
        if (this._tensec_count >= 10) {
            this._tensec_count = 0;
            this._tensecondTick.next(null);
        }
 
        if (this._minute_count >= 60) {
            this._minute_count = 0;
            this._oneminuteTick.next(null);
        }
 
        let _skip = false;
        if (this.Enable) {
            if (!_skip) {
                _skip = true;
                this._refreshTick.next(null);
                _skip = false;
            }
        }
    }
}