This is a tiny event system, sometimes called a "message bus"; or "bus" for short, and carries a similar API to the likes of the NodeJS Event Emitter - stripped down to the bare essentials: on
, off
, and emit
Usage:
--create a new emitter with the pubsub() function emitter = pubsub() --listen to an event emitter.on("foo", function (e) print('foo'..e) end ) --fire an event emitter.emit('foo', { a='b' }) --working with handler references: function onFoo() end emitter.on('foo', onFoo) // listen emitter.off('foo', onFoo) // unlisten |
DEMO
API
emit
Invoke all handlers for the given type.
Method emit(string, event)
parameter | type | description |
type | string | The event type to invoke |
evt | any | Any value (table is recommended), passed to each handler |
--emit() example: channel.emit('awesomeness', stuff) |
on
Register an event handler for the given type.
Method on(string, event)
parameter | type | description |
type | string | String Type of event to listen for |
handler | Function | Function to call in response to given event |
--on() example: channel.on('awesomeness', awesomeHandler) |
off
Remove an event handler for the given type.
Method off(string, event)
parameter | type | description |
type | string | Type of event to unregister handler from |
handler | Function | Handler function to remove |
--off() example: channel.off('awesomeness', awesomeHandler) |
[Please log in to post a comment]