Add clearTimeout() implementation to Bluebird
Necessary for cancellable promises
This commit is contained in:
parent
03da4a9d52
commit
aa70e60fc6
|
@ -35,47 +35,54 @@
|
||||||
// Set BackstagePass (which contains .Error, etc.) as global object
|
// Set BackstagePass (which contains .Error, etc.) as global object
|
||||||
self = this;
|
self = this;
|
||||||
|
|
||||||
|
// We need to maintain references to running nsITimers. Otherwise, they can
|
||||||
|
// get garbage collected before they fire. Also, we might need to cancel them.
|
||||||
|
var _runningTimers = {};
|
||||||
|
|
||||||
// Provide an implementation of setTimeout
|
// Provide an implementation of setTimeout
|
||||||
self.setTimeout = new function() {
|
self.setTimeout = function (func, ms) {
|
||||||
// We need to maintain references to running nsITimers. Otherwise, they can
|
var id = Math.floor(Math.random() * (1000000000000 - 1)) + 1
|
||||||
// get garbage collected before they fire.
|
var useMethodjit = Components.utils.methodjit;
|
||||||
var _runningTimers = [];
|
var timer = Components.classes["@mozilla.org/timer;1"]
|
||||||
|
.createInstance(Components.interfaces.nsITimer);
|
||||||
return function setTimeout(func, ms) {
|
timer.initWithCallback({"notify":function() {
|
||||||
var useMethodjit = Components.utils.methodjit;
|
// Remove timer from object so it can be garbage collected
|
||||||
var timer = Components.classes["@mozilla.org/timer;1"]
|
delete _runningTimers[id];
|
||||||
.createInstance(Components.interfaces.nsITimer);
|
|
||||||
timer.initWithCallback({"notify":function() {
|
// Execute callback function
|
||||||
// Remove timer from array so it can be garbage collected
|
try {
|
||||||
_runningTimers.splice(_runningTimers.indexOf(timer), 1);
|
func();
|
||||||
|
} catch(err) {
|
||||||
// Execute callback function
|
// Rethrow errors that occur so that they appear in the error
|
||||||
try {
|
// console with the appropriate name and line numbers. While the
|
||||||
func();
|
// the errors appear without this, the line numbers get eaten.
|
||||||
} catch(err) {
|
var scriptError = Components.classes["@mozilla.org/scripterror;1"]
|
||||||
// Rethrow errors that occur so that they appear in the error
|
.createInstance(Components.interfaces.nsIScriptError);
|
||||||
// console with the appropriate name and line numbers. While the
|
scriptError.init(
|
||||||
// the errors appear without this, the line numbers get eaten.
|
err.message || err.toString(),
|
||||||
var scriptError = Components.classes["@mozilla.org/scripterror;1"]
|
err.fileName || err.filename || null,
|
||||||
.createInstance(Components.interfaces.nsIScriptError);
|
null,
|
||||||
scriptError.init(
|
err.lineNumber || null,
|
||||||
err.message || err.toString(),
|
null,
|
||||||
err.fileName || err.filename || null,
|
scriptError.errorFlag,
|
||||||
null,
|
'component javascript'
|
||||||
err.lineNumber || null,
|
);
|
||||||
null,
|
Components.classes["@mozilla.org/consoleservice;1"]
|
||||||
scriptError.errorFlag,
|
.getService(Components.interfaces.nsIConsoleService)
|
||||||
'component javascript'
|
.logMessage(scriptError);
|
||||||
);
|
self.debug(err.stack, 1);
|
||||||
Components.classes["@mozilla.org/consoleservice;1"]
|
}
|
||||||
.getService(Components.interfaces.nsIConsoleService)
|
}}, ms, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
|
||||||
.logMessage(scriptError);
|
_runningTimers[id] = timer;
|
||||||
self.debug(err.stack, 1);
|
return id;
|
||||||
}
|
|
||||||
}}, ms, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
|
|
||||||
_runningTimers.push(timer);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
self.clearTimeout = function (id) {
|
||||||
|
var timer = _runningTimers[id];
|
||||||
|
if (timer) {
|
||||||
|
timer.cancel();
|
||||||
|
}
|
||||||
|
delete _runningTimers[id];
|
||||||
|
}
|
||||||
|
|
||||||
self.debug = function (msg) {
|
self.debug = function (msg) {
|
||||||
dump(msg + "\n\n");
|
dump(msg + "\n\n");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user