Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhancement queue flush client methods #985

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,34 @@
return { ...this.config };
}

/**
* Method for clearing flush queue.
*/
clearFlushQueue() {
const plugins = this.getPlugins();
plugins.forEach(async (plugin) => {
if (plugin.type == PluginType.destination) {

Check warning on line 176 in packages/core/src/analytics.ts

View workflow job for this annotation

GitHub Actions / build-and-test

Expected '===' and instead saw '=='
await plugin?.clearFlushQueue();
this.flushPolicyExecuter.reset();
}
});
}

/**
* Method to get count of events in flush queue.
*/
async getFlushQueueCount() {
const plugins = this.getPlugins();
let totalEventsCount = 0;
for (let i = 0; i <= plugins.length; i++) {
if (plugins[i]?.type == PluginType.destination) {

Check warning on line 190 in packages/core/src/analytics.ts

View workflow job for this annotation

GitHub Actions / build-and-test

Expected '===' and instead saw '=='
const eventsCount = await plugins[i]?.getQueueCount();
totalEventsCount += eventsCount;
}
}
return totalEventsCount;
}

constructor({
config,
logger,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export const useAnalytics = (): ClientMethods => {
group: async (...args) => client?.group(...args),
alias: async (...args) => client?.alias(...args),
reset: async (...args) => client?.reset(...args),
clearFlushQueue: async () => client?.clearFlushQueue(),
getFlushQueueCount: async () => client?.getFlushQueueCount(),
};
}, [client]);
};
9 changes: 9 additions & 0 deletions packages/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export class Plugin {
shutdown() {
// do nothing by default, user can override.
}

async clearFlushQueue() {
// Overridden in Segment Destination
}

async getQueueCount() {
// Overridden in Segment Destination
return 0;
}
}

export class EventPlugin extends Plugin {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/plugins/QueueFlushingPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,22 @@
return { events: filteredEvents };
});
}

/**
* Clear all events from the queue
*/
async clearQueue() {
await this.queueStore?.dispatch(() => {
return { events: [] };
});
}

/**

Check failure on line 127 in packages/core/src/plugins/QueueFlushingPlugin.ts

View workflow job for this annotation

GitHub Actions / build-and-test

Unexpected nullable number value in conditional. Please handle the nullish/zero/NaN cases explicitly
* Returns the count of items in the queue
*/
async getQueueCount() {
const state = await this.queueStore?.getState();
const eventsCount = state?.events.length || 0;
return eventsCount;
}
}
9 changes: 9 additions & 0 deletions packages/core/src/plugins/SegmentDestination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,13 @@ export class SegmentDestination extends DestinationPlugin {
// Wait until the queue is done restoring before flushing
return this.queuePlugin.flush();
}
async clearFlushQueue() {
//Wait until clearing current Flush queue
return this.queuePlugin.clearQueue();
}
async getQueueCount() {
// Wait until getting the count of queue
const eventsCount = await this.queuePlugin.getQueueCount();
return eventsCount;
}
}
71 changes: 71 additions & 0 deletions packages/core/src/plugins/__tests__/QueueFlushingPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,75 @@ describe('QueueFlushingPlugin', () => {
// @ts-ignore
expect(queuePlugin.queueStore?.getState().events).toHaveLength(0);
});

it('should clear all events from the queue', async () => {
const onFlush = jest.fn().mockResolvedValue(undefined);
const queuePlugin = setupQueuePlugin(onFlush, 10);

const event1: SegmentEvent = {
type: EventType.TrackEvent,
event: 'test1',
properties: {
test: 'test1',
},
};

const event2: SegmentEvent = {
type: EventType.TrackEvent,
event: 'test2',
properties: {
test: 'test2',
},
};

await queuePlugin.execute(event1);
await queuePlugin.execute(event2);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(queuePlugin.queueStore?.getState().events).toHaveLength(2);

await queuePlugin.clearQueue();

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(queuePlugin.queueStore?.getState().events).toHaveLength(0);
});

it('should return the count of items in the queue', async () => {
const onFlush = jest.fn().mockResolvedValue(undefined);
const queuePlugin = setupQueuePlugin(onFlush, 10);

const event1: SegmentEvent = {
type: EventType.TrackEvent,
event: 'test1',
properties: {
test: 'test1',
},
};

const event2: SegmentEvent = {
type: EventType.TrackEvent,
event: 'test2',
properties: {
test: 'test2',
},
};

await queuePlugin.execute(event1);
await queuePlugin.execute(event2);

let eventsCount = await queuePlugin.getQueueCount();
expect(eventsCount).toBe(2);

await queuePlugin.dequeue(event1);

eventsCount = await queuePlugin.getQueueCount();
expect(eventsCount).toBe(1);

await queuePlugin.clearQueue();

eventsCount = await queuePlugin.getQueueCount();
expect(eventsCount).toBe(0);
});
});
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ export type ClientMethods = {
group: (groupId: string, groupTraits?: GroupTraits) => Promise<void>;
alias: (newUserId: string) => Promise<void>;
reset: (resetAnonymousId?: boolean) => Promise<void>;
clearFlushQueue: () => Promise<void>;
getFlushQueueCount: () => Promise<number | undefined>;
};

type ContextApp = {
Expand Down
Loading