This repository was archived by the owner on Jan 15, 2026. It is now read-only.
forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_store.ts
More file actions
116 lines (108 loc) · 3.01 KB
/
Copy pathevent_store.ts
File metadata and controls
116 lines (108 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { EventStorePayload } from './event_store_payload';
import { Payload } from './payload';
/**
* Result of the next operation on an EventStoreIterator.
*/
export interface EventStoreIteratorNextResult {
/**
* The next event in the store, or undefined if there are no more events.
*/
value: EventStorePayload | undefined;
/**
* True if there are no more events in the store.
*/
done: boolean;
}
/**
* EventStoreIterator allows iterating over all events in the store.
*/
export interface EventStoreIterator {
/**
* Retrieve the next event in the store
*/
next: () => Promise<EventStoreIteratorNextResult>;
}
/**
* EventStore allows storing and retrieving events before they are sent to the collector
*/
export interface EventStore {
/**
* Count all events in the store
*/
count: () => Promise<number>;
/**
* Add an event to the store
* @returns the number of events in the store after adding
*/
add: (payload: EventStorePayload) => Promise<number>;
/**
* Remove the first `count` events from the store
*/
removeHead: (count: number) => Promise<void>;
/**
* Get an iterator over all events in the store
*/
iterator: () => EventStoreIterator;
/**
* Retrieve all payloads including their meta configuration in the store
*/
getAll: () => Promise<readonly EventStorePayload[]>;
/**
* Retrieve all pure payloads in the store
*/
getAllPayloads: () => Promise<readonly Payload[]>;
}
export interface EventStoreConfiguration {
/**
* The maximum amount of events that will be buffered in the event store
*
* This is useful to ensure the Tracker doesn't fill the 5MB or 10MB available to
* each website should the collector be unavailable due to lost connectivity.
* Will drop old events once the limit is hit
*/
maxSize?: number;
}
export interface InMemoryEventStoreConfiguration {
/**
* Initial events to add to the store
*/
events?: EventStorePayload[];
}
export function newInMemoryEventStore({
maxSize = 1000,
events = [],
}: EventStoreConfiguration & InMemoryEventStoreConfiguration): EventStore {
let store: EventStorePayload[] = [...events];
const count = () => Promise.resolve(store.length);
return {
count,
add: (payload: EventStorePayload) => {
store.push(payload);
while (store.length > maxSize) {
store.shift();
}
return count();
},
removeHead: (count: number) => {
for (let i = 0; i < count; i++) {
store.shift();
}
return Promise.resolve();
},
iterator: () => {
let index = 0;
// copy the store to prevent mutation
let events = [...store];
return {
next: () => {
if (index < events.length) {
return Promise.resolve({ value: events[index++], done: false });
}
return Promise.resolve({ value: undefined, done: true });
},
};
},
getAll: () => Promise.resolve([...store]),
getAllPayloads: () => Promise.resolve(store.map((e) => e.payload)),
};
}