Skip to content

Commit

Permalink
feat(async-queue): add isRunning
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm committed Jan 3, 2024
1 parent 83b2e11 commit 2e54a0a
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/async-queue/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {Dictionary} from '@alwatr/type-helper';
* ```
*/
export class AsyncQueue {
queue_: Dictionary<Promise<void>> = {};
private queue__: Dictionary<Promise<void>> = {};

/**
* Push a async task to the queue.
Expand All @@ -39,8 +39,8 @@ export class AsyncQueue {
async push(taskId: string, task: () => Promise<void>): Promise<void> {
const flatomise = newFlatomise();

const previousTaskPromise = this.queue_[taskId];
this.queue_[taskId] = flatomise.promise;
const previousTaskPromise = this.queue__[taskId];
this.queue__[taskId] = flatomise.promise;

try {
await previousTaskPromise;
Expand All @@ -51,12 +51,22 @@ export class AsyncQueue {

setTimeout(() => {
task().then(flatomise.resolve, flatomise.reject).then(() => {
if (this.queue_[taskId] === flatomise.promise) {
delete this.queue_[taskId];
if (this.queue__[taskId] === flatomise.promise) {
delete this.queue__[taskId];
}
});
}, 0);

return flatomise.promise;
}

/**
* Check if the task running in the queue.
*
* @param taskId task id
* @returns true if the task is running, otherwise false.
*/
isRunning(taskId: string): boolean {
return this.queue__[taskId] !== undefined;
}
}

0 comments on commit 2e54a0a

Please sign in to comment.