mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-04-07 08:31:02 +00:00
45 lines
No EOL
1.5 KiB
JavaScript
45 lines
No EOL
1.5 KiB
JavaScript
/*
|
|
machina.postal.js
|
|
Author: Jim Cowart
|
|
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
|
|
Version 0.1.0
|
|
*/
|
|
|
|
module.exports = function ( postal, machina ) {
|
|
var bus = machina.bus = {
|
|
channels : {},
|
|
config : {
|
|
handlerChannelSuffix : "",
|
|
eventChannelSuffix : ".events"
|
|
},
|
|
wireHandlersToBus : function ( fsm, handlerChannel ) {
|
|
bus.channels[handlerChannel]._subscriptions.push(
|
|
bus.channels[handlerChannel].subscribe( "#", function ( data, envelope ) {
|
|
fsm.handle.call( fsm, envelope.topic, data, envelope );
|
|
} )
|
|
);
|
|
},
|
|
wireEventsToBus : function ( fsm, eventChannel ) {
|
|
var publisher = bus.channels[eventChannel].eventPublisher = function () {
|
|
try {
|
|
bus.channels[eventChannel].publish( { topic : arguments[0], data : arguments[1] || {} } );
|
|
} catch ( exception ) {
|
|
if ( console && typeof console.log !== "undefined" ) {
|
|
console.log( exception.toString() );
|
|
}
|
|
}
|
|
};
|
|
fsm.on( "*", publisher );
|
|
},
|
|
wireUp : function ( fsm ) {
|
|
var handlerChannel = fsm.namespace + bus.config.handlerChannelSuffix,
|
|
eventChannel = fsm.namespace + bus.config.eventChannelSuffix;
|
|
bus.channels[handlerChannel] = postal.channel( { channel : handlerChannel } );
|
|
bus.channels[eventChannel] = postal.channel( { channel : eventChannel } );
|
|
bus.channels[handlerChannel]._subscriptions = [];
|
|
bus.wireHandlersToBus( fsm, handlerChannel );
|
|
bus.wireEventsToBus( fsm, eventChannel );
|
|
}
|
|
};
|
|
machina.on( "newFsm", bus.wireUp );
|
|
}; |