Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use non-throttled timeouts for websockets
Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com>
- Loading branch information
1 parent
b6c7c19
commit 383e9af
Showing
7 changed files
with
107 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2021 Signal Messenger, LLC | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
const { timers } = window.SignalContext; | ||
|
||
export type { Timeout } from './context/Timers'; | ||
|
||
export function setTimeout( | ||
...args: Parameters<typeof timers.setTimeout> | ||
): ReturnType<typeof timers.setTimeout> { | ||
return timers.setTimeout(...args); | ||
} | ||
|
||
export function clearTimeout( | ||
...args: Parameters<typeof timers.clearTimeout> | ||
): ReturnType<typeof timers.clearTimeout> { | ||
return timers.clearTimeout(...args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2021 Signal Messenger, LLC | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { setTimeout, clearTimeout } from 'timers'; | ||
|
||
export type Timeout = { | ||
id: number; | ||
__signalContext: never; | ||
}; | ||
|
||
export class Timers { | ||
private counter = 0; | ||
|
||
private readonly timers = new Map<number, NodeJS.Timeout>(); | ||
|
||
public setTimeout(callback: () => void, delay: number): Timeout { | ||
let id: number; | ||
do { | ||
id = this.counter; | ||
// eslint-disable-next-line no-bitwise | ||
this.counter = (this.counter + 1) >>> 0; | ||
} while (this.timers.has(id)); | ||
|
||
const timer = setTimeout(() => { | ||
this.timers.delete(id); | ||
callback(); | ||
}, delay); | ||
|
||
this.timers.set(id, timer); | ||
|
||
return ({ id } as unknown) as Timeout; | ||
} | ||
|
||
public clearTimeout({ id }: Timeout): ReturnType<typeof clearTimeout> { | ||
const timer = this.timers.get(id); | ||
if (timer === undefined) { | ||
return; | ||
} | ||
|
||
this.timers.delete(id); | ||
return clearTimeout(timer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters