Skip to content

Commit

Permalink
Fix #10
Browse files Browse the repository at this point in the history
Postpone client messages until document is visible
  • Loading branch information
el1t committed Dec 20, 2016
1 parent 023818f commit 5c852b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
8 changes: 4 additions & 4 deletions platform/safari/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@

CallbackWrapper.prototype.init = function(port, request, timeout) {
this.port = port;
this.request = request;
this.request = request || port;
this.timerId = timeout !== undefined ?
vAPI.setTimeout(this.callback, timeout) :
null;
Expand All @@ -841,7 +841,7 @@
delete toAuxPending[this.timerId];
this.timerId = null;
}
this.port.dispatchMessage(this.request.name, {
this.port.target.page.dispatchMessage(this.request.name, {
auxProcessId: this.request.message.auxProcessId,
channelName: this.request.message.channelName,
msg: response !== undefined ? response : null
Expand Down Expand Up @@ -931,7 +931,7 @@
// Auxiliary process to auxiliary process
if ( message.toTabId !== undefined ) {
// TODO: this doesn't work.
toAux(message, request.target.page);
toAux(message, request);
return;
}

Expand All @@ -944,7 +944,7 @@
// Auxiliary process to main process: prepare response
var callback = messaging.NOOPFUNC;
if ( message.auxProcessId !== undefined ) {
callback = callbackWrapperFactory(request.target.page, request).callback;
callback = callbackWrapperFactory(request).callback;
}

var sender = {
Expand Down
24 changes: 21 additions & 3 deletions platform/safari/vapi-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,11 @@
vAPI.messaging = {
channels: Object.create(null),
channelCount: 0,
// Waiting for response
pending: Object.create(null),
pendingCount: 0,
// Waiting to send
queued: [],
auxProcessId: 1,
shuttingDown: false,

Expand Down Expand Up @@ -414,6 +417,16 @@
Function(msg.details.code).call(self);
}
});
// Handle queued messages when page becomes visible
document.addEventListener('visibilitychange', function() {
if (document.hidden === true) return;
var message;
for (var i = 0; i < this.queued.length; i++) {
message = this.queued[i];
this.postMessage(message.auxProcessId, message);
}
this.queued.length = 0;
}.bind(this));
},

send: function(channelName, message, callback) {
Expand All @@ -437,14 +450,19 @@
if (!this.connector) {
this.connect();
}
this.postMessage(auxProcessId, {
message = {
channelName: channelName,
auxProcessId: auxProcessId,
toTabId: toTabId,
toChannel: toChannel,
msg: message
});

};
if (document.hidden === true) {
// Wait for visibility change
this.queued.push(message);
} else {
this.postMessage(auxProcessId, message);
}
},

postMessage: function(auxProcessId, message) {
Expand Down

0 comments on commit 5c852b1

Please sign in to comment.