Timer updates

- Switch to Mozilla's Timer.jsm for timer functions in XPCOM scope
- Add setInterval/clearInterval/requestIdleCallback/cancelIdleCallback
- Add all timer functions to plugins sandbox
This commit is contained in:
Dan Stillman 2023-05-28 04:48:31 -04:00
parent f966662911
commit 7c902d40a9
3 changed files with 18 additions and 48 deletions

View file

@ -92,8 +92,12 @@ Zotero.Plugins = new function () {
}
Object.assign(scope, { Services, Worker, ChromeWorker, Zotero });
// Add additional global functions
scope.setTimeout = Zotero.setTimeout;
scope.clearTimeout = Zotero.clearTimeout;
scope.setTimeout = setTimeout;
scope.clearTimeout = clearTimeout;
scope.setInterval = setInterval;
scope.clearInterval = clearInterval;
scope.requestIdleCallback = requestIdleCallback;
scope.cancelIdleCallback = cancelIdleCallback;
scopes.set(addon.id, scope);

View file

@ -1427,41 +1427,6 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
return Zotero.Utilities.Internal.spawn(generator, thisObject);
}
/**
* Emulates the behavior of window.setTimeout
*
* @param {Function} func The function to be called
* @param {Integer} ms The number of milliseconds to wait before calling func
* @return {Integer} - ID of timer to be passed to clearTimeout()
*/
var _lastTimeoutID = 0;
this.setTimeout = function (func, ms) {
var id = ++_lastTimeoutID;
var timer = Components.classes["@mozilla.org/timer;1"]
.createInstance(Components.interfaces.nsITimer);
var timerCallback = {
"notify": function () {
func();
_runningTimers.delete(id);
}
};
timer.initWithCallback(timerCallback, ms, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
_runningTimers.set(id, timer);
return id;
};
this.clearTimeout = function (id) {
var timer = _runningTimers.get(id);
if (timer) {
timer.cancel();
_runningTimers.delete(id);
}
};
/**
* Show Zotero pane overlay and progress bar in all windows
*

View file

@ -200,17 +200,6 @@ ZoteroContext.prototype = {
"Cc":Cc,
"Ci":Ci,
/**
* Convenience method to replicate window.setTimeout()
**/
"setTimeout":function setTimeout(func, ms){
return this.Zotero.setTimeout(func, ms);
},
"clearTimeout":function setTimeout(id) {
this.Zotero.clearTimeout(id);
},
/**
* Switches in or out of connector mode
*/
@ -246,6 +235,18 @@ ZoteroContext.prototype = {
}
};
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyModuleGetters(ZoteroContext.prototype, {
setTimeout: "resource://gre/modules/Timer.jsm",
clearTimeout: "resource://gre/modules/Timer.jsm",
setInterval: "resource://gre/modules/Timer.jsm",
clearInterval: "resource://gre/modules/Timer.jsm",
requestIdleCallback: "resource://gre/modules/Timer.jsm",
cancelIdleCallback: "resource://gre/modules/Timer.jsm",
});
/**
* The class from which the Zotero global XPCOM context is constructed
*