Bus

Bus

A generic message bus. Offers ways to receive and subscribe to messages on a hierarchy of channels.

Constructor

new Bus()

Example
// Listen to an event on the global system bus
function listener() {
    console.log('caught message!');
}
SystemBus.addListener('eventName', listener);

// Emit an event on the bus
SystemBus.emit('eventName');

// Remove the listener after you're done with it
SystemBus.removeListener('eventName', listener);

Members

(static) Channel

An alias for Channel.

Methods

addListener(channelName, callback, retrieveLatestEmitopt)

Register callback for a channel

Parameters:
Name Type Attributes Default Description
channelName string
callback function

function (data)

retrieveLatestEmit boolean <optional>
false

Retrieve the last emit done before this listener was added (if emitted with storeEmit)

Example
bus.addListener('channel', function (data){
    console.log('Received message with data:', data);
});

emit(channels, data, storeEmitopt)

Sends messages to all listeners with provided callback function.

Parameters:
Name Type Attributes Default Description
channels string | Array.<string>

channel(s) addressed

data Object
storeEmit boolean <optional>
false

Store the emit data for transmitting to future listeners

Example
bus.emit('emptyEvent');
bus.emit('eventWithData', data);
bus.emit(['channel1', 'channel2'], data);

getLastMessageOn(channelName)

Retrieves the last message sent on a channel. This will only work if message preservation is enabled when emitting.

Parameters:
Name Type Description
channelName

makeChannel(channelName) → {Channel}

Creates a Channel object that can be used to listen for and emit messags on a
Bus channel. This function is typical used in a module to create exports
that provide a way to use a channel.

In MyEvents.js:

export SomeEvent = SystemBus.makeChannel('some-channel');

In WantEvents.js:

import SomeEvent from ...

SomeEvent.addListener(() => { ... });

Parameters:
Name Type Description
channelName string

The channel used by the created object.

removeAllOnChannel(channelName)

Removes all listeners on a specific channel

Parameters:
Name Type Description
channelName

removeChannelAndChildren(channelName)

Removes a channel and its children

Parameters:
Name Type Description
channelName

removeListener(channelName, callbackToRemove)

Remove a listener from a channel but not from its children

Parameters:
Name Type Description
channelName string
callbackToRemove function
Example
bus.removeListener('channel', listener);

removeListenerFromAllChannels(callbackToRemove)

Removes a listener from all channels

Parameters:
Name Type Description
callbackToRemove