File tree Expand file tree Collapse file tree 1 file changed +19
-7
lines changed Expand file tree Collapse file tree 1 file changed +19
-7
lines changed Original file line number Diff line number Diff line change 270270 // Keep track of number of events to delete from queue
271271 var numberToSend = chooseHowManyToExecute ( outQueue ) ;
272272
273+ // The events (`numberToSend` of them), have been sent, so we remove them from the outQueue
274+ // We also call executeQueue() again, to let executeQueue() check if we should keep running through the queue
275+ function onPostSuccess ( numberToSend ) {
276+ for ( var deleteCount = 0 ; deleteCount < numberToSend ; deleteCount ++ ) {
277+ outQueue . shift ( ) ;
278+ }
279+ if ( useLocalStorage ) {
280+ helpers . attemptWriteLocalStorage ( queueName , JSON . stringify ( outQueue ) ) ;
281+ }
282+ executeQueue ( ) ;
283+ }
284+
273285 xhr . onreadystatechange = function ( ) {
274286 if ( xhr . readyState === 4 && xhr . status >= 200 && xhr . status < 400 ) {
275- for ( var deleteCount = 0 ; deleteCount < numberToSend ; deleteCount ++ ) {
276- outQueue . shift ( ) ;
277- }
278- if ( useLocalStorage ) {
279- helpers . attemptWriteLocalStorage ( queueName , JSON . stringify ( outQueue ) ) ;
280- }
281287 clearTimeout ( xhrTimeout ) ;
282- executeQueue ( ) ;
288+ onPostSuccess ( numberToSend ) ;
283289 } else if ( xhr . readyState === 4 && xhr . status >= 400 ) {
284290 clearTimeout ( xhrTimeout ) ;
285291 executingQueue = false ;
304310 }
305311
306312 }
313+ // When beaconStatus is true, we can't _guarantee_ that it was successful (beacon queues asynchronously)
314+ // but the browser has taken it out of our hands, so we want to flush the queue assuming it will do its job
315+ if ( beaconStatus === true ) {
316+ onPostSuccess ( numberToSend ) ;
317+ }
318+
307319 if ( ! useBeacon || ! beaconStatus ) {
308320 xhr . send ( encloseInPayloadDataEnvelope ( attachStmToEvent ( batch ) ) ) ;
309321 }
You can’t perform that action at this time.
0 commit comments