You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-25 05:23:13 +03:00
Migrate realtime-callbacks to TypeScript
This commit is contained in:
@@ -31,17 +31,17 @@ import { logger } from './logger';
|
|||||||
const TIMER_CHECK_PERIOD_MS = 1000;
|
const TIMER_CHECK_PERIOD_MS = 1000;
|
||||||
|
|
||||||
// counter, for making up ids to return from setTimeout
|
// counter, for making up ids to return from setTimeout
|
||||||
let _count = 0;
|
let count = 0;
|
||||||
|
|
||||||
// the key for our callback with the real global.setTimeout
|
// the key for our callback with the real global.setTimeout
|
||||||
let _realCallbackKey;
|
let realCallbackKey;
|
||||||
|
|
||||||
// a sorted list of the callbacks to be run.
|
// a sorted list of the callbacks to be run.
|
||||||
// each is an object with keys [runAt, func, params, key].
|
// each is an object with keys [runAt, func, params, key].
|
||||||
const _callbackList = [];
|
const callbackList = [];
|
||||||
|
|
||||||
// var debuglog = logger.log.bind(logger);
|
// var debuglog = logger.log.bind(logger);
|
||||||
const debuglog = function() {};
|
const debuglog = function(...params: any[]) {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace the function used by this module to get the current time.
|
* Replace the function used by this module to get the current time.
|
||||||
@@ -52,10 +52,10 @@ const debuglog = function() {};
|
|||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export function setNow(f) {
|
export function setNow(f: () => number): void {
|
||||||
_now = f || Date.now;
|
now = f || Date.now;
|
||||||
}
|
}
|
||||||
let _now = Date.now;
|
let now = Date.now;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* reimplementation of window.setTimeout, which will call the callback if
|
* reimplementation of window.setTimeout, which will call the callback if
|
||||||
@@ -67,15 +67,14 @@ let _now = Date.now;
|
|||||||
* @return {Number} an identifier for this callback, which may be passed into
|
* @return {Number} an identifier for this callback, which may be passed into
|
||||||
* clearTimeout later.
|
* clearTimeout later.
|
||||||
*/
|
*/
|
||||||
export function setTimeout(func, delayMs) {
|
export function setTimeout(func: (...params: any[]) => void, delayMs: number, ...params: any[]): number {
|
||||||
delayMs = delayMs || 0;
|
delayMs = delayMs || 0;
|
||||||
if (delayMs < 0) {
|
if (delayMs < 0) {
|
||||||
delayMs = 0;
|
delayMs = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const params = Array.prototype.slice.call(arguments, 2);
|
const runAt = now() + delayMs;
|
||||||
const runAt = _now() + delayMs;
|
const key = count++;
|
||||||
const key = _count++;
|
|
||||||
debuglog("setTimeout: scheduling cb", key, "at", runAt,
|
debuglog("setTimeout: scheduling cb", key, "at", runAt,
|
||||||
"(delay", delayMs, ")");
|
"(delay", delayMs, ")");
|
||||||
const data = {
|
const data = {
|
||||||
@@ -87,13 +86,13 @@ export function setTimeout(func, delayMs) {
|
|||||||
|
|
||||||
// figure out where it goes in the list
|
// figure out where it goes in the list
|
||||||
const idx = binarySearch(
|
const idx = binarySearch(
|
||||||
_callbackList, function(el) {
|
callbackList, function(el) {
|
||||||
return el.runAt - runAt;
|
return el.runAt - runAt;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
_callbackList.splice(idx, 0, data);
|
callbackList.splice(idx, 0, data);
|
||||||
_scheduleRealCallback();
|
scheduleRealCallback();
|
||||||
|
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
@@ -103,68 +102,69 @@ export function setTimeout(func, delayMs) {
|
|||||||
*
|
*
|
||||||
* @param {Number} key result from an earlier setTimeout call
|
* @param {Number} key result from an earlier setTimeout call
|
||||||
*/
|
*/
|
||||||
export function clearTimeout(key) {
|
export function clearTimeout(key: number): void {
|
||||||
if (_callbackList.length === 0) {
|
if (callbackList.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove the element from the list
|
// remove the element from the list
|
||||||
let i;
|
let i;
|
||||||
for (i = 0; i < _callbackList.length; i++) {
|
for (i = 0; i < callbackList.length; i++) {
|
||||||
const cb = _callbackList[i];
|
const cb = callbackList[i];
|
||||||
if (cb.key == key) {
|
if (cb.key == key) {
|
||||||
_callbackList.splice(i, 1);
|
callbackList.splice(i, 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// iff it was the first one in the list, reschedule our callback.
|
// iff it was the first one in the list, reschedule our callback.
|
||||||
if (i === 0) {
|
if (i === 0) {
|
||||||
_scheduleRealCallback();
|
scheduleRealCallback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// use the real global.setTimeout to schedule a callback to _runCallbacks.
|
// use the real global.setTimeout to schedule a callback to runCallbacks.
|
||||||
function _scheduleRealCallback() {
|
function scheduleRealCallback(): void {
|
||||||
if (_realCallbackKey) {
|
if (realCallbackKey) {
|
||||||
global.clearTimeout(_realCallbackKey);
|
global.clearTimeout(realCallbackKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
const first = _callbackList[0];
|
const first = callbackList[0];
|
||||||
|
|
||||||
if (!first) {
|
if (!first) {
|
||||||
debuglog("_scheduleRealCallback: no more callbacks, not rescheduling");
|
debuglog("scheduleRealCallback: no more callbacks, not rescheduling");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const now = _now();
|
const timestamp = now();
|
||||||
const delayMs = Math.min(first.runAt - now, TIMER_CHECK_PERIOD_MS);
|
const delayMs = Math.min(first.runAt - timestamp, TIMER_CHECK_PERIOD_MS);
|
||||||
|
|
||||||
debuglog("_scheduleRealCallback: now:", now, "delay:", delayMs);
|
debuglog("scheduleRealCallback: now:", timestamp, "delay:", delayMs);
|
||||||
_realCallbackKey = global.setTimeout(_runCallbacks, delayMs);
|
realCallbackKey = global.setTimeout(runCallbacks, delayMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
function _runCallbacks() {
|
function runCallbacks(): void {
|
||||||
let cb;
|
let cb;
|
||||||
const now = _now();
|
const timestamp = now();
|
||||||
debuglog("_runCallbacks: now:", now);
|
debuglog("runCallbacks: now:", timestamp);
|
||||||
|
|
||||||
// get the list of things to call
|
// get the list of things to call
|
||||||
const callbacksToRun = [];
|
const callbacksToRun = [];
|
||||||
|
// eslint-disable-next-line
|
||||||
while (true) {
|
while (true) {
|
||||||
const first = _callbackList[0];
|
const first = callbackList[0];
|
||||||
if (!first || first.runAt > now) {
|
if (!first || first.runAt > timestamp) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
cb = _callbackList.shift();
|
cb = callbackList.shift();
|
||||||
debuglog("_runCallbacks: popping", cb.key);
|
debuglog("runCallbacks: popping", cb.key);
|
||||||
callbacksToRun.push(cb);
|
callbacksToRun.push(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
// reschedule the real callback before running our functions, to
|
// reschedule the real callback before running our functions, to
|
||||||
// keep the codepaths the same whether or not our functions
|
// keep the codepaths the same whether or not our functions
|
||||||
// register their own setTimeouts.
|
// register their own setTimeouts.
|
||||||
_scheduleRealCallback();
|
scheduleRealCallback();
|
||||||
|
|
||||||
for (let i = 0; i < callbacksToRun.length; i++) {
|
for (let i = 0; i < callbacksToRun.length; i++) {
|
||||||
cb = callbacksToRun[i];
|
cb = callbacksToRun[i];
|
||||||
@@ -182,7 +182,7 @@ function _runCallbacks() {
|
|||||||
* returns the index of the last element for which func returns
|
* returns the index of the last element for which func returns
|
||||||
* greater than zero, or array.length if no such element exists.
|
* greater than zero, or array.length if no such element exists.
|
||||||
*/
|
*/
|
||||||
function binarySearch(array, func) {
|
function binarySearch<T>(array: T[], func: (T) => number): number {
|
||||||
// min is inclusive, max exclusive.
|
// min is inclusive, max exclusive.
|
||||||
let min = 0;
|
let min = 0;
|
||||||
let max = array.length;
|
let max = array.length;
|
||||||
Reference in New Issue
Block a user