@@ -11,7 +11,7 @@ interface CreateEventBufferParams {
11
11
useCompression : boolean ;
12
12
}
13
13
14
- export function createEventBuffer ( { useCompression } : CreateEventBufferParams ) : IEventBuffer {
14
+ export function createEventBuffer ( { useCompression } : CreateEventBufferParams ) : EventBuffer {
15
15
// eslint-disable-next-line no-restricted-globals
16
16
if ( useCompression && window . Worker ) {
17
17
const workerBlob = new Blob ( [ workerString ] ) ;
@@ -35,29 +35,29 @@ export function createEventBuffer({ useCompression }: CreateEventBufferParams):
35
35
return new EventBufferArray ( ) ;
36
36
}
37
37
38
- export interface IEventBuffer {
38
+ export interface EventBuffer {
39
39
readonly length : number ;
40
40
destroy ( ) : void ;
41
41
addEvent ( event : RecordingEvent , isCheckout ?: boolean ) : void ;
42
42
finish ( ) : Promise < string | Uint8Array > ;
43
43
}
44
44
45
- class EventBufferArray implements IEventBuffer {
46
- events : RecordingEvent [ ] ;
45
+ class EventBufferArray implements EventBuffer {
46
+ private events : RecordingEvent [ ] ;
47
47
48
- constructor ( ) {
48
+ public constructor ( ) {
49
49
this . events = [ ] ;
50
50
}
51
51
52
- destroy ( ) : void {
52
+ public destroy ( ) : void {
53
53
this . events = [ ] ;
54
54
}
55
55
56
- get length ( ) : number {
56
+ public get length ( ) : number {
57
57
return this . events . length ;
58
58
}
59
59
60
- addEvent ( event : RecordingEvent , isCheckout ?: boolean ) : void {
60
+ public addEvent ( event : RecordingEvent , isCheckout ?: boolean ) : void {
61
61
if ( isCheckout ) {
62
62
this . events = [ event ] ;
63
63
return ;
@@ -66,7 +66,7 @@ class EventBufferArray implements IEventBuffer {
66
66
this . events . push ( event ) ;
67
67
}
68
68
69
- finish ( ) : Promise < string > {
69
+ public finish ( ) : Promise < string > {
70
70
return new Promise < string > ( resolve => {
71
71
// Make a copy of the events array reference and immediately clear the
72
72
// events member so that we do not lose new events while uploading
@@ -79,26 +79,51 @@ class EventBufferArray implements IEventBuffer {
79
79
}
80
80
81
81
// exporting for testing
82
- export class EventBufferCompressionWorker implements IEventBuffer {
82
+ export class EventBufferCompressionWorker implements EventBuffer {
83
83
private worker : null | Worker ;
84
84
private eventBufferItemLength : number = 0 ;
85
- private _id : number = 0 ;
85
+ private id : number = 0 ;
86
86
87
- constructor ( worker : Worker ) {
87
+ public constructor ( worker : Worker ) {
88
88
this . worker = worker ;
89
89
}
90
90
91
+ public destroy ( ) : void {
92
+ __DEBUG_BUILD__ && logger . log ( '[Replay] Destroying compression worker' ) ;
93
+ this . worker ?. terminate ( ) ;
94
+ this . worker = null ;
95
+ }
96
+
91
97
/**
92
- * Read-only incrementing counter
98
+ * Note that this may not reflect what is actually in the event buffer. This
99
+ * is only a local count of the buffer size since `addEvent` is async.
93
100
*/
94
- get id ( ) : number {
95
- return this . _id ++ ;
101
+ public get length ( ) : number {
102
+ return this . eventBufferItemLength ;
103
+ }
104
+
105
+ public async addEvent ( event : RecordingEvent , isCheckout ?: boolean ) : Promise < string | Uint8Array > {
106
+ if ( isCheckout ) {
107
+ // This event is a checkout, make sure worker buffer is cleared before
108
+ // proceeding.
109
+ await this . _postMessage ( {
110
+ id : this . _getAndIncrementId ( ) ,
111
+ method : 'init' ,
112
+ args : [ ] ,
113
+ } ) ;
114
+ }
115
+
116
+ return this . _sendEventToWorker ( event ) ;
117
+ }
118
+
119
+ public finish ( ) : Promise < Uint8Array > {
120
+ return this . _finishRequest ( this . _getAndIncrementId ( ) ) ;
96
121
}
97
122
98
123
/**
99
124
* Post message to worker and wait for response before resolving promise.
100
125
*/
101
- postMessage ( { id, method, args } : WorkerRequest ) : Promise < WorkerResponse [ 'response' ] > {
126
+ private _postMessage ( { id, method, args } : WorkerRequest ) : Promise < WorkerResponse [ 'response' ] > {
102
127
return new Promise ( ( resolve , reject ) => {
103
128
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
104
129
const listener = ( { data } : MessageEvent ) => {
@@ -141,42 +166,9 @@ export class EventBufferCompressionWorker implements IEventBuffer {
141
166
} ) ;
142
167
}
143
168
144
- init ( ) : void {
145
- void this . postMessage ( { id : this . id , method : 'init' , args : [ ] } ) ;
146
- __DEBUG_BUILD__ && logger . log ( '[Replay] Initialized compression worker' ) ;
147
- }
148
-
149
- destroy ( ) : void {
150
- __DEBUG_BUILD__ && logger . log ( '[Replay] Destroying compression worker' ) ;
151
- this . worker ?. terminate ( ) ;
152
- this . worker = null ;
153
- }
154
-
155
- /**
156
- * Note that this may not reflect what is actually in the event buffer. This
157
- * is only a local count of the buffer size since `addEvent` is async.
158
- */
159
- get length ( ) : number {
160
- return this . eventBufferItemLength ;
161
- }
162
-
163
- async addEvent ( event : RecordingEvent , isCheckout ?: boolean ) : Promise < string | Uint8Array > {
164
- if ( isCheckout ) {
165
- // This event is a checkout, make sure worker buffer is cleared before
166
- // proceeding.
167
- await this . postMessage ( {
168
- id : this . id ,
169
- method : 'init' ,
170
- args : [ ] ,
171
- } ) ;
172
- }
173
-
174
- return this . sendEventToWorker ( event ) ;
175
- }
176
-
177
- sendEventToWorker : ( event : RecordingEvent ) => Promise < string | Uint8Array > = ( event : RecordingEvent ) => {
178
- const promise = this . postMessage ( {
179
- id : this . id ,
169
+ private _sendEventToWorker ( event : RecordingEvent ) : Promise < string | Uint8Array > {
170
+ const promise = this . _postMessage ( {
171
+ id : this . _getAndIncrementId ( ) ,
180
172
method : 'addEvent' ,
181
173
args : [ event ] ,
182
174
} ) ;
@@ -185,18 +177,18 @@ export class EventBufferCompressionWorker implements IEventBuffer {
185
177
this . eventBufferItemLength ++ ;
186
178
187
179
return promise ;
188
- } ;
180
+ }
189
181
190
- finishRequest : ( id : number ) => Promise < Uint8Array > = async ( id : number ) = > {
191
- const promise = this . postMessage ( { id, method : 'finish' , args : [ ] } ) ;
182
+ private async _finishRequest ( id : number ) : Promise < Uint8Array > {
183
+ const promise = this . _postMessage ( { id, method : 'finish' , args : [ ] } ) ;
192
184
193
185
// XXX: See note in `get length()`
194
186
this . eventBufferItemLength = 0 ;
195
187
196
188
return promise as Promise < Uint8Array > ;
197
- } ;
189
+ }
198
190
199
- finish ( ) : Promise < Uint8Array > {
200
- return this . finishRequest ( this . id ) ;
191
+ private _getAndIncrementId ( ) : number {
192
+ return this . id ++ ;
201
193
}
202
194
}
0 commit comments