Skip to content

Commit

Permalink
Merge pull request #90 from tungv/fix-test-wrapper
Browse files Browse the repository at this point in the history
Fix test wrapper
  • Loading branch information
ttlong3103 authored Nov 27, 2024
2 parents 3ac8f6a + b7f7f5f commit 247e6ae
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/jerni/src/lib/testWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ export default async function testWrapper(
}

let lastProjectedEventId = 0; // id of the last projected event
let isFlushing = false; // flag to indicate if a flush is in progress
async function flush() {
// if a flush is already in progress, reschedule this flush.
if (isFlushing) {
// Don't use process.nextTick() to avoid infinite loop
setImmediate(flush);
return;
}

isFlushing = true;

const eventsToFlush = [...flushQueue];
flushQueue.length = 0; // clear the queue for the next round of flush() scheduling

Expand All @@ -68,10 +78,13 @@ export default async function testWrapper(
await store.handleEvents(eventsToFlush);
}
// update the last projected event id
// biome-ignore lint/style/noNonNullAssertion: last event is guaranteed to exist
lastProjectedEventId = eventsToFlush.at(-1)!.id;

isFlushing = false;
}

async function waitForEvent(event: JourneyCommittedEvent, timeout: number, elapsed: number = 0) {
async function waitForEvent(event: JourneyCommittedEvent, timeout: number, elapsed = 0) {
const start = Date.now();
// check if the event is already projected
if (event.id <= lastProjectedEventId) return;
Expand Down

0 comments on commit 247e6ae

Please sign in to comment.