Skip to content

Commit

Permalink
Add comments and docs to OperationQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
joaosreis committed Jan 10, 2025
1 parent e242173 commit 8bb6b68
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/common/src/operationQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,33 @@ export class OperationQueue {
private queue: Promise<void>;

constructor() {
// Start with a resolved promise
this.queue = Promise.resolve();
}

/**
* Queue an operation to be executed
* @param operation - The operation to queue
* @returns A promise that resolves when the operation is completed
*/
public queueOperation<T>(operation: AsyncOperation<T>): Promise<T> {
// Chain the operation to the end of the queue and store the result
// of the queued operation to return to the caller if he wants to wait
// for it
const result = this.queue.then(() => operation());

// Update the queue and discard any errors
this.queue = result.then(
() => undefined,
() => undefined
);
return result;
}

/**
* Wait for all operations to complete
* @returns A promise that resolves when all operations are completed
*/
public async onCompleted(): Promise<void> {
await this.queue;
}
Expand Down

0 comments on commit 8bb6b68

Please sign in to comment.