Move lazy, spawn, and serial to Utilities.Internal

This commit is contained in:
Sylvester Keil 2018-08-02 17:20:09 +02:00
parent 95559a0c62
commit 91ddec5bd9
No known key found for this signature in database
GPG key ID: 878933BCEAB25A10
2 changed files with 45 additions and 36 deletions

View file

@ -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

View file

@ -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);
}