Skip to content

Commit

Permalink
fix: persistent tracking queue flush when tracker set (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori authored Oct 16, 2024
1 parent fef53e5 commit 93532c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
26 changes: 9 additions & 17 deletions packages/experiment-browser/src/integration/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -193,17 +184,18 @@ 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;
for (const event of this.inMemoryQueue) {
if (!this.tracker(event)) return;
}
this.inMemoryQueue = [];
if (this.poller) {
safeGlobal.clearInterval(this.poller);
this.poller = undefined;
}
}

private loadQueue(): void {
Expand Down
12 changes: 6 additions & 6 deletions packages/experiment-browser/test/integration/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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([]);
Expand Down

0 comments on commit 93532c1

Please sign in to comment.