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');
|
const Initialization = require('./views/initialization');
|
||||||
|
|
||||||
// Workflow
|
// Workflow
|
||||||
const { IdleDetector } = require('./idle_detector');
|
|
||||||
const MessageDataMigrator = require('./messages_data_migrator');
|
const MessageDataMigrator = require('./messages_data_migrator');
|
||||||
|
|
||||||
// Processes / Services
|
// Processes / Services
|
||||||
|
@ -413,7 +412,6 @@ exports.setup = (options = {}) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const Workflow = {
|
const Workflow = {
|
||||||
IdleDetector,
|
|
||||||
MessageDataMigrator,
|
MessageDataMigrator,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,54 +1,50 @@
|
||||||
// Copyright 2018-2020 Signal Messenger, LLC
|
// Copyright 2018-2021 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
/* eslint-env browser */
|
import EventEmitter from 'events';
|
||||||
|
import * as log from './logging/log';
|
||||||
const EventEmitter = require('events');
|
|
||||||
|
|
||||||
const POLL_INTERVAL_MS = 5 * 1000;
|
const POLL_INTERVAL_MS = 5 * 1000;
|
||||||
const IDLE_THRESHOLD_MS = 20;
|
const IDLE_THRESHOLD_MS = 20;
|
||||||
|
|
||||||
class IdleDetector extends EventEmitter {
|
export class IdleDetector extends EventEmitter {
|
||||||
constructor() {
|
private handle: undefined | ReturnType<typeof requestIdleCallback>;
|
||||||
super();
|
private timeoutId: undefined | ReturnType<typeof setTimeout>;
|
||||||
this.handle = null;
|
|
||||||
this.timeoutId = null;
|
public start(): void {
|
||||||
|
log.info('Start idle detector');
|
||||||
|
this.scheduleNextCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
public stop(): void {
|
||||||
window.SignalContext.log.info('Start idle detector');
|
|
||||||
this._scheduleNextCallback();
|
|
||||||
}
|
|
||||||
|
|
||||||
stop() {
|
|
||||||
if (!this.handle) {
|
if (!this.handle) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
window.SignalContext.log.info('Stop idle detector');
|
log.info('Stop idle detector');
|
||||||
this._clearScheduledCallbacks();
|
this.clearScheduledCallbacks();
|
||||||
}
|
}
|
||||||
|
|
||||||
_clearScheduledCallbacks() {
|
private clearScheduledCallbacks() {
|
||||||
if (this.handle) {
|
if (this.handle) {
|
||||||
cancelIdleCallback(this.handle);
|
cancelIdleCallback(this.handle);
|
||||||
this.handle = null;
|
delete this.handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.timeoutId) {
|
if (this.timeoutId) {
|
||||||
clearTimeout(this.timeoutId);
|
clearTimeout(this.timeoutId);
|
||||||
this.timeoutId = null;
|
delete this.timeoutId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_scheduleNextCallback() {
|
private scheduleNextCallback() {
|
||||||
this._clearScheduledCallbacks();
|
this.clearScheduledCallbacks();
|
||||||
this.handle = window.requestIdleCallback(deadline => {
|
this.handle = window.requestIdleCallback(deadline => {
|
||||||
const { didTimeout } = deadline;
|
const { didTimeout } = deadline;
|
||||||
const timeRemaining = deadline.timeRemaining();
|
const timeRemaining = deadline.timeRemaining();
|
||||||
const isIdle = timeRemaining >= IDLE_THRESHOLD_MS;
|
const isIdle = timeRemaining >= IDLE_THRESHOLD_MS;
|
||||||
this.timeoutId = setTimeout(
|
this.timeoutId = setTimeout(
|
||||||
() => this._scheduleNextCallback(),
|
() => this.scheduleNextCallback(),
|
||||||
POLL_INTERVAL_MS
|
POLL_INTERVAL_MS
|
||||||
);
|
);
|
||||||
if (isIdle || didTimeout) {
|
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 { normalizeUuid } from './util/normalizeUuid';
|
||||||
import { filter } from './util/iterables';
|
import { filter } from './util/iterables';
|
||||||
import { isNotNil } from './util/isNotNil';
|
import { isNotNil } from './util/isNotNil';
|
||||||
|
import { IdleDetector } from './IdleDetector';
|
||||||
import { senderCertificateService } from './services/senderCertificate';
|
import { senderCertificateService } from './services/senderCertificate';
|
||||||
import { GROUP_CREDENTIALS_KEY } from './services/groupCredentialFetcher';
|
import { GROUP_CREDENTIALS_KEY } from './services/groupCredentialFetcher';
|
||||||
import * as KeyboardLayout from './services/keyboardLayout';
|
import * as KeyboardLayout from './services/keyboardLayout';
|
||||||
|
@ -160,6 +161,8 @@ export async function cleanupSessionResets(): Promise<void> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function startApp(): Promise<void> {
|
export async function startApp(): Promise<void> {
|
||||||
|
const idleDetector = new IdleDetector();
|
||||||
|
|
||||||
await KeyboardLayout.initialize();
|
await KeyboardLayout.initialize();
|
||||||
|
|
||||||
window.Whisper.events = window._.clone(window.Backbone.Events);
|
window.Whisper.events = window._.clone(window.Backbone.Events);
|
||||||
|
@ -436,7 +439,7 @@ export async function startApp(): Promise<void> {
|
||||||
// of preload.js processing
|
// of preload.js processing
|
||||||
window.setImmediate = window.nodeSetImmediate;
|
window.setImmediate = window.nodeSetImmediate;
|
||||||
|
|
||||||
const { IdleDetector, MessageDataMigrator } = window.Signal.Workflow;
|
const { MessageDataMigrator } = window.Signal.Workflow;
|
||||||
const { removeDatabase: removeIndexedDB, doesDatabaseExist } =
|
const { removeDatabase: removeIndexedDB, doesDatabaseExist } =
|
||||||
window.Signal.IndexedDB;
|
window.Signal.IndexedDB;
|
||||||
const { Message } = window.Signal.Types;
|
const { Message } = window.Signal.Types;
|
||||||
|
@ -451,7 +454,6 @@ export async function startApp(): Promise<void> {
|
||||||
log.info('background page reloaded');
|
log.info('background page reloaded');
|
||||||
log.info('environment:', window.getEnvironment());
|
log.info('environment:', window.getEnvironment());
|
||||||
|
|
||||||
let idleDetector: WhatIsThis;
|
|
||||||
let newVersion = false;
|
let newVersion = false;
|
||||||
|
|
||||||
window.document.title = window.getTitle();
|
window.document.title = window.getTitle();
|
||||||
|
@ -585,9 +587,7 @@ export async function startApp(): Promise<void> {
|
||||||
|
|
||||||
// Stop background processing
|
// Stop background processing
|
||||||
AttachmentDownloads.stop();
|
AttachmentDownloads.stop();
|
||||||
if (idleDetector) {
|
idleDetector.stop();
|
||||||
idleDetector.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop processing incoming messages
|
// Stop processing incoming messages
|
||||||
if (messageReceiver) {
|
if (messageReceiver) {
|
||||||
|
@ -747,7 +747,6 @@ export async function startApp(): Promise<void> {
|
||||||
|
|
||||||
Views.Initialization.setMessage(window.i18n('loading'));
|
Views.Initialization.setMessage(window.i18n('loading'));
|
||||||
|
|
||||||
idleDetector = new IdleDetector();
|
|
||||||
let isMigrationWithIndexComplete = false;
|
let isMigrationWithIndexComplete = false;
|
||||||
log.info(
|
log.info(
|
||||||
`Starting background data migration. Target version: ${Message.CURRENT_SCHEMA_VERSION}`
|
`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;
|
OS: typeof OS;
|
||||||
Workflow: {
|
Workflow: {
|
||||||
IdleDetector: WhatIsThis;
|
|
||||||
MessageDataMigrator: WhatIsThis;
|
MessageDataMigrator: WhatIsThis;
|
||||||
};
|
};
|
||||||
IndexedDB: {
|
IndexedDB: {
|
||||||
|
|
Loading…
Reference in a new issue