epii-blocking-queue
Advanced tools
+6
-0
@@ -8,5 +8,8 @@ export type TaskItem = { | ||
| export declare class BlockingQueue { | ||
| private interval; | ||
| queue: Array<TaskItem>; | ||
| enable: boolean; | ||
| stopimmediate: boolean; | ||
| lockWaitNum: number; | ||
| lockFinishNum: number; | ||
| constructor(interval?: number); | ||
@@ -18,2 +21,5 @@ push(fun: Function): Promise<any>; | ||
| clear(): void; | ||
| lock(): Promise<void>; | ||
| unlock(): void; | ||
| } | ||
| export declare function getBlockingQueue(name?: string): BlockingQueue; |
+37
-2
@@ -12,3 +12,3 @@ "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.BlockingQueue = exports.sleep = void 0; | ||
| exports.getBlockingQueue = exports.BlockingQueue = exports.sleep = void 0; | ||
| function sleep(t) { return new Promise((r) => setTimeout(r, t)); } | ||
@@ -19,5 +19,8 @@ exports.sleep = sleep; | ||
| constructor(interval = 50) { | ||
| this.interval = interval; | ||
| this.queue = []; | ||
| this.enable = true; | ||
| this.stopimmediate = true; | ||
| this.lockWaitNum = 0; | ||
| this.lockFinishNum = 0; | ||
| (() => __awaiter(this, void 0, void 0, function* () { | ||
@@ -38,4 +41,7 @@ while (true) { | ||
| } | ||
| yield sleep(50); | ||
| } | ||
| yield sleep(interval); | ||
| else { | ||
| yield sleep(interval); | ||
| } | ||
| } | ||
@@ -72,3 +78,32 @@ }))(); | ||
| } | ||
| lock() { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| this.lockWaitNum++; | ||
| let _forNum = this.lockWaitNum; | ||
| yield this.push(() => __awaiter(this, void 0, void 0, function* () { | ||
| while (true) { | ||
| if (_forNum - 1 === this.lockFinishNum) { | ||
| break; | ||
| } | ||
| if (!this.enable) { | ||
| this.unlock(); | ||
| throw new Error("thread is stoped"); | ||
| } | ||
| yield sleep(100); | ||
| } | ||
| })); | ||
| }); | ||
| } | ||
| unlock() { | ||
| this.lockFinishNum++; | ||
| } | ||
| } | ||
| exports.BlockingQueue = BlockingQueue; | ||
| let queues = {}; | ||
| function getBlockingQueue(name = "main") { | ||
| if (!queues[name]) { | ||
| queues[name] = new BlockingQueue(); | ||
| } | ||
| return queues[name]; | ||
| } | ||
| exports.getBlockingQueue = getBlockingQueue; |
+1
-1
| { | ||
| "name": "epii-blocking-queue", | ||
| "version": "1.0.0", | ||
| "version": "1.1.0", | ||
| "description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
| { | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "target": "ES2016", | ||
| "module": "CommonJS", | ||
| "declaration": true, | ||
| "strict": true, | ||
| "esModuleInterop": true | ||
| }, | ||
| "include": ["src"] | ||
| } |
4250
32.73%128
31.96%3
-25%