Initial commit

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
dinlo
2026-05-31 18:45:31 +08:00
commit e0a986eb30
1018 changed files with 615974 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2018-2020 Steel Brain
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+33
View File
@@ -0,0 +1,33 @@
# Promise-Queue
Promise queue with a nice API.
### Installation
```
npm install --save sb-promise-queue
```
## API
```js
interface Options {
concurrency?: number
}
class PromiseQueue {
constructor(options: Options = {concurrency: 1});
clear()
onIdle(callback: Function): Function
// call the return value function to remove listener
waitTillIdle(): Promise<void>
add(callback: Function)
}
export { PromiseQueue }
```
## License
The contents of this repository/package are licensed under the terms of The MIT License. See the LICENSE file for more info.
+78
View File
@@ -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
View File
@@ -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
View File
@@ -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 };
+65
View File
@@ -0,0 +1,65 @@
{
"name": "sb-promise-queue",
"version": "2.1.1",
"description": "Promise queue with a nice API",
"main": "lib/cjs/index.js",
"typings": "lib/typings/index.d.ts",
"module": "lib/esm/index.mjs",
"exports": {
".": {
"types": "./lib/typings/index.d.ts",
"import": "./lib/esm/index.mjs",
"require": "./lib/cjs/index.js"
},
"./package.json": "./package.json"
},
"type": "commonjs",
"scripts": {
"test": "ava",
"lint": "(tsc -p . --noEmit) && (eslint . --ext .ts) && (prettier --list-different src/*.ts)",
"prepare": "yarn build:clean ; yarn build:esm ; yarn build:cjs ; yarn build:typings",
"build:clean": "rm -rf lib",
"build:esm": "tsc --module es2015 --target es2018 --outDir lib/esm && mv lib/esm/index.js lib/esm/index.mjs",
"build:cjs": "tsc --module commonjs --target es5 --outDir lib/cjs",
"build:typings": "tsc --declaration --outDir lib/typings --emitDeclarationOnly"
},
"repository": {
"type": "git",
"url": "git+https://github.com/steelbrain/promise-queue.git"
},
"author": "steelbrain",
"license": "MIT",
"bugs": {
"url": "https://github.com/steelbrain/promise-queue/issues"
},
"keywords": [
"Promise",
"queue"
],
"files": [
"lib/*"
],
"homepage": "https://github.com/steelbrain/promise-queue#readme",
"devDependencies": {
"ava": "^3.11.1",
"eslint-config-steelbrain": "^10.0.0-beta2",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
},
"dependencies": {},
"ava": {
"files": [
"test/*-test.ts"
],
"extensions": [
"ts"
],
"require": [
"ts-node/register/transpile-only"
]
},
"engines": {
"node": ">= 8"
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}