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
|
|
|
*/
|
|
|
|
|
2017-07-28 11:03:51 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["setTimeout", "setTimeoutWithTarget", "clearTimeout",
|
|
|
|
"setInterval", "setIntervalWithTarget", "clearInterval"];
|
2013-02-25 23:08:33 +04:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
2017-07-28 11:03:51 +03:00
|
|
|
const Cr = Components.results;
|
2013-02-25 23:08:33 +04:00
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
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
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var gTimerTable = new Map(); // int -> nsITimer
|
2013-02-25 23:08:33 +04: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 = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback,
|
|
|
|
Ci.nsINamed]),
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2017-07-28 11:03:51 +03:00
|
|
|
this.setTimeout = function setTimeout(aCallback, aMilliseconds, ...aArgs) {
|
|
|
|
return _setTimeoutOrIsInterval(
|
|
|
|
aCallback, aMilliseconds, false, null, aArgs);
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2015-03-28 01:38:17 +03:00
|
|
|
|
2017-07-28 11:03:51 +03:00
|
|
|
this.setTimeoutWithTarget = function setTimeoutWithTarget(aCallback,
|
|
|
|
aMilliseconds,
|
|
|
|
aTarget,
|
|
|
|
...aArgs) {
|
|
|
|
return _setTimeoutOrIsInterval(
|
|
|
|
aCallback, aMilliseconds, false, aTarget, aArgs);
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2017-07-28 11:03:51 +03:00
|
|
|
|
|
|
|
this.setInterval = function setInterval(aCallback, aMilliseconds, ...aArgs) {
|
|
|
|
return _setTimeoutOrIsInterval(
|
|
|
|
aCallback, aMilliseconds, true, null, aArgs);
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2017-07-28 11:03:51 +03:00
|
|
|
|
|
|
|
this.setIntervalWithTarget = function setIntervalWithTarget(aCallback,
|
|
|
|
aMilliseconds,
|
|
|
|
aTarget,
|
|
|
|
...aArgs) {
|
|
|
|
return _setTimeoutOrIsInterval(
|
|
|
|
aCallback, aMilliseconds, true, aTarget, aArgs);
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2015-03-28 01:38:17 +03:00
|
|
|
|
|
|
|
this.clearInterval = this.clearTimeout = function clearTimeout(aId) {
|
|
|
|
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
|
|
|
};
|