summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/Timer.sys.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/modules/Timer.sys.mjs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/modules/Timer.sys.mjs')
-rw-r--r--toolkit/modules/Timer.sys.mjs139
1 files changed, 139 insertions, 0 deletions
diff --git a/toolkit/modules/Timer.sys.mjs b/toolkit/modules/Timer.sys.mjs
new file mode 100644
index 0000000000..c77fe90e11
--- /dev/null
+++ b/toolkit/modules/Timer.sys.mjs
@@ -0,0 +1,139 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * JS module implementation of setTimeout and clearTimeout.
+ */
+
+// This gives us >=2^30 unique timer IDs, enough for 1 per ms for 12.4 days.
+var gNextId = 1; // setTimeout and setInterval must return a positive integer
+
+var gTimerTable = new Map(); // int -> nsITimer or idleCallback
+
+// Don't generate this for every timer.
+var setTimeout_timerCallbackQI = ChromeUtils.generateQI([
+ "nsITimerCallback",
+ "nsINamed",
+]);
+
+function _setTimeoutOrIsInterval(
+ aCallback,
+ aMilliseconds,
+ aIsInterval,
+ aTarget,
+ aArgs
+) {
+ if (typeof aCallback !== "function") {
+ throw new Error(
+ `callback is not a function in ${
+ aIsInterval ? "setInterval" : "setTimeout"
+ }`
+ );
+ }
+ let id = gNextId++;
+ let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+
+ if (aTarget) {
+ timer.target = aTarget;
+ }
+
+ let callback = {
+ QueryInterface: setTimeout_timerCallbackQI,
+
+ // nsITimerCallback
+ notify() {
+ if (!aIsInterval) {
+ gTimerTable.delete(id);
+ }
+ aCallback.apply(null, aArgs);
+ },
+
+ // nsINamed
+ get name() {
+ return `${
+ aIsInterval ? "setInterval" : "setTimeout"
+ }() for ${Cu.getDebugName(aCallback)}`;
+ },
+ };
+
+ timer.initWithCallback(
+ callback,
+ aMilliseconds,
+ aIsInterval ? timer.TYPE_REPEATING_SLACK : timer.TYPE_ONE_SHOT
+ );
+
+ gTimerTable.set(id, timer);
+ return id;
+}
+
+export function setTimeout(aCallback, aMilliseconds, ...aArgs) {
+ return _setTimeoutOrIsInterval(aCallback, aMilliseconds, false, null, aArgs);
+}
+
+export function setTimeoutWithTarget(
+ aCallback,
+ aMilliseconds,
+ aTarget,
+ ...aArgs
+) {
+ return _setTimeoutOrIsInterval(
+ aCallback,
+ aMilliseconds,
+ false,
+ aTarget,
+ aArgs
+ );
+}
+
+export function setInterval(aCallback, aMilliseconds, ...aArgs) {
+ return _setTimeoutOrIsInterval(aCallback, aMilliseconds, true, null, aArgs);
+}
+
+export function setIntervalWithTarget(
+ aCallback,
+ aMilliseconds,
+ aTarget,
+ ...aArgs
+) {
+ return _setTimeoutOrIsInterval(
+ aCallback,
+ aMilliseconds,
+ true,
+ aTarget,
+ aArgs
+ );
+}
+
+function clear(aId) {
+ if (gTimerTable.has(aId)) {
+ gTimerTable.get(aId).cancel();
+ gTimerTable.delete(aId);
+ }
+}
+export var clearInterval = clear;
+export var clearTimeout = clear;
+
+export function requestIdleCallback(aCallback, aOptions) {
+ if (typeof aCallback !== "function") {
+ throw new Error("callback is not a function in requestIdleCallback");
+ }
+ let id = gNextId++;
+
+ let callback = (...aArgs) => {
+ if (gTimerTable.has(id)) {
+ gTimerTable.delete(id);
+ aCallback(...aArgs);
+ }
+ };
+
+ ChromeUtils.idleDispatch(callback, aOptions);
+ gTimerTable.set(id, callback);
+ return id;
+}
+
+export function cancelIdleCallback(aId) {
+ if (gTimerTable.has(aId)) {
+ gTimerTable.delete(aId);
+ }
+}