@@ -11,7 +11,7 @@ interface CreateEventBufferParams {
1111 useCompression : boolean ;
1212}
1313
14- export function createEventBuffer ( { useCompression } : CreateEventBufferParams ) : IEventBuffer {
14+ export function createEventBuffer ( { useCompression } : CreateEventBufferParams ) : EventBuffer {
1515 // eslint-disable-next-line no-restricted-globals
1616 if ( useCompression && window . Worker ) {
1717 const workerBlob = new Blob ( [ workerString ] ) ;
@@ -35,29 +35,29 @@ export function createEventBuffer({ useCompression }: CreateEventBufferParams):
3535 return new EventBufferArray ( ) ;
3636}
3737
38- export interface IEventBuffer {
38+ export interface EventBuffer {
3939 readonly length : number ;
4040 destroy ( ) : void ;
4141 addEvent ( event : RecordingEvent , isCheckout ?: boolean ) : void ;
4242 finish ( ) : Promise < string | Uint8Array > ;
4343}
4444
45- class EventBufferArray implements IEventBuffer {
46- events : RecordingEvent [ ] ;
45+ class EventBufferArray implements EventBuffer {
46+ private events : RecordingEvent [ ] ;
4747
48- constructor ( ) {
48+ public constructor ( ) {
4949 this . events = [ ] ;
5050 }
5151
52- destroy ( ) : void {
52+ public destroy ( ) : void {
5353 this . events = [ ] ;
5454 }
5555
56- get length ( ) : number {
56+ public get length ( ) : number {
5757 return this . events . length ;
5858 }
5959
60- addEvent ( event : RecordingEvent , isCheckout ?: boolean ) : void {
60+ public addEvent ( event : RecordingEvent , isCheckout ?: boolean ) : void {
6161 if ( isCheckout ) {
6262 this . events = [ event ] ;
6363 return ;
@@ -66,7 +66,7 @@ class EventBufferArray implements IEventBuffer {
6666 this . events . push ( event ) ;
6767 }
6868
69- finish ( ) : Promise < string > {
69+ public finish ( ) : Promise < string > {
7070 return new Promise < string > ( resolve => {
7171 // Make a copy of the events array reference and immediately clear the
7272 // events member so that we do not lose new events while uploading
@@ -79,26 +79,51 @@ class EventBufferArray implements IEventBuffer {
7979}
8080
8181// exporting for testing
82- export class EventBufferCompressionWorker implements IEventBuffer {
82+ export class EventBufferCompressionWorker implements EventBuffer {
8383 private worker : null | Worker ;
8484 private eventBufferItemLength : number = 0 ;
85- private _id : number = 0 ;
85+ private id : number = 0 ;
8686
87- constructor ( worker : Worker ) {
87+ public constructor ( worker : Worker ) {
8888 this . worker = worker ;
8989 }
9090
91+ public destroy ( ) : void {
92+ __DEBUG_BUILD__ && logger . log ( '[Replay] Destroying compression worker' ) ;
93+ this . worker ?. terminate ( ) ;
94+ this . worker = null ;
95+ }
96+
9197 /**
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.
93100 */
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 ( ) ) ;
96121 }
97122
98123 /**
99124 * Post message to worker and wait for response before resolving promise.
100125 */
101- postMessage ( { id, method, args } : WorkerRequest ) : Promise < WorkerResponse [ 'response' ] > {
126+ private _postMessage ( { id, method, args } : WorkerRequest ) : Promise < WorkerResponse [ 'response' ] > {
102127 return new Promise ( ( resolve , reject ) => {
103128 // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
104129 const listener = ( { data } : MessageEvent ) => {
@@ -141,42 +166,9 @@ export class EventBufferCompressionWorker implements IEventBuffer {
141166 } ) ;
142167 }
143168
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 ( ) ,
180172 method : 'addEvent' ,
181173 args : [ event ] ,
182174 } ) ;
@@ -185,18 +177,18 @@ export class EventBufferCompressionWorker implements IEventBuffer {
185177 this . eventBufferItemLength ++ ;
186178
187179 return promise ;
188- } ;
180+ }
189181
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 : [ ] } ) ;
192184
193185 // XXX: See note in `get length()`
194186 this . eventBufferItemLength = 0 ;
195187
196188 return promise as Promise < Uint8Array > ;
197- } ;
189+ }
198190
199- finish ( ) : Promise < Uint8Array > {
200- return this . finishRequest ( this . id ) ;
191+ private _getAndIncrementId ( ) : number {
192+ return this . id ++ ;
201193 }
202194}
0 commit comments