diff --git a/packages/core/src/__tests__/methods/process.test.ts b/packages/core/src/__tests__/methods/process.test.ts index 1b8beb58..69f22287 100644 --- a/packages/core/src/__tests__/methods/process.test.ts +++ b/packages/core/src/__tests__/methods/process.test.ts @@ -68,6 +68,8 @@ describe('process', () => { jest.spyOn(client.isReady, 'value', 'get').mockReturnValue(true); // @ts-ignore await client.onReady(); + // @ts-ignore + await client.processPendingEvents(); expectedEvent = { ...expectedEvent, context: { ...store.context.get() }, diff --git a/packages/core/src/analytics.ts b/packages/core/src/analytics.ts index 9f4ec731..1ded23b6 100644 --- a/packages/core/src/analytics.ts +++ b/packages/core/src/analytics.ts @@ -307,9 +307,13 @@ export class SegmentClient { // check if the app was opened from a deep link this.trackDeepLinks(), ]); - await this.onReady(); this.isReady.value = true; + + // Process all pending events + await this.processPendingEvents(); + // Trigger manual flush + this.flushPolicyExecuter.manualFlush(); } catch (error) { this.reportInternalError( new SegmentError( @@ -561,15 +565,13 @@ export class SegmentClient { // Start flush policies // 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 this.setupFlushPolicies(); - - // Send all events in the queue + } + private async processPendingEvents() { const pending = await this.store.pendingEvents.get(true); - for (const e of pending) { - await this.startTimelineProcessing(e); - await this.store.pendingEvents.remove(e); + for (const event of pending) { + await this.startTimelineProcessing(event); + await this.store.pendingEvents.remove(event); } - - this.flushPolicyExecuter.manualFlush(); } async flush(): Promise {