diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index b59710cb56..c781852582 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -1293,6 +1293,46 @@ Zotero.Utilities.Internal = { .join('\n'); }, + /** + * Generate a function that produces a static output + * + * Zotero.lazy(fn) returns a function. The first time this function + * is called, it calls fn() and returns its output. Subsequent + * calls return the same output as the first without calling fn() + * again. + */ + lazy: function (fn) { + var x, called = false; + return function() { + if(!called) { + x = fn.apply(this); + called = true; + } + return x; + }; + }, + + serial: function (fn) { + Components.utils.import("resource://zotero/concurrentCaller.js"); + var caller = new ConcurrentCaller({ + numConcurrent: 1, + onError: e => Zotero.logError(e) + }); + return function () { + var args = arguments; + return caller.start(function () { + return fn.apply(this, args); + }.bind(this)); + }; + }, + + spawn: function (generator, thisObject) { + if (thisObject) { + return Zotero.Promise.coroutine(generator.bind(thisObject))(); + } + return Zotero.Promise.coroutine(generator)(); + }, + /** * Defines property on the object * More compact way to do Object.defineProperty diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index 3de48b9139..c96bba8c53 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -1365,47 +1365,16 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js"); return file; } - - /** - * Generate a function that produces a static output - * - * Zotero.lazy(fn) returns a function. The first time this function - * is called, it calls fn() and returns its output. Subsequent - * calls return the same output as the first without calling fn() - * again. - */ this.lazy = function(fn) { - var x, called = false; - return function() { - if(!called) { - x = fn.apply(this); - called = true; - } - return x; - }; - }; - - - this.serial = function (fn) { - Components.utils.import("resource://zotero/concurrentCaller.js"); - var caller = new ConcurrentCaller({ - numConcurrent: 1, - onError: e => Zotero.logError(e) - }); - return function () { - var args = arguments; - return caller.start(function () { - return fn.apply(this, args); - }.bind(this)); - }; + return Zotero.Utilities.Internal.lazy(fn); } + this.serial = function (fn) { + return Zotero.Utilities.Internal.serial(fn); + } this.spawn = function (generator, thisObject) { - if (thisObject) { - return Zotero.Promise.coroutine(generator.bind(thisObject))(); - } - return Zotero.Promise.coroutine(generator)(); + return Zotero.Utilities.Internal.spawn(generator, thisObject); }