Hopefully chase out a race in sframe-channel

This commit is contained in:
Caleb James DeLisle 2017-09-13 11:00:00 +02:00
parent 1713167a5d
commit e3c0ec29f7
1 changed files with 17 additions and 13 deletions

View File

@ -1,7 +1,8 @@
// This file provides the API for the channel for talking to and from the sandbox iframe. // This file provides the API for the channel for talking to and from the sandbox iframe.
define([ define([
'/common/sframe-protocol.js' '/common/sframe-protocol.js',
], function (SFrameProtocol) { '/common/common-util.js'
], function (SFrameProtocol, Util) {
var mkTxid = function () { var mkTxid = function () {
return Math.random().toString(16).replace('0.', '') + Math.random().toString(16).replace('0.', ''); return Math.random().toString(16).replace('0.', '') + Math.random().toString(16).replace('0.', '');
@ -9,6 +10,7 @@ define([
var create = function (ow, cb, isSandbox) { var create = function (ow, cb, isSandbox) {
var otherWindow; var otherWindow;
var evReady = Util.mkEvent(true);
var handlers = {}; var handlers = {};
var queries = {}; var queries = {};
@ -34,11 +36,13 @@ define([
delete queries[txid]; delete queries[txid];
cb(undefined, data.content, msg); cb(undefined, data.content, msg);
}; };
otherWindow.postMessage(JSON.stringify({ evReady.reg(function () {
txid: txid, otherWindow.postMessage(JSON.stringify({
content: content, txid: txid,
q: q content: content,
}), '*'); q: q
}), '*');
});
}; };
// Fire an event. channel.event('EV_SOMETHING', { args: "whatever" }); // Fire an event. channel.event('EV_SOMETHING', { args: "whatever" });
@ -50,7 +54,9 @@ define([
if (e.indexOf('EV_') !== 0) { if (e.indexOf('EV_') !== 0) {
throw new Error('please only use events (starting with EV_) for event messages'); throw new Error('please only use events (starting with EV_) for event messages');
} }
otherWindow.postMessage(JSON.stringify({ content: content, q: e }), '*'); evReady.reg(function () {
otherWindow.postMessage(JSON.stringify({ content: content, q: e }), '*');
});
}; };
// Be notified on query or event. channel.on('EV_SOMETHING', function (args, reply) { ... }); // Be notified on query or event. channel.on('EV_SOMETHING', function (args, reply) { ... });
@ -106,18 +112,14 @@ define([
}, true); }, true);
// Make sure both iframes are ready // Make sure both iframes are ready
var readyHandlers = [];
chan.onReady = function (h) { chan.onReady = function (h) {
if (typeof(h) !== "function") { return; } if (typeof(h) !== "function") { return; }
readyHandlers.push(h); chan.on('EV_RPC_READY', function () { h(); });
}; };
chan.ready = function () { chan.ready = function () {
chan.whenReg('EV_RPC_READY', function () { chan.whenReg('EV_RPC_READY', function () {
chan.event('EV_RPC_READY'); chan.event('EV_RPC_READY');
}); });
chan.on('EV_RPC_READY', function () {
readyHandlers.forEach(function (h) { h(); });
});
}; };
var txid; var txid;
@ -129,6 +131,7 @@ define([
//console.log(msg); //console.log(msg);
} else if (!otherWindow) { } else if (!otherWindow) {
otherWindow = ow; otherWindow = ow;
evReady.fire();
ow.postMessage(JSON.stringify({ txid: data.txid }), '*'); ow.postMessage(JSON.stringify({ txid: data.txid }), '*');
cb(chan); cb(chan);
} else if (typeof(data.q) === 'string' && handlers[data.q]) { } else if (typeof(data.q) === 'string' && handlers[data.q]) {
@ -149,6 +152,7 @@ define([
if (isSandbox) { if (isSandbox) {
// we're in the sandbox // we're in the sandbox
otherWindow = ow; otherWindow = ow;
evReady.fire();
cb(chan); cb(chan);
} }
}; };