Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for async request-respond through post-message #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion lib/PluginApi.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var xde = require('cross-domain-events');
var data = {};
var i = 0;

Expand Down Expand Up @@ -30,12 +31,64 @@ function publish (id, subject) {
});
}


/**
*
* Gardr.reqres
*
* Request-response pattern for sending messages
* between gardr hostplugins and ext banners and plugins.
*/

// Sends a message to otherWindow with params
// waits for the response, and calls callback upon that.
function postMessage (subject, params, callback, otherWindow) {
otherWindow = otherWindow || window.top;

try {
xde.sendTo(otherWindow, 'gardr:' + subject, params);

xde.on('gardr:' + subject + ':response', function (evt) {
callback(null, evt.data);
});
} catch(e) {
callback(e);
}
}


// listens to postmessages with subject
// calls the given callback, and upon return
// sends the result back
function respondToPostMessage (subject, cb) {
xde.on('gardr:' + subject, onRespondToMsg(subject, cb))

function onRespondToMsg (subject, cb) {

return function (evt) {
// because cb can be async, it is responsible for
// triggering the _onCallbackComplete function
cb(evt.data, onCallbackComplete.bind(null, subject, evt));
}
}

function onCallbackComplete (subject, evt, res) {
xde.sendTo(evt.source, 'gardr:' + subject + ':response', res);
}
}


var PluginApi = function () {
this.id = i++;

set(this.id, 'listeners', {});
this.on = subscribe.bind(null, this.id);
this.trigger = publish.bind(null, this.id)
this.trigger = publish.bind(null, this.id);

// The reqres module enables gardr plugins to communicate after
// the banner has been document writed.
this.postMessage = postMessage;
this.respondToPostMessage = respondToPostMessage;

this._reset = function () { set(this.id, 'listeners', {}); };
};
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
"karma": "~0.12.1",
"karma-browserifast": "~0.4.1",
"es5-shim": "~2.3.0"
},
"dependencies": {
"cross-domain-events": "0.0.2"
}
}
64 changes: 54 additions & 10 deletions test/PluginApi.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
/*jshint expr: true, nonew: false*/
var PluginApi = require('../lib/PluginApi.js');
var PluginApi = require('../lib/PluginApi.js'),
xde = require('cross-domain-events'),
api;

describe('PluginApi', function () {

beforeEach(function () {
api = new PluginApi();
});

afterEach(function () {
api._reset();
});

describe('constructor', function () {

it('should not throw if no arguments', function () {
expect(function () {
new PluginApi();
Expand All @@ -12,15 +23,6 @@ describe('PluginApi', function () {
});

describe('events', function () {
var api;

beforeEach(function () {
api = new PluginApi();
});

afterEach(function () {
api._reset();
});

it('should exist', function () {
expect(api.on).to.exist;
Expand Down Expand Up @@ -63,4 +65,46 @@ describe('PluginApi', function () {
}).not.to.throw();
});
});

describe('reqres', function () {

describe('end-to-end', function () {
var evtName = 'yoer:bar';
var reqData = {fooInt : 1337};

it('should call request.callback with proper params when there is a responder', function (done) {
callRespondToPostMessage(api, evtName);
callPostMessage(api, evtName, reqData, 1338, done);
});
});

describe('errors', function () {
it('should catch xde errors', function () {
api.postMessage('test:event', {}, function (err) {
expect(err).to.exist;
expect(err.message).to.equal('otherWindow does not support postMessage');
}, 'not a window object');
});
})
});
});


function callRespondToPostMessage (api, evtName) {
api.respondToPostMessage(evtName, onResponseMsg);

function onResponseMsg (responseData, commCb) {
var result = responseData.fooInt + 1;
commCb({fooIntResult : result});
}
}

function callPostMessage (api, evtName, reqData, expectedResult, done) {
api.postMessage(evtName, reqData, onXdeCb, window);

function onXdeCb (err, responseData) {
expect(err).to.be.null;
expect(responseData.fooIntResult).to.equal(expectedResult);
done();
}
}