mirror of
https://github.com/siyuan-note/plugin-sample-vite-svelte.git
synced 2025-06-07 18:46:01 +00:00
🔧 ci: 更新 vite 打包脚本
This commit is contained in:
parent
4aee720da5
commit
591cf2e95e
10 changed files with 146 additions and 135 deletions
48
src/libs/promise-pool.ts
Normal file
48
src/libs/promise-pool.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
export default class PromiseLimitPool<T> {
|
||||
private maxConcurrent: number;
|
||||
private currentRunning = 0;
|
||||
private queue: (() => void)[] = [];
|
||||
private promises: Promise<T>[] = [];
|
||||
|
||||
constructor(maxConcurrent: number) {
|
||||
this.maxConcurrent = maxConcurrent;
|
||||
}
|
||||
|
||||
add(fn: () => Promise<T>): void {
|
||||
const promise = new Promise<T>((resolve, reject) => {
|
||||
const run = async () => {
|
||||
try {
|
||||
this.currentRunning++;
|
||||
const result = await fn();
|
||||
resolve(result);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
} finally {
|
||||
this.currentRunning--;
|
||||
this.next();
|
||||
}
|
||||
};
|
||||
|
||||
if (this.currentRunning < this.maxConcurrent) {
|
||||
run();
|
||||
} else {
|
||||
this.queue.push(run);
|
||||
}
|
||||
});
|
||||
this.promises.push(promise);
|
||||
}
|
||||
|
||||
async awaitAll(): Promise<T[]> {
|
||||
return Promise.all(this.promises);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the next task in the queue.
|
||||
*/
|
||||
private next(): void {
|
||||
if (this.queue.length > 0 && this.currentRunning < this.maxConcurrent) {
|
||||
const nextRun = this.queue.shift()!;
|
||||
nextRun();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue