Shouter
Simple, yet powerful micro framework for event broadcasts.
; const help = ;shouter;shouter;shouter;
Install
NPM:
$ npm install shouter
And then (ES5):
var shouter = shouter; shouter ; shouter results ;
See example
folder for ES6 and ES.next example.
Size
≈816 byte (gzipped)
Listening to channels and routes
When subscribing to an event you must specify a channel, route and a callback:
shouter;
The route can however be a asterisk, *
, to subscribe on all routes on the specific channel:
shouter;shouter; // this will trigger "callback".
You can also choose to trigger all routes:
shouter;shouter; // this will trigger "callback".
You can also bind a context to the callback:
let context = called: 0;let { thiscalled++;}shouter;// Same as: shouter.on('channel', 'route', callback.bind(context));
You can also pass a number of arguments:
let a+b+c;shouter;shouter;
Or something like this:
shouter;shouter results ; // 1
Async
All events will be trigged as async events:
let console;shouter;shouter;console; // Output:// last one// callback
trigger
will however return a promise object with the result of all trigged events:
let console;shouter;shouter results ; // This will output:// callback// last one
results
will contain the result of all trigged events:
let 3142;let 2718;shouter;shouter;shouter results ; // This will output (or in reverse order):// 3.142// 2.718
results
is wrapped in Promise.all
:
let throw 'I forgot pi';let 2718;shouter;shouter;shouter results ; // This will output:// I forgot pi
You can save an event if you don't know if the subscriber has started yet:
shouter ; // Save the event to future subscriber let console;let context = undefined; // default: undefinedlet getAlreadySubmittedEvents = true; // default: falseshouter; // will trigger callback
Decorators
If you are using ES2017 (aka ES8) (or typescript) you can decorate your classes:
You can run this example by babel-node examples/decorators.js
(remember to run npm install
first)
; @ { return this_name = name; } @ { return this_name; } // This will only work on static methods since we don't have access to `this` // See: https://github.com/wycats/javascript-decorators/issues/13#issuecomment-120875498 for more information @ { console; } shouter;shouter; let oskar = ;oskarname = 'Oskar';console; shouter; // output:// The person's name is: Oskar// My new name is Oskar// Someone is asking for my name and I told them: Oskar// Hello, Dexter Morgan
Typescript
Resolve the type definition by setting: ´moduleResolution´ to ´node´:
...
"moduleResolution": "node",
...
Contribute
See more examples in test
or exampels
folder.
All code are written in ES6/ES.next (ES2016/ES2018).
To install: npm install
To run the exampels
: babel-node {file}
, eg: babel-node examples/ping_pong.js
To build the source to ES5: gulp
.
To test: npm test