2023-10-20 22:35:26 +08:00
|
|
|
// SPDX-FileCopyrightText: 2023 XWiki CryptPad Team <contact@cryptpad.org> and contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2019-09-04 17:51:33 +08:00
|
|
|
/*
|
|
|
|
var q = Queue();
|
|
|
|
q(id, function (next) {
|
|
|
|
// whatever you need to do....
|
|
|
|
|
|
|
|
// when you're done
|
2019-12-06 02:35:26 +08:00
|
|
|
next(); // guaranteed to be asynchronous :D
|
2019-09-04 17:51:33 +08:00
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
|
|
|
var fix1 = function (f, x) {
|
|
|
|
return function () { f(x); };
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = function () {
|
|
|
|
var map = {};
|
|
|
|
|
|
|
|
var next = function (id) {
|
2019-12-06 02:35:26 +08:00
|
|
|
setTimeout(function () {
|
2020-07-23 21:27:29 +08:00
|
|
|
if (!map[id] || map[id].length === 0) { return void delete map[id]; }
|
2019-12-06 02:35:26 +08:00
|
|
|
var task = map[id].shift();
|
|
|
|
task(fix1(next, id));
|
|
|
|
});
|
2019-09-04 17:51:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return function (id, task) {
|
|
|
|
// support initialization with just a function
|
|
|
|
if (typeof(id) === 'function' && typeof(task) === 'undefined') {
|
|
|
|
task = id;
|
|
|
|
id = '';
|
|
|
|
}
|
|
|
|
// ...but you really need to pass a function
|
|
|
|
if (typeof(task) !== 'function') { throw new Error("Expected function"); }
|
|
|
|
|
|
|
|
// if the intended queue already has tasks in progress, add this one to the end of the queue
|
|
|
|
if (map[id]) { return void map[id].push(task); }
|
|
|
|
|
|
|
|
// otherwise create a queue containing the given task
|
|
|
|
map[id] = [task];
|
|
|
|
next(id);
|
|
|
|
};
|
|
|
|
};
|