Navigation Menu

Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Implemented buffering of calls made prior to onReady http://github.co…
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindkinsey committed Jun 19, 2010
1 parent e7c686d commit 2ee33a3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/easyXDM.js
Expand Up @@ -579,15 +579,15 @@ function prepareTransportStack(config){
})];
break;
case "1":
stackEls = [new easyXDM.stack.PostMessageTransport(config)];
stackEls = [new easyXDM.stack.PostMessageTransport(config), new easyXDM.stack.QueueBehavior()];
break;
case "2":
stackEls = [new easyXDM.stack.NameTransport(config), new easyXDM.stack.QueueBehavior(), new easyXDM.stack.VerifyBehavior({
initiate: config.isHost
})];
break;
case "3":
stackEls = [new easyXDM.stack.NixTransport(config)];
stackEls = [new easyXDM.stack.NixTransport(config), new easyXDM.stack.QueueBehavior()];
break;
}

Expand Down
3 changes: 2 additions & 1 deletion src/stack/NixTransport.js
Expand Up @@ -21,8 +21,9 @@ easyXDM.stack.NixTransport = function(config){
frame, send, targetOrigin, proxy;

return (pub = {
outgoing: function(message, domain){
outgoing: function(message, domain, fn){
send(message);
fn();
},
destroy: function(){
// #ifdef debug
Expand Down
3 changes: 2 additions & 1 deletion src/stack/PostMessageTransport.js
Expand Up @@ -60,8 +60,9 @@ easyXDM.stack.PostMessageTransport = function(config){
}

return (pub = {
outgoing: function(message, domain){
outgoing: function(message, domain, fn){
callerWindow.postMessage(config.channel + " " + message, domain || targetOrigin);
fn();
},
destroy: function(){
// #ifdef debug
Expand Down
20 changes: 16 additions & 4 deletions src/stack/QueueBehavior.js
@@ -1,5 +1,5 @@
/*jslint evil: true, browser: true, immed: true, passfail: true, undef: true, newcap: true*/
/*global easyXDM, window, escape, unescape, debug*/
/*global easyXDM, window, escape, unescape, debug, undef*/

/**
* @class easyXDM.stack.QueueBehavior
Expand All @@ -17,7 +17,7 @@ easyXDM.stack.QueueBehavior = function(config){
var trace = debug.getTracer("easyXDM.stack.QueueBehavior");
trace("constructor");
// #endif
var pub, queue = [], waiting = false, incoming = "", destroying, maxLength = (config) ? config.maxLength : 0, encode = (config) ? (config.encode || false) : false;
var pub, queue = [], waiting = true, incoming = "", destroying, maxLength = 0;

function dispatch(){
if (waiting || queue.length === 0 || destroying) {
Expand All @@ -40,14 +40,26 @@ easyXDM.stack.QueueBehavior = function(config){
});
}
return (pub = {
init: function(){
if (undef(config)) {
config = {};
}
maxLength = config.maxLength ? config.maxLength : 0;
pub.down.init();
},
callback: function(success){
waiting = false;
dispatch();
pub.up.callback(success);
},
incoming: function(message, origin){
var indexOf = message.indexOf("_"), seq = parseInt(message.substring(0, indexOf), 10);
incoming += message.substring(indexOf + 1);
if (seq === 0) {
// #ifdef debug
trace("received the last fragment");
// #endif
if (encode) {
if (config.encode) {
incoming = decodeURIComponent(incoming);
}
pub.up.incoming(incoming, origin);
Expand All @@ -60,7 +72,7 @@ easyXDM.stack.QueueBehavior = function(config){
// #endif
},
outgoing: function(message, origin, fn){
if (encode) {
if (config.encode) {
message = encodeURIComponent(message);
}
var fragments = [], fragment;
Expand Down
29 changes: 29 additions & 0 deletions src/tests/tests.js
Expand Up @@ -431,6 +431,35 @@ function runTests(){
this.transport.postMessage(this.expectedMessage);
}
}]
}, {
name: "test easyXDM.Socket{} with buffering",
setUp: function(){
this.expectedMessage = "4abcd1234";
},
tearDown: function(){
this.transport.destroy();
if (document.getElementsByTagName("iframe").length !== 0) {
throw new Error("iframe still present");
}
},
steps: [{
name: "onReady is fired, and buffered message sent",
timeout: 5000,
run: function(){
var scope = this;
this.transport = new easyXDM.Socket({
local: "../name.html",
remote: REMOTE + "/test_transport.html",
onMessage: function(message, origin){
scope.notifyResult((scope.expectedMessage === message));
},
onReady: function(){
scope.notifyResult(true);
}
});
this.transport.postMessage(this.expectedMessage);
}
}]
}, {
name: "test easyXDM.Socket{} with query parameters",
setUp: function(){
Expand Down

0 comments on commit 2ee33a3

Please sign in to comment.