Convert IdleDetector
to TypeScript
This commit is contained in:
parent
ebcd3e3e43
commit
2fe5ec6ab2
4 changed files with 24 additions and 36 deletions
|
@ -1,63 +0,0 @@
|
|||
// Copyright 2018-2020 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
/* eslint-env browser */
|
||||
|
||||
const EventEmitter = require('events');
|
||||
|
||||
const POLL_INTERVAL_MS = 5 * 1000;
|
||||
const IDLE_THRESHOLD_MS = 20;
|
||||
|
||||
class IdleDetector extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this.handle = null;
|
||||
this.timeoutId = null;
|
||||
}
|
||||
|
||||
start() {
|
||||
window.SignalContext.log.info('Start idle detector');
|
||||
this._scheduleNextCallback();
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (!this.handle) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.SignalContext.log.info('Stop idle detector');
|
||||
this._clearScheduledCallbacks();
|
||||
}
|
||||
|
||||
_clearScheduledCallbacks() {
|
||||
if (this.handle) {
|
||||
cancelIdleCallback(this.handle);
|
||||
this.handle = null;
|
||||
}
|
||||
|
||||
if (this.timeoutId) {
|
||||
clearTimeout(this.timeoutId);
|
||||
this.timeoutId = null;
|
||||
}
|
||||
}
|
||||
|
||||
_scheduleNextCallback() {
|
||||
this._clearScheduledCallbacks();
|
||||
this.handle = window.requestIdleCallback(deadline => {
|
||||
const { didTimeout } = deadline;
|
||||
const timeRemaining = deadline.timeRemaining();
|
||||
const isIdle = timeRemaining >= IDLE_THRESHOLD_MS;
|
||||
this.timeoutId = setTimeout(
|
||||
() => this._scheduleNextCallback(),
|
||||
POLL_INTERVAL_MS
|
||||
);
|
||||
if (isIdle || didTimeout) {
|
||||
this.emit('idle', { timestamp: Date.now(), didTimeout, timeRemaining });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
IdleDetector,
|
||||
};
|
|
@ -131,7 +131,6 @@ const { QualifiedAddress } = require('../../ts/types/QualifiedAddress');
|
|||
const Initialization = require('./views/initialization');
|
||||
|
||||
// Workflow
|
||||
const { IdleDetector } = require('./idle_detector');
|
||||
const MessageDataMigrator = require('./messages_data_migrator');
|
||||
|
||||
// Processes / Services
|
||||
|
@ -413,7 +412,6 @@ exports.setup = (options = {}) => {
|
|||
};
|
||||
|
||||
const Workflow = {
|
||||
IdleDetector,
|
||||
MessageDataMigrator,
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue