|
|
@ -8,22 +8,30 @@ define([
|
|
|
|
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.', '');
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var create = function (onMsg, postMsg, cb, isWorker) {
|
|
|
|
var create = function (onMsg, postMsg, cb) {
|
|
|
|
var chanLoaded;
|
|
|
|
var chanLoaded;
|
|
|
|
var waitingData;
|
|
|
|
var waitingData = [];
|
|
|
|
if (!isWorker) {
|
|
|
|
|
|
|
|
chanLoaded = false;
|
|
|
|
|
|
|
|
waitingData = [];
|
|
|
|
|
|
|
|
onMsg.reg(function (data) {
|
|
|
|
|
|
|
|
if (chanLoaded) { return; }
|
|
|
|
|
|
|
|
waitingData.push(data);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var evReady = Util.mkEvent(true);
|
|
|
|
var evReady = Util.mkEvent(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onMsg.reg(function (msg) {
|
|
|
|
|
|
|
|
if (chanLoaded) { return; }
|
|
|
|
|
|
|
|
var data = msg.data;
|
|
|
|
|
|
|
|
if (data === '_READY') {
|
|
|
|
|
|
|
|
postMsg('_READY');
|
|
|
|
|
|
|
|
chanLoaded = true;
|
|
|
|
|
|
|
|
evReady.fire();
|
|
|
|
|
|
|
|
waitingData.forEach(function (d) {
|
|
|
|
|
|
|
|
onMsg.fire(d);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
waitingData.push(data);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var handlers = {};
|
|
|
|
var handlers = {};
|
|
|
|
var queries = {};
|
|
|
|
var queries = {};
|
|
|
|
|
|
|
|
var acks = {};
|
|
|
|
|
|
|
|
|
|
|
|
// list of handlers which are registered from the other side...
|
|
|
|
// list of handlers which are registered from the other side...
|
|
|
|
var insideHandlers = [];
|
|
|
|
var insideHandlers = [];
|
|
|
@ -32,16 +40,24 @@ define([
|
|
|
|
var chan = {};
|
|
|
|
var chan = {};
|
|
|
|
|
|
|
|
|
|
|
|
// Send a query. channel.query('Q_SOMETHING', { args: "whatever" }, function (reply) { ... });
|
|
|
|
// Send a query. channel.query('Q_SOMETHING', { args: "whatever" }, function (reply) { ... });
|
|
|
|
|
|
|
|
// We have a timeout for receiving an ACK, but unlimited time for receiving an answer to the query
|
|
|
|
chan.query = function (q, content, cb, opts) {
|
|
|
|
chan.query = function (q, content, cb, opts) {
|
|
|
|
var txid = mkTxid();
|
|
|
|
var txid = mkTxid();
|
|
|
|
opts = opts || {};
|
|
|
|
opts = opts || {};
|
|
|
|
var to = opts.timeout || 30000;
|
|
|
|
var to = opts.timeout || 30000;
|
|
|
|
var timeout = setTimeout(function () {
|
|
|
|
var timeout = setTimeout(function () {
|
|
|
|
delete queries[txid];
|
|
|
|
delete queries[txid];
|
|
|
|
//console.log("Timeout making query " + q);
|
|
|
|
cb('TIMEOUT');
|
|
|
|
}, to);
|
|
|
|
}, to);
|
|
|
|
queries[txid] = function (data, msg) {
|
|
|
|
acks[txid] = function (err) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
|
|
|
delete acks[txid];
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
delete queries[txid];
|
|
|
|
|
|
|
|
cb('UNHANDLED');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
queries[txid] = function (data, msg) {
|
|
|
|
delete queries[txid];
|
|
|
|
delete queries[txid];
|
|
|
|
cb(undefined, data.content, msg);
|
|
|
|
cb(undefined, data.content, msg);
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -103,10 +119,10 @@ define([
|
|
|
|
}
|
|
|
|
}
|
|
|
|
insideHandlers.push(content);
|
|
|
|
insideHandlers.push(content);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
chan.whenReg('EV_REGISTER_HANDLER', evReady.fire);
|
|
|
|
//chan.whenReg('EV_REGISTER_HANDLER', evReady.fire);
|
|
|
|
|
|
|
|
|
|
|
|
// Make sure both iframes are ready
|
|
|
|
// Make sure both iframes are ready
|
|
|
|
var isReady =false;
|
|
|
|
var isReady = false;
|
|
|
|
chan.onReady = function (h) {
|
|
|
|
chan.onReady = function (h) {
|
|
|
|
if (isReady) {
|
|
|
|
if (isReady) {
|
|
|
|
return void h();
|
|
|
|
return void h();
|
|
|
@ -121,29 +137,43 @@ define([
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
onMsg.reg(function (msg) {
|
|
|
|
onMsg.reg(function (msg) {
|
|
|
|
|
|
|
|
if (!chanLoaded) { return; }
|
|
|
|
|
|
|
|
if (!msg.data || msg.data === '_READY') { return; }
|
|
|
|
var data = JSON.parse(msg.data);
|
|
|
|
var data = JSON.parse(msg.data);
|
|
|
|
if (typeof(data.q) === 'string' && handlers[data.q]) {
|
|
|
|
if (typeof(data.ack) !== "undefined") {
|
|
|
|
handlers[data.q].forEach(function (f) {
|
|
|
|
if (acks[data.txid]) { acks[data.txid](!data.ack); }
|
|
|
|
f(data || JSON.parse(msg.data), msg);
|
|
|
|
} else if (typeof(data.q) === 'string') {
|
|
|
|
data = undefined;
|
|
|
|
if (handlers[data.q]) {
|
|
|
|
});
|
|
|
|
// If this is a "query", send an ack
|
|
|
|
|
|
|
|
if (data.txid) {
|
|
|
|
|
|
|
|
postMsg(JSON.stringify({
|
|
|
|
|
|
|
|
txid: data.txid,
|
|
|
|
|
|
|
|
ack: true
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers[data.q].forEach(function (f) {
|
|
|
|
|
|
|
|
f(data || JSON.parse(msg.data), msg);
|
|
|
|
|
|
|
|
data = undefined;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (data.txid) {
|
|
|
|
|
|
|
|
postMsg(JSON.stringify({
|
|
|
|
|
|
|
|
txid: data.txid,
|
|
|
|
|
|
|
|
ack: false
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} else if (typeof(data.q) === 'undefined' && queries[data.txid]) {
|
|
|
|
} else if (typeof(data.q) === 'undefined' && queries[data.txid]) {
|
|
|
|
queries[data.txid](data, msg);
|
|
|
|
queries[data.txid](data, msg);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
console.log("DROP Unhandled message");
|
|
|
|
/*console.log("DROP Unhandled message");
|
|
|
|
console.log(msg.data, isWorker);
|
|
|
|
console.log(msg.data, window);
|
|
|
|
console.log(msg);
|
|
|
|
console.log(msg);*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
if (isWorker) {
|
|
|
|
|
|
|
|
evReady.fire();
|
|
|
|
postMsg('_READY');
|
|
|
|
} else {
|
|
|
|
|
|
|
|
chanLoaded = true;
|
|
|
|
|
|
|
|
waitingData.forEach(function (d) {
|
|
|
|
|
|
|
|
onMsg.fire(d);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
waitingData = [];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(chan);
|
|
|
|
cb(chan);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|