Just an isomorphic event emitter
I don't maintain this. In fact, I'd like to forget I wrote it, but some projects need to stay around and keep my humility in check.
Please hide your eyes and move along.
The libraries I build are often expected to work in node and the browser with the same code, and to keep things portable, I avoid build steps. They're awesome, but they have their place.
Iso-emitter is a tiny event emitter that is node agnostic.
It's also splat oriented:
var stream = new Stream
stream.on('event 1', 'event 2', handler1, handler2)
stream.emit('event 1' /*, arguments */ )
// both handlers are invoked
Iso-emitter is intentionally kept simple.
Now coming to a theater near you, splat oriented API (bum bumda bum!)
Events are strings. Callbacks are functions. It really doesn't matter what order you put them in, but to keep things clean, I recommend ordering events left, callbacks right.
Call emit, optionally with arguments, to invoke all subscribed functions.
After passing in the events you want to emit on, it'll return a function
that emits to each of them. Named after the glorious Function.prototype.bind
.
When you call it from the constructor, it emits globally, on every stream instance.
Listening
var stream = new Stream
stream.on('turn', function (direction) {
var positions = {
Up: { direction: -1, axis: 'y' },
Down: { direction: 1, axis: 'y' },
Left: { direction: -1, axis: 'x' },
Right: { direction: 1, axis: 'x' },
}
player.direction(positions[direction])
})
Binding
var turn = (new Stream).bind('turn', 'render')
window.onkeydown = function (e) {
if (typeof e.key === 'string') {
var direction = e.key.match(/Arrow(\w+)/)
turn(direction[1])
}
}
splat subscribing
stream.on(
'out of bounds',
'collision',
player.eliminate
)
emitting
stream.on('move', function (coords) {
if (outOfBounds(coords)) {
stream.emit('out of bounds', player)
}
})
Hope ya like it. If you find any problems, hit me up on gitter or submit an issue or pull request.