-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathImpressionsCacheInMemory.ts
More file actions
59 lines (50 loc) · 1.4 KB
/
ImpressionsCacheInMemory.ts
File metadata and controls
59 lines (50 loc) · 1.4 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
import { IImpressionsCacheSync } from '../types';
import SplitIO from '../../../types/splitio';
export class ImpressionsCacheInMemory implements IImpressionsCacheSync {
public name = 'impressions';
private onFullQueue?: () => void;
private readonly maxQueue: number;
private queue: SplitIO.ImpressionDTO[];
/**
*
* @param impressionsQueueSize - number of queued impressions to call onFullQueueCb.
* Default value is 0, that means no maximum value, in case we want to avoid this being triggered.
*/
constructor(impressionsQueueSize: number = 0) {
this.maxQueue = impressionsQueueSize;
this.queue = [];
}
setOnFullQueueCb(cb: () => void) {
this.onFullQueue = cb;
}
/**
* Store impressions in sequential order
*/
track(data: SplitIO.ImpressionDTO[]) {
this.queue.push(...data);
// Check if the cache queue is full and we need to flush it.
if (this.maxQueue > 0 && this.queue.length >= this.maxQueue && this.onFullQueue) {
this.onFullQueue();
}
}
/**
* Clear the data stored on the cache.
*/
clear() {
this.queue = [];
}
/**
* Pop the collected data, used as payload for posting.
*/
pop(toMerge?: SplitIO.ImpressionDTO[]) {
const data = this.queue;
this.clear();
return toMerge ? toMerge.concat(data) : data;
}
/**
* Check if the cache is empty.
*/
isEmpty() {
return this.queue.length === 0;
}
}