@@ -18,6 +18,7 @@ export function createDeflateEncoder(
1818 let compressedData : Uint8ArrayBuffer [ ] = [ ]
1919 let compressedDataTrailer : Uint8ArrayBuffer
2020
21+ let isEmpty = true
2122 let nextWriteActionId = 0
2223 const pendingWriteActions : Array < {
2324 writeCallback ?: ( additionalEncodedBytesCount : number ) => void
@@ -35,20 +36,24 @@ export function createDeflateEncoder(
3536 return
3637 }
3738
38- rawBytesCount += workerResponse . additionalBytesCount
39- compressedData . push ( workerResponse . result )
40- compressedDataTrailer = workerResponse . trailer
41-
42- const nextPendingAction = pendingWriteActions . shift ( )
43- if ( nextPendingAction && nextPendingAction . id === workerResponse . id ) {
44- if ( nextPendingAction . writeCallback ) {
45- nextPendingAction . writeCallback ( workerResponse . result . byteLength )
46- } else if ( nextPendingAction . finishCallback ) {
47- nextPendingAction . finishCallback ( )
39+ const nextPendingAction = pendingWriteActions [ 0 ]
40+ if ( nextPendingAction ) {
41+ if ( nextPendingAction . id === workerResponse . id ) {
42+ pendingWriteActions . shift ( )
43+
44+ rawBytesCount += workerResponse . additionalBytesCount
45+ compressedData . push ( workerResponse . result )
46+ compressedDataTrailer = workerResponse . trailer
47+
48+ if ( nextPendingAction . writeCallback ) {
49+ nextPendingAction . writeCallback ( workerResponse . result . byteLength )
50+ } else if ( nextPendingAction . finishCallback ) {
51+ nextPendingAction . finishCallback ( )
52+ }
53+ } else if ( nextPendingAction . id < workerResponse . id ) {
54+ removeMessageListener ( )
55+ addTelemetryDebug ( 'Worker responses received out of order.' )
4856 }
49- } else {
50- removeMessageListener ( )
51- addTelemetryDebug ( 'Worker responses received out of order.' )
5257 }
5358 }
5459 )
@@ -68,20 +73,20 @@ export function createDeflateEncoder(
6873 }
6974
7075 function sendResetIfNeeded ( ) {
71- if ( nextWriteActionId > 0 ) {
76+ if ( ! isEmpty ) {
7277 worker . postMessage ( {
7378 action : 'reset' ,
7479 streamId,
7580 } )
76- nextWriteActionId = 0
81+ isEmpty = true
7782 }
7883 }
7984
8085 return {
8186 isAsync : true ,
8287
8388 get isEmpty ( ) {
84- return nextWriteActionId === 0
89+ return isEmpty
8590 } ,
8691
8792 write ( data , callback ) {
@@ -96,6 +101,7 @@ export function createDeflateEncoder(
96101 writeCallback : callback ,
97102 data,
98103 } )
104+ isEmpty = false
99105 nextWriteActionId += 1
100106 } ,
101107
@@ -117,16 +123,9 @@ export function createDeflateEncoder(
117123
118124 finishSync ( ) {
119125 sendResetIfNeeded ( )
120-
121- const pendingData = pendingWriteActions
122- . map ( ( pendingWriteAction ) => {
123- // Make sure we do not call any write or finish callback
124- delete pendingWriteAction . writeCallback
125- delete pendingWriteAction . finishCallback
126- return pendingWriteAction . data
127- } )
128- . join ( '' )
129-
126+ const pendingData = pendingWriteActions . map ( ( pendingWriteAction ) => pendingWriteAction . data ) . join ( '' )
127+ // Ignore all pending write actions responses from the worker
128+ pendingWriteActions . length = 0
130129 return { ...consumeResult ( ) , pendingData }
131130 } ,
132131
0 commit comments