Download src/utils/queue-manager.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 1.68 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/queue-manager.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/utils/queue-manager.js
-
curl -L -o queue-manager.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/queue-manager.js
1.68 kB
| class ConcurrencyQueue { | |
| constructor(maxConcurrency = 2) { | |
| this.maxConcurrency = maxConcurrency; | |
| this.runningCount = 0; | |
| this.queue = []; | |
| } | |
| enqueue(taskFn, { signal } = {}) { | |
| return new Promise((resolve, reject) => { | |
| // ์ทจ์ํ ๋๊ธฐ ํญ๋ชฉ๋ง ํ์ํ๋ค. ์คํ ์ค์ธ ๋ค๋ฅธ ์์ ์ด๋ ์ ์ฒด ํ๋ฅผ ๋น์ฐ์ง ์๋๋ค. | |
| const cleanup = () => signal?.removeEventListener('abort', abort); | |
| const entry = { taskFn, resolve, reject, cleanup }; | |
| const abort = () => { | |
| const index = this.queue.indexOf(entry); | |
| if (index >= 0) this.queue.splice(index, 1); | |
| cleanup(); | |
| reject(new Error('Queued request cancelled')); | |
| }; | |
| if (signal?.aborted) { abort(); return; } | |
| signal?.addEventListener('abort', abort, { once: true }); | |
| this.queue.push(entry); | |
| this.next(); | |
| }); | |
| } | |
| next() { | |
| if (this.runningCount >= this.maxConcurrency) { | |
| return; | |
| } | |
| if (this.queue.length === 0) { | |
| return; | |
| } | |
| const { taskFn, resolve, reject, cleanup } = this.queue.shift(); | |
| cleanup(); | |
| this.runningCount++; | |
| Promise.resolve() | |
| .then(() => taskFn()) | |
| .then(result => { | |
| this.runningCount = Math.max(0, this.runningCount - 1); | |
| resolve(result); | |
| this.next(); | |
| }) | |
| .catch(err => { | |
| this.runningCount = Math.max(0, this.runningCount - 1); | |
| reject(err); | |
| this.next(); | |
| }); | |
| } | |
| cancelAll() { | |
| const pending = this.queue.splice(0); | |
| for (const { reject, cleanup } of pending) { | |
| cleanup(); | |
| reject(new Error('Queue cancelled')); | |
| } | |
| } | |
| } | |
| module.exports = { ConcurrencyQueue }; | |