All files / services InactivityDetector.js

95% Statements 38/40
77.77% Branches 7/9
100% Functions 4/4
95% Lines 38/40

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 401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 2x 2x 1x 1x 2x 2x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x
/**
 * handle app inactivity trigger
 */
export default class InactivityDetector {
    constructor(config, loggerService) {
        this.config = config;
        this.logger = loggerService.getLogger().child({label: 'InactivityDetector'});
        this.INACTIVITY_DELAY_MIN = this.config.inactivityDelayMin || 3;
    }
 
    onInactivity() {
        InactivityDetector.timer = null;
        for (const handler of InactivityDetector.onInactivityListeners) {
            try {
                handler();
            } catch (exception) {
                this.logger.warn(`onInactivity handler error : ${exception.message}`);
            }
        }
    }
 
    activityTic() {
        const {onInactivity, INACTIVITY_DELAY_MIN, logger} = this;
        if (InactivityDetector.timer !== null) {
            clearTimeout(InactivityDetector.timer);
        }
        if (InactivityDetector.onInactivityListeners.length > 0) {
            if (InactivityDetector.timer === null) { // first time only
                logger.info(`setTimeout(${INACTIVITY_DELAY_MIN} min, ...) with ${InactivityDetector.onInactivityListeners.length} listeners`)
            }
            InactivityDetector.timer = setTimeout(onInactivity.bind(this), INACTIVITY_DELAY_MIN * 60000);
        }
    }
 
    registerOnInactivityListener(listener) {
        InactivityDetector.onInactivityListeners.push(listener)
    }
}
InactivityDetector.timer = null;
InactivityDetector.onInactivityListeners = [];