The common commopnents for bus.io
Access the common library
var common = require('bus.io-common')
, Mesage = common.Message
, Builder = common.Builder
, Controller = common.Controller
;
The message encapulates a few core components.
actor
The person that created this message.action
The action the actor performed.target
The other actor the action was performed on.content
The data associated with the action performed on the target.
Simply creating a mesage.
var message = Mesage();
All positional parameters.
Message#(actor:String, action:String, content:mixed, target:String, created:Date, id:String, reference:String, published:Date)
var message = Message('i', 'said', 'hello', 'you', new Date(), uuid.v1(), null, null);
If you miss any tailing parameters they will be set to defaults which means you can do.
var message = Message('i', 'said', 'hello', 'you');
Pass in an object
with the attributes.
var message = Message({
actor: 'I',
action: 'say',
content: 'hello',
target: 'you'
});
Simply returns the passed instnace
var a = new Message();
var b = new Message(a);
assert.equal(a, b);
Returns a cloned instance of this message, however has a new id
.
var a = new Message();
var b = a.clone();
assert.notEqual(a.id(), b.id());
var actor = message.actor();
var actor = message.actor('me').actor();
assert.equal(actor, 'me');
var action = message.action();
var action = message.action('spoke').action();
assert.equal(action, 'spoke');
var content = message.content();
var content = message.content('stuff').content();
assert.equal(content, 'stuff');
var target = message.target();
var target = message.target('spoke').target();
assert.equal(target, 'spoke');
Gets the message id
which is a UUID version 1.
var id = message.id();
Gets the time message was created
.
var created = message.created();
assert.equal(created intanceof Date, true);
Gets the id
of the message in which this message references.
var reference = message.reference();
Gets the time in which the message was published or null
if it wasn't.
var published = message.published();
This is used to build a message. It is an EventEmitter
and when it is done
building a message it publishes a built
event with the Message
instance.
To listen to the built
event.
var builder = Builder();
builder.on('built', function (message) {
// do something with the message
});
Gets a builder instance and uses internally a blank Message
instance.
var builder = Builder();
An Object
instance or a Message
instance or null
value can be passed in. It
will be base for the object being built.
var builder = Builder({actor: 'I'});
var content = builder.content('stuff').content();
assert.equal(content, 'stuff');
var target = builder.target();
var target = builder.target('spoke').target();
assert.equal(target, 'spoke');
Gets the message id
which is a UUID version 1.
var id = builder.id();
Gets the time the message was created
.
var created = builder.created();
assert.equal(created intanceof Date, true);
Gets the id
of the message in which this builder references.
var reference = builder.reference();
Gets the time in which the message was published or null
if it wasn't.
var published = builder.published();
Sets the actor
on the message.
builder.i('actor');
Sets the action
on the message.
builder.did('action');
Sets the content
on the message.
builder.what('some content');
Gets the message data.
var data = builder.data();
Sets the message data.
builder.data({actor:'me', target:'you', action:'say', content:'hello'});
Triggers the built
event.
builder.on('built', function (message) {
console.log('the message', message);
});
builder.deliver();
You can pass in multiple targets so the builder will build a message for each passed in target.
builder.to('joe','jim','john','jack');
The Controller
controls the message and delegates function calls to the Message
instance
it is controlling. The controller will emit an event when you manipulate the message.
The message
must be a Message
instance.
var controller = Controller( Message() );
var actor = controller.actor();
var actor = controller.actor('me').actor();
assert.equal(actor, 'me');
var action = controller.action();
var action = controller.action('spoke').action();
assert.equal(action, 'spoke');
var content = controller.content();
var content = controller.content('stuff').content();
assert.equal(content, 'stuff');
var target = controller.target();
var target = controller.target('spoke').target();
assert.equal(target, 'spoke');
Gets the message id
which is a UUID version 1.
var id = controller.id();
Gets the time the message was created
.
var created = controller.created();
assert.equal(created intanceof Date, true);
Gets the id
of the message in which this controller references.
var reference = controller.reference();
Gets the time in which the message was published or null
if it wasn't.
var published = controller.published();
The controller will emit a respond
event with a new message. The new message's
actor and target are swapped and the new content is replaced with the passed in
argument. Calling respond
will stop propagation of the message in any of the event handlers.
contoller.on('respond', function (message) {
console.log('message', message);
});
controller.respond('hello');
The controller will emit a deliver
event and set the delivered
flag to the time it was triggered.
This is how to respond to the actor as if you are the target the message was being sent to.
Calling deliver
will stop propagation of the message in any of the event handlers.
contoller.on('deliver', function (message) {
console.log('message', message);
});
controller.deliver();
The controller will emit a deliver
event to each of passed in targets. This is how
you deliver a message to the targret. Calling deliver
will stop propagation of the message
in any of the event handlers.
contoller.on('deliver', function (message) {
console.log('message', message);
});
controller.deliver('you', 'me', 'dupree');
This will consume the message and propagation of the message either to the bus, on the bus, or to the socket will be halted.
controller.consume();
When processing a message you decide that you want to respond with an error simply call errored
.
controller.errored(new Error('Some Error'));
Install node.js (See download and install instructions here: http://nodejs.org/).
Clone this repository
> git clone [email protected]:turbonetix/bus.io-common.git
cd into the directory and install the dependencies
> cd bus.io-common
> npm install && npm shrinkwrap --dev
Install coffee-script
> npm install coffee-script -g
Tests are run using grunt. You must first globally install the grunt-cli with npm.
> sudo npm install -g grunt-cli
To run the tests, just run grunt
> grunt spec