forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeatedEventFilter.ts
More file actions
79 lines (67 loc) · 2.74 KB
/
Copy pathrepeatedEventFilter.ts
File metadata and controls
79 lines (67 loc) · 2.74 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
import { getMediaEventSchema } from './schemata';
import { FilterOutRepeatedEvents, MediaEventType, EventWithContext } from './types';
/**
* This class filters out repeated events that are sent by the media player.
* This applies to seek and volume change events.
*/
export class RepeatedEventFilter {
private aggregateEventsWithOrder: { [schema: string]: boolean } = {};
private eventsToAggregate: { [schema: string]: (() => void)[] } = {};
private flushTimeout?: number;
private flushTimeoutMs: number;
constructor(configuration?: FilterOutRepeatedEvents) {
let allFiltersEnabled = configuration === undefined || configuration === true;
if (allFiltersEnabled || (typeof configuration === 'object' && configuration.seekEvents !== false)) {
this.aggregateEventsWithOrder[getMediaEventSchema(MediaEventType.SeekStart)] = true;
this.aggregateEventsWithOrder[getMediaEventSchema(MediaEventType.SeekEnd)] = false;
}
if (allFiltersEnabled || (typeof configuration === 'object' && configuration.volumeChangeEvents !== false)) {
this.aggregateEventsWithOrder[getMediaEventSchema(MediaEventType.VolumeChange)] = false;
}
this.flushTimeoutMs = (typeof configuration === 'object' ? configuration.flushTimeoutMs : undefined) ?? 5000;
Object.keys(this.aggregateEventsWithOrder).forEach((schema) => {
this.eventsToAggregate[schema] = [];
});
}
trackFilteredEvents(events: EventWithContext[], trackEvent: (event: EventWithContext) => void) {
let startFlushTimeout = false;
events.forEach(({ event, context }) => {
if (this.eventsToAggregate[event.schema] !== undefined) {
startFlushTimeout = true;
this.eventsToAggregate[event.schema].push(() => trackEvent({ event, context }));
} else {
startFlushTimeout = false;
// flush any events waiting
this.flush();
trackEvent({ event, context });
}
});
if (startFlushTimeout && this.flushTimeout === undefined) {
this.setFlushTimeout();
}
}
flush() {
this.clearFlushTimeout();
Object.keys(this.eventsToAggregate).forEach((schema) => {
let eventsToAggregate = this.eventsToAggregate[schema];
if (eventsToAggregate.length > 0) {
if (this.aggregateEventsWithOrder[schema]) {
eventsToAggregate[0]();
} else {
eventsToAggregate[eventsToAggregate.length - 1]();
}
this.eventsToAggregate[schema] = [];
}
});
}
private clearFlushTimeout() {
if (this.flushTimeout !== undefined) {
clearTimeout(this.flushTimeout);
this.flushTimeout = undefined;
}
}
private setFlushTimeout() {
this.clearFlushTimeout();
this.flushTimeout = window.setTimeout(() => this.flush(), this.flushTimeoutMs);
}
}