2013-02-25 23:08:33 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
2015-07-17 08:10:35 +03:00
|
|
|
* JS module implementation of setTimeout and clearTimeout.
|
2013-02-25 23:08:33 +04:00
|
|
|
*/
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = [
|
|
|
|
"setTimeout",
|
|
|
|
"setTimeoutWithTarget",
|
|
|
|
"clearTimeout",
|
2019-03-04 21:53:14 +03:00
|
|
|
"setInterval",
|
|
|
|
"setIntervalWithTarget",
|
|
|
|
"clearInterval",
|
|
|
|
"requestIdleCallback",
|
|
|
|
"cancelIdleCallback",
|
|
|
|
];
|
2013-02-25 23:08:33 +04:00
|
|
|
|
|
|
|
// This gives us >=2^30 unique timer IDs, enough for 1 per ms for 12.4 days.
|
2015-09-15 21:19:45 +03:00
|
|
|
var gNextId = 1; // setTimeout and setInterval must return a positive integer
|
2013-02-25 23:08:33 +04:00
|
|
|
|
2019-03-04 21:53:14 +03:00
|
|
|
var gTimerTable = new Map(); // int -> nsITimer or idleCallback
|
2013-02-25 23:08:33 +04:00
|
|
|
|
2018-02-28 02:31:22 +03:00
|
|
|
// Don't generate this for every timer.
|
2018-04-23 06:55:06 +03:00
|
|
|
var setTimeout_timerCallbackQI = ChromeUtils.generateQI([
|
|
|
|
Ci.nsITimerCallback,
|
|
|
|
Ci.nsINamed,
|
|
|
|
]);
|
2019-07-05 12:15:43 +03:00
|
|
|
|
2017-07-28 11:03:51 +03:00
|
|
|
function _setTimeoutOrIsInterval(
|
|
|
|
aCallback,
|
|
|
|
aMilliseconds,
|
|
|
|
aIsInterval,
|
|
|
|
aTarget,
|
|
|
|
aArgs
|
|
|
|
) {
|
2018-01-27 04:13:34 +03:00
|
|
|
if (typeof aCallback !== "function") {
|
|
|
|
throw new Error(
|
|
|
|
`callback is not a function in ${
|
|
|
|
aIsInterval ? "setInterval" : "setTimeout"
|
|
|
|
}`
|
|
|
|
);
|
|
|
|
}
|
2015-03-28 01:38:17 +03:00
|
|
|
let id = gNextId++;
|
2013-02-25 23:08:33 +04:00
|
|
|
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
2017-07-28 11:03:51 +03:00
|
|
|
|
|
|
|
if (aTarget) {
|
|
|
|
timer.target = aTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
let callback = {
|
2018-02-28 02:31:22 +03:00
|
|
|
QueryInterface: setTimeout_timerCallbackQI,
|
2017-07-28 11:03:51 +03:00
|
|
|
|
|
|
|
// nsITimerCallback
|
|
|
|
notify() {
|
|
|
|
if (!aIsInterval) {
|
|
|
|
gTimerTable.delete(id);
|
|
|
|
}
|
|
|
|
aCallback.apply(null, aArgs);
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsINamed
|
2017-08-24 09:56:18 +03:00
|
|
|
name:
|
|
|
|
(aIsInterval ? "setInterval() for " : "setTimeout() for ") +
|
|
|
|
Cu.generateXPCWrappedJS(aCallback).QueryInterface(Ci.nsINamed).name,
|
2017-07-28 11:03:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
timer.initWithCallback(
|
|
|
|
callback,
|
|
|
|
aMilliseconds,
|
|
|
|
aIsInterval ? timer.TYPE_REPEATING_SLACK : timer.TYPE_ONE_SHOT
|
|
|
|
);
|
2013-02-25 23:08:33 +04:00
|
|
|
|
2015-03-28 01:38:17 +03:00
|
|
|
gTimerTable.set(id, timer);
|
2013-02-25 23:08:33 +04:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
function setTimeout(aCallback, aMilliseconds, ...aArgs) {
|
2017-07-28 11:03:51 +03:00
|
|
|
return _setTimeoutOrIsInterval(aCallback, aMilliseconds, false, null, aArgs);
|
2018-02-23 22:50:01 +03:00
|
|
|
}
|
2015-03-28 01:38:17 +03:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
function setTimeoutWithTarget(aCallback, aMilliseconds, aTarget, ...aArgs) {
|
2017-07-28 11:03:51 +03:00
|
|
|
return _setTimeoutOrIsInterval(
|
|
|
|
aCallback,
|
|
|
|
aMilliseconds,
|
|
|
|
false,
|
|
|
|
aTarget,
|
|
|
|
aArgs
|
|
|
|
);
|
2018-02-23 22:50:01 +03:00
|
|
|
}
|
2017-07-28 11:03:51 +03:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
function setInterval(aCallback, aMilliseconds, ...aArgs) {
|
2017-07-28 11:03:51 +03:00
|
|
|
return _setTimeoutOrIsInterval(aCallback, aMilliseconds, true, null, aArgs);
|
2018-02-23 22:50:01 +03:00
|
|
|
}
|
2017-07-28 11:03:51 +03:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
function setIntervalWithTarget(aCallback, aMilliseconds, aTarget, ...aArgs) {
|
2017-07-28 11:03:51 +03:00
|
|
|
return _setTimeoutOrIsInterval(
|
|
|
|
aCallback,
|
|
|
|
aMilliseconds,
|
|
|
|
true,
|
|
|
|
aTarget,
|
|
|
|
aArgs
|
|
|
|
);
|
2018-02-23 22:50:01 +03:00
|
|
|
}
|
2015-03-28 01:38:17 +03:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var clearInterval = (this.clearTimeout = function clearTimeout(aId) {
|
2015-03-28 01:38:17 +03:00
|
|
|
if (gTimerTable.has(aId)) {
|
|
|
|
gTimerTable.get(aId).cancel();
|
|
|
|
gTimerTable.delete(aId);
|
2013-02-25 23:08:33 +04:00
|
|
|
}
|
2017-10-15 21:50:30 +03:00
|
|
|
});
|
2019-03-04 21:53:14 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancelIdleCallback(aId) {
|
|
|
|
if (gTimerTable.has(aId)) {
|
|
|
|
gTimerTable.delete(aId);
|
|
|
|
}
|
|
|
|
}
|