File tree Expand file tree Collapse file tree 2 files changed +25
-7
lines changed Expand file tree Collapse file tree 2 files changed +25
-7
lines changed Original file line number Diff line number Diff line change 828
828
829
829
CallbackWrapper . prototype . init = function ( port , request , timeout ) {
830
830
this . port = port ;
831
- this . request = request ;
831
+ this . request = request || port ;
832
832
this . timerId = timeout !== undefined ?
833
833
vAPI . setTimeout ( this . callback , timeout ) :
834
834
null ;
841
841
delete toAuxPending [ this . timerId ] ;
842
842
this . timerId = null ;
843
843
}
844
- this . port . dispatchMessage ( this . request . name , {
844
+ this . port . target . page . dispatchMessage ( this . request . name , {
845
845
auxProcessId : this . request . message . auxProcessId ,
846
846
channelName : this . request . message . channelName ,
847
847
msg : response !== undefined ? response : null
931
931
// Auxiliary process to auxiliary process
932
932
if ( message . toTabId !== undefined ) {
933
933
// TODO: this doesn't work.
934
- toAux ( message , request . target . page ) ;
934
+ toAux ( message , request ) ;
935
935
return ;
936
936
}
937
937
944
944
// Auxiliary process to main process: prepare response
945
945
var callback = messaging . NOOPFUNC ;
946
946
if ( message . auxProcessId !== undefined ) {
947
- callback = callbackWrapperFactory ( request . target . page , request ) . callback ;
947
+ callback = callbackWrapperFactory ( request ) . callback ;
948
948
}
949
949
950
950
var sender = {
Original file line number Diff line number Diff line change 319
319
vAPI . messaging = {
320
320
channels : Object . create ( null ) ,
321
321
channelCount : 0 ,
322
+ // Waiting for response
322
323
pending : Object . create ( null ) ,
323
324
pendingCount : 0 ,
325
+ // Waiting to send
326
+ queued : [ ] ,
324
327
auxProcessId : 1 ,
325
328
shuttingDown : false ,
326
329
414
417
Function ( msg . details . code ) . call ( self ) ;
415
418
}
416
419
} ) ;
420
+ // Handle queued messages when page becomes visible
421
+ document . addEventListener ( 'visibilitychange' , function ( ) {
422
+ if ( document . hidden === true ) return ;
423
+ var message ;
424
+ for ( var i = 0 ; i < this . queued . length ; i ++ ) {
425
+ message = this . queued [ i ] ;
426
+ this . postMessage ( message . auxProcessId , message ) ;
427
+ }
428
+ this . queued . length = 0 ;
429
+ } . bind ( this ) ) ;
417
430
} ,
418
431
419
432
send : function ( channelName , message , callback ) {
437
450
if ( ! this . connector ) {
438
451
this . connect ( ) ;
439
452
}
440
- this . postMessage ( auxProcessId , {
453
+ message = {
441
454
channelName : channelName ,
442
455
auxProcessId : auxProcessId ,
443
456
toTabId : toTabId ,
444
457
toChannel : toChannel ,
445
458
msg : message
446
- } ) ;
447
-
459
+ } ;
460
+ if ( document . hidden === true ) {
461
+ // Wait for visibility change
462
+ this . queued . push ( message ) ;
463
+ } else {
464
+ this . postMessage ( auxProcessId , message ) ;
465
+ }
448
466
} ,
449
467
450
468
postMessage : function ( auxProcessId , message ) {
You can’t perform that action at this time.
0 commit comments