Skip to content

Commit dac4d6b

Browse files
sunitaprajapati89Sunita Prajapati
andauthored
fix: fixed race condition causing events to be lost during initialization (#1103)
* fix: fixed race condition causing events to be lost during initialization - added unit test cases - fixed the race condition * fix: race condition to prevent loss of events during initialisation - refactored onReady() - removed unused tests which were written for wrong fix --------- Co-authored-by: Sunita Prajapati <[email protected]>
1 parent 409f954 commit dac4d6b

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

packages/core/src/__tests__/methods/process.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ describe('process', () => {
6868
jest.spyOn(client.isReady, 'value', 'get').mockReturnValue(true);
6969
// @ts-ignore
7070
await client.onReady();
71+
// @ts-ignore
72+
await client.processPendingEvents();
7173
expectedEvent = {
7274
...expectedEvent,
7375
context: { ...store.context.get() },

packages/core/src/analytics.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,13 @@ export class SegmentClient {
307307
// check if the app was opened from a deep link
308308
this.trackDeepLinks(),
309309
]);
310-
311310
await this.onReady();
312311
this.isReady.value = true;
312+
313+
// Process all pending events
314+
await this.processPendingEvents();
315+
// Trigger manual flush
316+
this.flushPolicyExecuter.manualFlush();
313317
} catch (error) {
314318
this.reportInternalError(
315319
new SegmentError(
@@ -561,15 +565,13 @@ export class SegmentClient {
561565
// Start flush policies
562566
// This should be done before any pending events are added to the queue so that any policies that rely on events queued can trigger accordingly
563567
this.setupFlushPolicies();
564-
565-
// Send all events in the queue
568+
}
569+
private async processPendingEvents() {
566570
const pending = await this.store.pendingEvents.get(true);
567-
for (const e of pending) {
568-
await this.startTimelineProcessing(e);
569-
await this.store.pendingEvents.remove(e);
571+
for (const event of pending) {
572+
await this.startTimelineProcessing(event);
573+
await this.store.pendingEvents.remove(event);
570574
}
571-
572-
this.flushPolicyExecuter.manualFlush();
573575
}
574576

575577
async flush(): Promise<void> {

0 commit comments

Comments
 (0)