message-bank
v1.0.5
Published
Subscribable message bank
Downloads
7
Readme
Message Bank
Subscribable message bank.
MessageBank behaves subtly different than a standard message bus. Subscribing to a message type that has been dispatched previously the callback function will fire immediately.
API
###MessageBank(options:Object)
Creates a new message bank instance with predefined options.
var bank = new MessageBank();
####options.setup(callback:Function)
Alias for bank.setup(func)
var bank = new MessageBank({
setup: function() { }
});
####options.config(options:Object)
Alias for bank.config()
var bank = new MessageBank({
config: [{
type: 'SUPER',
transform: function(data, opts) { }
}]
});
###Bank().setup(callback:Function)
Calls callback function immediatly with MessageBank instance as function context. Inert function by default, provides entry point additional setup behavior not default to MessageBox
bank.setup(function() { });
###Bank().config(type:String, config:Object)
Also: Bank().config(config:Object)
Bank().config(config:ObjectArray)
Method for configuring how dispatched data is handled for specific dispatch type. Currently only defines transform config option, but this is a good place to enter custom configuration to be used in a custom parse function.
####config.transform(callback:Function)
By default transform config is called with data and options as arguments during the parse phase of a dispatch call.
bank.config('SUPER', {
transform: function(data, opts) {
data.duper = opts.isNonsense ? false : data.duper;
return data;
}
});
###Bank().dispatch(type:String, data:Object, options:Object)
Also: Bank().dispatch(dispatch:Object)
Bank().dispatch(dispatch:ObjectArray)
Dispatches a payload of data to the MessageBank, any subscriptions matching the payload type will be called.
bank.dispatch('SUPER', { duper: true }, { isNonsense: true });
###Bank().subscribe(type:String, callback:Function, options:Object)
Subscribe to a specific type of dispatch payload, if previous payload exists callback will be called immediately. Subscribe returns an id that can used to unsubscribe later.
Subscribing to the *
or ALL
type will respond to all dispatches. Bank().subscribe(callback:Function, opts:Options)
will do the same.
bank.subscribe('SUPER', function(data, opts) {
console.log(data, opts);
})
#####options.immediate
By default subscriptions will be called immediately if a payload of that type exists, setting immediate:false
will hold of on calling until the next dispatch.
bank.subscribe('SUPER', function(data, opts) {
console.log(data, opts);
}, { immediate: false });
###Bank().unsubscribe(id:Integer)
Unsubscribe from a dispatch type using subscription ID returned when defining a subscription.
var id = bank.subscribe('SUPER', function() {
// run only once
bank.unsubscribe(id);
});
###Bank().parse(type:String, data:Object, options:Object)
Internal function for manipulating dispatched data expecting a result of { data: {}, options: {} }
By default parse runs any transform configs, but it can be overwriten for custom behavior.