|
1 | 1 | /**
|
2 |
| - * Copyright 2022, Optimizely |
| 2 | + * Copyright 2022-2023, Optimizely |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -import { getLogger } from '../logging' |
| 17 | +import { getLogger } from '../logging'; |
18 | 18 | // TODO change this to use Managed from js-sdk-models when available
|
19 |
| -import { Managed } from './managed' |
| 19 | +import { Managed } from './managed'; |
20 | 20 |
|
21 |
| -const logger = getLogger('EventProcessor') |
| 21 | +const logger = getLogger('EventProcessor'); |
22 | 22 |
|
23 |
| -export type EventQueueSink<K> = (buffer: K[]) => Promise<any> |
| 23 | +export type EventQueueSink<K> = (buffer: K[]) => Promise<any>; |
24 | 24 |
|
25 | 25 | export interface EventQueue<K> extends Managed {
|
26 |
| - enqueue(event: K): void |
| 26 | + enqueue(event: K): void; |
27 | 27 | }
|
28 | 28 |
|
29 | 29 | export interface EventQueueFactory<K> {
|
30 |
| - createEventQueue(config: { |
31 |
| - sink: EventQueueSink<K> |
32 |
| - flushInterval: number |
33 |
| - maxQueueSize: number |
34 |
| - }): EventQueue<K> |
| 30 | + createEventQueue(config: { sink: EventQueueSink<K>, flushInterval: number, maxQueueSize: number }): EventQueue<K>; |
35 | 31 | }
|
36 | 32 |
|
37 | 33 | class Timer {
|
38 |
| - private timeout: number |
39 |
| - private callback: () => void |
40 |
| - private timeoutId?: number |
| 34 | + private timeout: number; |
| 35 | + private callback: () => void; |
| 36 | + private timeoutId?: number; |
41 | 37 |
|
42 | 38 | constructor({ timeout, callback }: { timeout: number; callback: () => void }) {
|
43 |
| - this.timeout = Math.max(timeout, 0) |
44 |
| - this.callback = callback |
| 39 | + this.timeout = Math.max(timeout, 0); |
| 40 | + this.callback = callback; |
45 | 41 | }
|
46 | 42 |
|
47 | 43 | start(): void {
|
48 |
| - this.timeoutId = setTimeout(this.callback, this.timeout) as any |
| 44 | + this.timeoutId = setTimeout(this.callback, this.timeout) as any; |
49 | 45 | }
|
50 | 46 |
|
51 | 47 | refresh(): void {
|
52 |
| - this.stop() |
53 |
| - this.start() |
| 48 | + this.stop(); |
| 49 | + this.start(); |
54 | 50 | }
|
55 | 51 |
|
56 | 52 | stop(): void {
|
57 | 53 | if (this.timeoutId) {
|
58 |
| - clearTimeout(this.timeoutId as any) |
| 54 | + clearTimeout(this.timeoutId as any); |
59 | 55 | }
|
60 | 56 | }
|
61 | 57 | }
|
62 | 58 |
|
63 | 59 | export class SingleEventQueue<K> implements EventQueue<K> {
|
64 |
| - private sink: EventQueueSink<K> |
| 60 | + private sink: EventQueueSink<K>; |
65 | 61 |
|
66 | 62 | constructor({ sink }: { sink: EventQueueSink<K> }) {
|
67 |
| - this.sink = sink |
| 63 | + this.sink = sink; |
68 | 64 | }
|
69 | 65 |
|
70 | 66 | start(): Promise<any> {
|
71 | 67 | // no-op
|
72 |
| - return Promise.resolve() |
| 68 | + return Promise.resolve(); |
73 | 69 | }
|
74 | 70 |
|
75 | 71 | stop(): Promise<any> {
|
76 | 72 | // no-op
|
77 |
| - return Promise.resolve() |
| 73 | + return Promise.resolve(); |
78 | 74 | }
|
79 | 75 |
|
80 | 76 | enqueue(event: K): void {
|
81 |
| - this.sink([event]) |
| 77 | + this.sink([event]); |
82 | 78 | }
|
83 | 79 | }
|
84 | 80 |
|
85 | 81 | export class DefaultEventQueue<K> implements EventQueue<K> {
|
86 | 82 | // expose for testing
|
87 |
| - public timer: Timer |
88 |
| - private buffer: K[] |
89 |
| - private maxQueueSize: number |
90 |
| - private sink: EventQueueSink<K> |
| 83 | + public timer: Timer; |
| 84 | + private buffer: K[]; |
| 85 | + private maxQueueSize: number; |
| 86 | + private sink: EventQueueSink<K>; |
| 87 | + private closingSink?: EventQueueSink<K>; |
91 | 88 | // batchComparator is called to determine whether two events can be included
|
92 | 89 | // together in the same batch
|
93 |
| - private batchComparator: (eventA: K, eventB: K) => boolean |
94 |
| - private started: boolean |
| 90 | + private batchComparator: (eventA: K, eventB: K) => boolean; |
| 91 | + private started: boolean; |
95 | 92 |
|
96 | 93 | constructor({
|
97 | 94 | flushInterval,
|
98 | 95 | maxQueueSize,
|
99 | 96 | sink,
|
| 97 | + closingSink, |
100 | 98 | batchComparator,
|
101 | 99 | }: {
|
102 |
| - flushInterval: number |
103 |
| - maxQueueSize: number |
104 |
| - sink: EventQueueSink<K> |
105 |
| - batchComparator: (eventA: K, eventB: K) => boolean |
| 100 | + flushInterval: number; |
| 101 | + maxQueueSize: number; |
| 102 | + sink: EventQueueSink<K>; |
| 103 | + closingSink?: EventQueueSink<K>; |
| 104 | + batchComparator: (eventA: K, eventB: K) => boolean; |
106 | 105 | }) {
|
107 |
| - this.buffer = [] |
108 |
| - this.maxQueueSize = Math.max(maxQueueSize, 1) |
109 |
| - this.sink = sink |
110 |
| - this.batchComparator = batchComparator |
| 106 | + this.buffer = []; |
| 107 | + this.maxQueueSize = Math.max(maxQueueSize, 1); |
| 108 | + this.sink = sink; |
| 109 | + this.closingSink = closingSink; |
| 110 | + this.batchComparator = batchComparator; |
111 | 111 | this.timer = new Timer({
|
112 | 112 | callback: this.flush.bind(this),
|
113 | 113 | timeout: flushInterval,
|
114 |
| - }) |
115 |
| - this.started = false |
| 114 | + }); |
| 115 | + this.started = false; |
116 | 116 | }
|
117 | 117 |
|
118 | 118 | start(): Promise<any> {
|
119 |
| - this.started = true |
| 119 | + this.started = true; |
120 | 120 | // dont start the timer until the first event is enqueued
|
121 | 121 |
|
122 | 122 | return Promise.resolve();
|
123 | 123 | }
|
124 | 124 |
|
125 | 125 | stop(): Promise<any> {
|
126 |
| - this.started = false |
127 |
| - const result = this.sink(this.buffer) |
128 |
| - this.buffer = [] |
129 |
| - this.timer.stop() |
130 |
| - return result |
| 126 | + this.started = false; |
| 127 | + const result = this.closingSink ? this.closingSink(this.buffer) : this.sink(this.buffer); |
| 128 | + this.buffer = []; |
| 129 | + this.timer.stop(); |
| 130 | + return result; |
131 | 131 | }
|
132 | 132 |
|
133 | 133 | enqueue(event: K): void {
|
134 | 134 | if (!this.started) {
|
135 |
| - logger.warn('Queue is stopped, not accepting event') |
136 |
| - return |
| 135 | + logger.warn('Queue is stopped, not accepting event'); |
| 136 | + return; |
137 | 137 | }
|
138 | 138 |
|
139 | 139 | // If new event cannot be included into the current batch, flush so it can
|
140 | 140 | // be in its own new batch.
|
141 |
| - const bufferedEvent: K | undefined = this.buffer[0] |
| 141 | + const bufferedEvent: K | undefined = this.buffer[0]; |
142 | 142 | if (bufferedEvent && !this.batchComparator(bufferedEvent, event)) {
|
143 |
| - this.flush() |
| 143 | + this.flush(); |
144 | 144 | }
|
145 | 145 |
|
146 | 146 | // start the timer when the first event is put in
|
147 | 147 | if (this.buffer.length === 0) {
|
148 |
| - this.timer.refresh() |
| 148 | + this.timer.refresh(); |
149 | 149 | }
|
150 |
| - this.buffer.push(event) |
| 150 | + this.buffer.push(event); |
151 | 151 |
|
152 | 152 | if (this.buffer.length >= this.maxQueueSize) {
|
153 |
| - this.flush() |
| 153 | + this.flush(); |
154 | 154 | }
|
155 | 155 | }
|
156 | 156 |
|
157 |
| - flush() : void { |
158 |
| - this.sink(this.buffer) |
159 |
| - this.buffer = [] |
160 |
| - this.timer.stop() |
| 157 | + flush(): void { |
| 158 | + this.sink(this.buffer); |
| 159 | + this.buffer = []; |
| 160 | + this.timer.stop(); |
161 | 161 | }
|
162 | 162 | }
|
0 commit comments