Package Exports
- barracks
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (barracks) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Barracks
Event dispatcher for the flux architecture. Provides event composition
through this.waitFor(), checks for recursive calls while maintaining
a simple interface (3 functions).
Installation
$ npm i --save barracksOverview
/**
* Initialize a dispatcher.
*/
var barracks = require('barracks');
var dispatcher = barracks({
users: {
add: function(user, done) {
console.log(user + ' got added');
done();
},
remove: function(user, done) {
console.log(user + ' was removed');
done();
}
},
courses: {
get: function(val, done) {
console.log('Get ' + val);
done();
},
set: function(val, done) {
console.log('Set ' + val);
done();
}
}
});
/**
* Dispatch an event.
*/
dispatcher('users_add', 'Loki');
// => 'Loki got added'API
dispatcher = barracks(actions)
Initialize a new barracks instance. The actions object should contain
functions, namespaced at most one level deep. Returns a function.
// Initialize without namespaces.
var dispatcher = barracks({
user: function() {},
group: function() {}
});
// Initialize with namespaces.
var dispatcher = barracks({
users: {
add: function() {},
remove: function() {}
},
courses: {
get: function() {},
put: function() {}
}
});dispatcher(action, data)
barracks() returns a dispatcher function which can be called to dispatch an
action. By dispatching an action you call the corresponding function from
the dispatcher and pass it the data. You can think of it as just calling a
function.
In order to access namespaced functions you can delimit your string with
underscores. So to access courses.get you'd dispatch the string courses_get.
// Call a non-namespaced action.
dispatcher('group', [123, 'hello']);
// Call a namespaced action.
dispatcher('users_add', {foo: 'bar'});dispatcher.waitFor(action)
Execute another function within the dispatcher before proceeding. Registered
callbacks are always bound to the scope of the dispatcher, so you can just
call this.waitFor to access the function from within a registered callback.
In the example below users_initalize will delegate execution to user_add and
user_listen before proceeding to execute its own code.
var socket = require('sockjs-client');
var request = require('request');
var dispatcher = barracks({
users: {
initialize: function(done) {
var arr = ['user_add', 'user_listen'];
this.waitFor(arr, function() {
console.log('initialized');
});
},
add: function(done) {
request('myapi.co/api/users', function(err, res) {
userStore.set(res);
done();
});
},
listen: function(done) {
var sock = new socket('myapi.co/api/socket');
sock.onMessage(console.log);
done();
}
}
});