diff --git a/js/modules/signal.js b/js/modules/signal.js index 24513205e270..164af85aef55 100644 --- a/js/modules/signal.js +++ b/js/modules/signal.js @@ -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, }; diff --git a/js/modules/idle_detector.js b/ts/IdleDetector.ts similarity index 50% rename from js/modules/idle_detector.js rename to ts/IdleDetector.ts index a012271cbd35..2a5d74344a7f 100644 --- a/js/modules/idle_detector.js +++ b/ts/IdleDetector.ts @@ -1,54 +1,50 @@ -// Copyright 2018-2020 Signal Messenger, LLC +// Copyright 2018-2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only -/* eslint-env browser */ - -const EventEmitter = require('events'); +import EventEmitter from 'events'; +import * as log from './logging/log'; const POLL_INTERVAL_MS = 5 * 1000; const IDLE_THRESHOLD_MS = 20; -class IdleDetector extends EventEmitter { - constructor() { - super(); - this.handle = null; - this.timeoutId = null; +export class IdleDetector extends EventEmitter { + private handle: undefined | ReturnType; + private timeoutId: undefined | ReturnType; + + public start(): void { + log.info('Start idle detector'); + this.scheduleNextCallback(); } - start() { - window.SignalContext.log.info('Start idle detector'); - this._scheduleNextCallback(); - } - - stop() { + public stop(): void { if (!this.handle) { return; } - window.SignalContext.log.info('Stop idle detector'); - this._clearScheduledCallbacks(); + log.info('Stop idle detector'); + this.clearScheduledCallbacks(); } - _clearScheduledCallbacks() { + private clearScheduledCallbacks() { if (this.handle) { cancelIdleCallback(this.handle); - this.handle = null; + delete this.handle; } if (this.timeoutId) { clearTimeout(this.timeoutId); - this.timeoutId = null; + delete this.timeoutId; } } - _scheduleNextCallback() { - this._clearScheduledCallbacks(); + private 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(), + () => this.scheduleNextCallback(), POLL_INTERVAL_MS ); if (isIdle || didTimeout) { @@ -57,7 +53,3 @@ class IdleDetector extends EventEmitter { }); } } - -module.exports = { - IdleDetector, -}; diff --git a/ts/background.ts b/ts/background.ts index 5ff1af8653d3..073b921deced 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -37,6 +37,7 @@ import { dropNull } from './util/dropNull'; import { normalizeUuid } from './util/normalizeUuid'; import { filter } from './util/iterables'; import { isNotNil } from './util/isNotNil'; +import { IdleDetector } from './IdleDetector'; import { senderCertificateService } from './services/senderCertificate'; import { GROUP_CREDENTIALS_KEY } from './services/groupCredentialFetcher'; import * as KeyboardLayout from './services/keyboardLayout'; @@ -160,6 +161,8 @@ export async function cleanupSessionResets(): Promise { } export async function startApp(): Promise { + const idleDetector = new IdleDetector(); + await KeyboardLayout.initialize(); window.Whisper.events = window._.clone(window.Backbone.Events); @@ -436,7 +439,7 @@ export async function startApp(): Promise { // of preload.js processing window.setImmediate = window.nodeSetImmediate; - const { IdleDetector, MessageDataMigrator } = window.Signal.Workflow; + const { MessageDataMigrator } = window.Signal.Workflow; const { removeDatabase: removeIndexedDB, doesDatabaseExist } = window.Signal.IndexedDB; const { Message } = window.Signal.Types; @@ -451,7 +454,6 @@ export async function startApp(): Promise { log.info('background page reloaded'); log.info('environment:', window.getEnvironment()); - let idleDetector: WhatIsThis; let newVersion = false; window.document.title = window.getTitle(); @@ -585,9 +587,7 @@ export async function startApp(): Promise { // Stop background processing AttachmentDownloads.stop(); - if (idleDetector) { - idleDetector.stop(); - } + idleDetector.stop(); // Stop processing incoming messages if (messageReceiver) { @@ -747,7 +747,6 @@ export async function startApp(): Promise { Views.Initialization.setMessage(window.i18n('loading')); - idleDetector = new IdleDetector(); let isMigrationWithIndexComplete = false; log.info( `Starting background data migration. Target version: ${Message.CURRENT_SCHEMA_VERSION}` diff --git a/ts/window.d.ts b/ts/window.d.ts index 7d75d6205481..78ff3321fc14 100644 --- a/ts/window.d.ts +++ b/ts/window.d.ts @@ -401,7 +401,6 @@ declare global { }; OS: typeof OS; Workflow: { - IdleDetector: WhatIsThis; MessageDataMigrator: WhatIsThis; }; IndexedDB: {