diff --git a/packages/experiment-browser/src/integration/manager.ts b/packages/experiment-browser/src/integration/manager.ts index d8508aa..1205da1 100644 --- a/packages/experiment-browser/src/integration/manager.ts +++ b/packages/experiment-browser/src/integration/manager.ts @@ -61,17 +61,17 @@ export class IntegrationManager { if (integration.setup) { this.integration.setup(this.config, this.client).then( () => { - this.queue.tracker = this.integration.track.bind(integration); + this.queue.setTracker(this.integration.track.bind(integration)); this.resolve(); }, (e) => { console.error('Integration setup failed.', e); - this.queue.tracker = this.integration.track.bind(integration); + this.queue.setTracker(this.integration.track.bind(integration)); this.resolve(); }, ); } else { - this.queue.tracker = this.integration.track.bind(integration); + this.queue.setTracker(this.integration.track.bind(integration)); this.resolve(); } } @@ -170,20 +170,11 @@ export class PersistentTrackingQueue { private readonly maxQueueSize: number; private readonly isLocalStorageAvailable = isLocalStorageAvailable(); private inMemoryQueue: ExperimentEvent[] = []; - private poller: any | undefined; - - tracker: ((event: ExperimentEvent) => boolean) | undefined; + private tracker: ((event: ExperimentEvent) => boolean) | undefined; constructor(instanceName: string, maxQueueSize: number = MAX_QUEUE_SIZE) { this.storageKey = `EXP_unsent_${instanceName}`; this.maxQueueSize = maxQueueSize; - this.loadQueue(); - if (this.inMemoryQueue.length > 0) { - this.poller = safeGlobal.setInterval(() => { - this.loadFlushStore(); - }, 1000); - } - this.loadFlushStore(); } push(event: ExperimentEvent): void { @@ -193,6 +184,11 @@ export class PersistentTrackingQueue { this.storeQueue(); } + setTracker(tracker: (event: ExperimentEvent) => boolean): void { + this.tracker = tracker; + this.loadFlushStore(); + } + private flush(): void { if (!this.tracker) return; if (this.inMemoryQueue.length === 0) return; @@ -200,10 +196,6 @@ export class PersistentTrackingQueue { if (!this.tracker(event)) return; } this.inMemoryQueue = []; - if (this.poller) { - safeGlobal.clearInterval(this.poller); - this.poller = undefined; - } } private loadQueue(): void { diff --git a/packages/experiment-browser/test/integration/manager.test.ts b/packages/experiment-browser/test/integration/manager.test.ts index e93fe8a..08eadd2 100644 --- a/packages/experiment-browser/test/integration/manager.test.ts +++ b/packages/experiment-browser/test/integration/manager.test.ts @@ -323,10 +323,10 @@ describe('PersistentTrackingQueue', () => { const instanceName = '$default_instance'; const queue = new PersistentTrackingQueue(instanceName); const trackedEvents: ExperimentEvent[] = []; - queue.tracker = (event) => { + queue.setTracker((event) => { trackedEvents.push(event); return false; - }; + }); const event: ExperimentEvent = { eventType: '$exposure', eventProperties: { @@ -354,10 +354,10 @@ describe('PersistentTrackingQueue', () => { const instanceName = '$default_instance'; const queue = new PersistentTrackingQueue(instanceName); const trackedEvents: ExperimentEvent[] = []; - queue.tracker = (event) => { + queue.setTracker((event) => { trackedEvents.push(event); return true; - }; + }); const event: ExperimentEvent = { eventType: '$exposure', eventProperties: { @@ -399,10 +399,10 @@ describe('PersistentTrackingQueue', () => { JSON.stringify([event]), ); - queue.tracker = (event) => { + queue.setTracker((event) => { trackedEvents.push(event); return true; - }; + }); queue.push(event); expect(queue['inMemoryQueue']).toEqual([]);