first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Create an event queue that processes events sequentially.
|
||||
*
|
||||
* @param processEvent - Async function that processes a single event.
|
||||
* @returns A function that enqueues events for processing.
|
||||
*/
|
||||
export function createEventQueue<T>(processEvent: (event: T) => Promise<void>) {
|
||||
// The internal queue holding events.
|
||||
const queue: T[] = [];
|
||||
|
||||
// Flag to indicate whether an event is currently being processed.
|
||||
let processing = false;
|
||||
|
||||
/**
|
||||
* Process the next event in the queue (if not already processing).
|
||||
*/
|
||||
async function processNext(): Promise<void> {
|
||||
if (processing || queue.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
processing = true;
|
||||
const currentEvent = queue.shift();
|
||||
|
||||
if (currentEvent !== undefined) {
|
||||
try {
|
||||
await processEvent(currentEvent);
|
||||
} catch (error) {
|
||||
console.error('Error processing event:', error);
|
||||
}
|
||||
}
|
||||
|
||||
processing = false;
|
||||
|
||||
// Recursively process the next event.
|
||||
await processNext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue an event and trigger processing.
|
||||
*
|
||||
* @param event - The event to enqueue.
|
||||
*/
|
||||
function enqueue(event: T): void {
|
||||
queue.push(event);
|
||||
void processNext();
|
||||
}
|
||||
|
||||
return { enqueue };
|
||||
}
|
||||
Reference in New Issue
Block a user