Initial commit
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+78
@@ -0,0 +1,78 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.PromiseQueue = void 0;
|
||||
var PromiseQueue = /** @class */ (function () {
|
||||
function PromiseQueue(_a) {
|
||||
var _b = (_a === void 0 ? {} : _a).concurrency, concurrency = _b === void 0 ? 1 : _b;
|
||||
this.options = { concurrency: concurrency };
|
||||
this.running = 0;
|
||||
this.queue = [];
|
||||
this.idleCallbacks = [];
|
||||
}
|
||||
PromiseQueue.prototype.clear = function () {
|
||||
this.queue = [];
|
||||
};
|
||||
PromiseQueue.prototype.onIdle = function (callback) {
|
||||
var _this = this;
|
||||
this.idleCallbacks.push(callback);
|
||||
return function () {
|
||||
var index = _this.idleCallbacks.indexOf(callback);
|
||||
if (index !== -1) {
|
||||
_this.idleCallbacks.splice(index, 1);
|
||||
}
|
||||
};
|
||||
};
|
||||
PromiseQueue.prototype.waitTillIdle = function () {
|
||||
var _this = this;
|
||||
return new Promise(function (resolve) {
|
||||
if (_this.running === 0) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
var dispose = _this.onIdle(function () {
|
||||
dispose();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
};
|
||||
PromiseQueue.prototype.add = function (callback) {
|
||||
var _this = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
var runCallback = function () {
|
||||
_this.running += 1;
|
||||
try {
|
||||
Promise.resolve(callback()).then(function (val) {
|
||||
resolve(val);
|
||||
_this.processNext();
|
||||
}, function (err) {
|
||||
reject(err);
|
||||
_this.processNext();
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
reject(err);
|
||||
_this.processNext();
|
||||
}
|
||||
};
|
||||
if (_this.running >= _this.options.concurrency) {
|
||||
_this.queue.push(runCallback);
|
||||
}
|
||||
else {
|
||||
runCallback();
|
||||
}
|
||||
});
|
||||
};
|
||||
// Internal function, don't use
|
||||
PromiseQueue.prototype.processNext = function () {
|
||||
this.running -= 1;
|
||||
var callback = this.queue.shift();
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
else if (this.running === 0) {
|
||||
this.idleCallbacks.forEach(function (item) { return item(); });
|
||||
}
|
||||
};
|
||||
return PromiseQueue;
|
||||
}());
|
||||
exports.PromiseQueue = PromiseQueue;
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
class PromiseQueue {
|
||||
constructor({ concurrency = 1 } = {}) {
|
||||
this.options = { concurrency };
|
||||
this.running = 0;
|
||||
this.queue = [];
|
||||
this.idleCallbacks = [];
|
||||
}
|
||||
clear() {
|
||||
this.queue = [];
|
||||
}
|
||||
onIdle(callback) {
|
||||
this.idleCallbacks.push(callback);
|
||||
return () => {
|
||||
const index = this.idleCallbacks.indexOf(callback);
|
||||
if (index !== -1) {
|
||||
this.idleCallbacks.splice(index, 1);
|
||||
}
|
||||
};
|
||||
}
|
||||
waitTillIdle() {
|
||||
return new Promise((resolve) => {
|
||||
if (this.running === 0) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
const dispose = this.onIdle(() => {
|
||||
dispose();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
add(callback) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const runCallback = () => {
|
||||
this.running += 1;
|
||||
try {
|
||||
Promise.resolve(callback()).then((val) => {
|
||||
resolve(val);
|
||||
this.processNext();
|
||||
}, (err) => {
|
||||
reject(err);
|
||||
this.processNext();
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
reject(err);
|
||||
this.processNext();
|
||||
}
|
||||
};
|
||||
if (this.running >= this.options.concurrency) {
|
||||
this.queue.push(runCallback);
|
||||
}
|
||||
else {
|
||||
runCallback();
|
||||
}
|
||||
});
|
||||
}
|
||||
// Internal function, don't use
|
||||
processNext() {
|
||||
this.running -= 1;
|
||||
const callback = this.queue.shift();
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
else if (this.running === 0) {
|
||||
this.idleCallbacks.forEach((item) => item());
|
||||
}
|
||||
}
|
||||
}
|
||||
export { PromiseQueue };
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
declare type QueueItem = () => void;
|
||||
declare type IdleCallback = () => void | Promise<void>;
|
||||
declare type AddCallback = () => void | Promise<void>;
|
||||
interface Options {
|
||||
concurrency?: number;
|
||||
}
|
||||
declare class PromiseQueue {
|
||||
options: Required<Options>;
|
||||
running: number;
|
||||
queue: QueueItem[];
|
||||
idleCallbacks: IdleCallback[];
|
||||
constructor({ concurrency }?: Options);
|
||||
clear(): void;
|
||||
onIdle(callback: IdleCallback): () => void;
|
||||
waitTillIdle(): Promise<void>;
|
||||
add(callback: AddCallback): Promise<unknown>;
|
||||
private processNext;
|
||||
}
|
||||
export { PromiseQueue };
|
||||
Reference in New Issue
Block a user