blackbird
Advanced tools
Comparing version 0.0.1 to 0.0.3
{ | ||
"name": "blackbird", | ||
"version": "0.0.1", | ||
"version": "0.0.3", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "Aron Kornhall <aron@kornhall.se>" |
@@ -74,3 +74,3 @@ (function(){ | ||
function nack(id, err){ | ||
sendObj({ack: true, origin: _id, id: id, args: [err]}); | ||
sendObj({ack: true, origin: _id, id: id, args: [err || 'Error']}); | ||
} | ||
@@ -121,3 +121,3 @@ | ||
}, function(err){ | ||
cb(err); | ||
cb(err || 'Error'); | ||
}); | ||
@@ -172,2 +172,22 @@ }else{ | ||
// Transport templates | ||
var transports = { | ||
iframe: function(targetWindow, sourceWindow, domain){ | ||
if(!targetWindow) throw new Error('No targetWindow specified'); | ||
sourceWindow = sourceWindow || window; | ||
domain = domain || '*'; | ||
return { | ||
send: function(data){ | ||
targetWindow.postMessage(data, domain); | ||
}, | ||
listen: function(fn){ | ||
sourceWindow.addEventListener('message', function(e) { | ||
fn(e.data); | ||
}); | ||
} | ||
}; | ||
} | ||
}; | ||
function blackbird(inbound, outbound, transport){ | ||
@@ -177,2 +197,4 @@ return new Blackbird(inbound, outbound, transport); | ||
blackbird.transports = transports; | ||
// Expose blackbird as node module, amd module or global | ||
@@ -179,0 +201,0 @@ if(typeof module !== 'undefined' && module.exports){ |
{ | ||
"name": "blackbird", | ||
"version": "0.0.1", | ||
"version": "0.0.3", | ||
"description": "Confortable interfacing of event based transports", | ||
@@ -20,4 +20,3 @@ "main": "lib/blackbird.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -28,4 +27,5 @@ "request": "~2.34.0", | ||
"chai": "~1.9.1", | ||
"mocha": "~1.18.2" | ||
"mocha": "~1.18.2", | ||
"bluebird": "^1.2.2" | ||
} | ||
} |
#Blackbird | ||
![blackbird](doc/img/blackbird.png) | ||
Blackbird provides easy-to-use proxy objects that let you call methods across event | ||
based transports such as websockets, iframe postMessage etc. Any message based | ||
communication channel can be used so only your imagination sets the limit. | ||
Blackbird provides easy-to-use proxy objects that let you call methods | ||
across event based transports such as websockets, iframe postMessage channel | ||
etc. Instead of manually emitting and listening to events, blackbird lets | ||
you define interfaces that you can implement on one end point. On the | ||
other end point a proxy object is generated that you can call just as if | ||
it had been a local object. Any message based communication channel can | ||
be used so only your imagination sets the limit. | ||
##Why? | ||
Consider this code for requesting data from one endpoint to the other: | ||
Without blackbird the code for requesting data from one endpoint to the other could look something like this: | ||
@@ -25,6 +29,7 @@ // Endpoint A | ||
Now compare it with this: | ||
This is not very generic and very error prone. | ||
Now compare it with the following blackbird code: | ||
// Endpoint A | ||
blackbird.getName(function(name){ | ||
outbound.getName(function(err, name){ | ||
console.log(name); | ||
@@ -34,10 +39,12 @@ }); | ||
// Endpoint B | ||
blackbird.getName = function(){ | ||
inbound.getName = function(){ | ||
return Paul; | ||
} | ||
Or if you prefer promises over callback: | ||
blackbird abstracts away the underlying messaging and lets you concentrate | ||
on your application logic instead. If you prefer promises over callback | ||
blackbird supports that too out of the box. | ||
// Endpoint A | ||
blackbird.getName(function(name){ | ||
outbound.getName().then(function(name){ | ||
console.log(name); | ||
@@ -47,7 +54,8 @@ }); | ||
// Endpoint B | ||
blackbird.getName = function(){ | ||
inbound.getName = function(){ | ||
return Paul; | ||
} | ||
Blackbird can be used in node, in the browser per script tag and as an AMD module. | ||
Blackbird can be used in node, in the browser per script tag and as an AMD | ||
module. | ||
@@ -62,12 +70,3 @@ Follow [@AronKornhall](http://twitter.com/AronKornhall) for news and updates | ||
// Define a transport | ||
var transport = { | ||
send: function(data){ | ||
window.parent.postMessage(data, '*'); | ||
}, | ||
listen: function(fn){ | ||
window.addEventListener('message', function(e) { | ||
fn(e.data); | ||
}); | ||
} | ||
}; | ||
var transport = blackbird.transports.iframe(window.parent); | ||
@@ -90,12 +89,3 @@ // Inbound interface (will be called from remote) | ||
// Define transport | ||
var transport = { | ||
send: function(data){ | ||
iframe.contentWindow.postMessage(data, '*'); | ||
}, | ||
listen: function(fn){ | ||
window.addEventListener('message', function(e) { | ||
fn(e.data); | ||
}); | ||
} | ||
}; | ||
var transport = blackbird.transports.iframe(iframe.contentWindow); | ||
@@ -119,7 +109,10 @@ // Outbound interface (will be proxied to remote end) | ||
Node | ||
npm install blackbird | ||
Browser | ||
download blackbird.js and include it in a script tag or load it using AMD | ||
bower install blackbird | ||
or download blackbird.js | ||
##Test | ||
@@ -150,2 +143,18 @@ npm test | ||
--------- | ||
blackbird.transports.iframe(targetWindow, sourceWindow, domain) | ||
Shorthand for creating an iframe transport from `sourceWindow` to | ||
`targetWindow`. `domain` is a domain mask that can be used to limit to | ||
which domains messages can be sent. | ||
__Arguments__ | ||
targetWindow {Object} the window to which we should send outgoing messages. To send messages to an embedded iframe use iframe.contentWindow (where iframe is the dom iframe element). To send data to the embedding (host) window of an embedded iframe as a target, use window.parent | ||
sourceWindow {Object} the window on which we should listen for incoming messages. Defaults to the global window. | ||
domain {String} the domain mask. Can be used to limit to which domains messages are sent. Defaults to '*' meaning any domain. | ||
##License | ||
@@ -152,0 +161,0 @@ |
@@ -94,2 +94,23 @@ var blackbird = require('../lib/blackbird'); | ||
it('rejects promise without content', function(done) { | ||
var targetItf = { | ||
fn1: function(){ | ||
return new Promise(function(resolve, reject){ | ||
reject(); | ||
}); | ||
} | ||
}; | ||
var sourceItf = { | ||
fn1: function(){} | ||
}; | ||
blackbird({}, sourceItf, transport); | ||
blackbird(targetItf, {}, transport); | ||
sourceItf.fn1().then(function(val){ | ||
done('should be rejected'); | ||
}, function(err){ | ||
done(); | ||
}); | ||
}); | ||
it('missing fn should reject promise', function(done) { | ||
@@ -96,0 +117,0 @@ var targetItf = { |
@@ -76,2 +76,23 @@ var blackbird = require('../lib/blackbird'); | ||
it('callback is called after remote nack', function(done) { | ||
var called = false; | ||
var targetItf = { | ||
fn: function(cb){ | ||
called = true; | ||
cb('error'); | ||
} | ||
}; | ||
var sourceItf = { | ||
fn: function(){} | ||
}; | ||
blackbird({}, sourceItf, transport); | ||
blackbird(targetItf, {}, transport); | ||
sourceItf.fn(function(err){ | ||
called.should.equal(true); | ||
err.should.equal('error'); | ||
done(); | ||
}); | ||
}); | ||
it('callback with one arg', function(done) { | ||
@@ -78,0 +99,0 @@ var targetItf = { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38377
12
800
177
6