Convert IdleDetector
to TypeScript
This commit is contained in:
parent
ebcd3e3e43
commit
2fe5ec6ab2
4 changed files with 24 additions and 36 deletions
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
|
@ -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<typeof requestIdleCallback>;
|
||||
private timeoutId: undefined | ReturnType<typeof setTimeout>;
|
||||
|
||||
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,
|
||||
};
|
|
@ -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<void> {
|
|||
}
|
||||
|
||||
export async function startApp(): Promise<void> {
|
||||
const idleDetector = new IdleDetector();
|
||||
|
||||
await KeyboardLayout.initialize();
|
||||
|
||||
window.Whisper.events = window._.clone(window.Backbone.Events);
|
||||
|
@ -436,7 +439,7 @@ export async function startApp(): Promise<void> {
|
|||
// 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<void> {
|
|||
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<void> {
|
|||
|
||||
// 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<void> {
|
|||
|
||||
Views.Initialization.setMessage(window.i18n('loading'));
|
||||
|
||||
idleDetector = new IdleDetector();
|
||||
let isMigrationWithIndexComplete = false;
|
||||
log.info(
|
||||
`Starting background data migration. Target version: ${Message.CURRENT_SCHEMA_VERSION}`
|
||||
|
|
1
ts/window.d.ts
vendored
1
ts/window.d.ts
vendored
|
@ -401,7 +401,6 @@ declare global {
|
|||
};
|
||||
OS: typeof OS;
|
||||
Workflow: {
|
||||
IdleDetector: WhatIsThis;
|
||||
MessageDataMigrator: WhatIsThis;
|
||||
};
|
||||
IndexedDB: {
|
||||
|
|
Loading…
Reference in a new issue