Log In  

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

Cart #38909 | 2017-03-31 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
11

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)
P#38910 2017-03-31 05:44 ( Edited 2017-04-01 03:55)

very cool! thanks for this.

P#38956 2017-03-31 23:10 ( Edited 2017-04-01 03:10)

You are most welcome :)

P#38960 2017-03-31 23:26 ( Edited 2017-04-01 03:32)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 16:01:43 | 0.010s | Q:16