Module: message-bus

This module implements a very simple message bus system for loose coupling of components. It is only used for logging initially, but we keep it in case there is a good reason for loose coupling, which may include communication between different instances. Messages are sent on a "bus" and any other module can subscribe to messages of certain types. The logger module is an example for a message consumer.

Source:

Methods

on(type, fn) → {undefined}

Parameters:
Name Type Description
type string

String format "source:type" or only "type" for all handlers from any source. Request types should end in "-request", but I won't add code to check it. Handlers registered with on(...) should return a Promise - but I don't check that either.

fn MessageHandlerCb

This function is available on the MessageBusObj API-object returned by the createMessageBus function.

Note that if you call "on" more than once with the same callback function A, there will be no warning (no collision detection), and B, the callback will still be called just once.

Event types (examples)

Sent with msg={Error} from anywhere. Details are in the Error object. [any]:error

Debug messages [any]:info

Sent with msg={{type,hash}} whenever a new ONE microdata object has been saved to long-term storage. ´storage:new-object`

For example emitted by node-imap: alert(<string>message): Emitted when the server issues an alert (e.g. "the server is going down for maintenance") imap-mailbox-retriever:alert

Sent by an ImapMailboxRetriever instance after closing the IMAP connection imap-mailbox-retriever:end

Source:
Returns:

Type: undefined

once(type, fn) → {undefined}

Parameters:
Name Type Description
type string

String format "source:type" or only "type" for all handlers from any source.

fn MessageHandlerCb

This function is available on the MessageBusObj API-object returned by the createMessageBus function.

Note that unlike with "on", when you call "once" more than once with the same callback function A, just like with "on" there will be no warning, but B, unlike "on" the callback function will be called as many times as you called "once" to register it (albeit just once each time, of course).

Source:
See:
Returns:

Type: undefined

remove(type, fn) → {undefined}

Parameters:
Name Type Description
type string
fn MessageHandlerCb

This function is available on the MessageBusObj API-object returned by the createMessageBus function.

Source:
Returns:

Type: undefined

send(src, type, …messagesopt) → {undefined}

Parameters:
Name Type Attributes Description
src string
type string
messages * <optional>
<repeatable>

This function is available on the MessageBusObj API-object returned by the createMessageBus function.

Listeners are called synchronously in insertion order - but all specific listeners, those who specified a source "source:type" in the .on(...) function, are called first. Listeners for handlers to be called regardless of the source are next.

Source:
Returns:

Type: undefined

(static) createMessageBus(moduleId) → {MessageBusObj}

Parameters:
Name Type Description
moduleId string

A string used to identify the source of any messages sent through the returned MessageBus object's methods.

Creates a MessageBus object associated with the given ID string.

Source:
Returns:

An API-object that makes several functions available.

Type: MessageBusObj

(static) createWithRunningId(moduleId) → {MessageBusObj}

Parameters:
Name Type Description
moduleId string

A string used to identify the source of any messages sent through the returned MessageBus object's methods. Internally a counter is appended.

Frontend to the createMessageBus function which adds a number to the given moduleId string that increases by one each time the same moduleId string is used. This function is for creating MessageBusObj instances for modules that are not singletons. The created moduleId that can be used to subscribe to messages from this component will be of the form ${moduleId}-${number}, for example, if moduleId is myModule the actual ID associated with the MessageBusObj API-object will be myModule-3 if this method is called three times with this same moduleId.

Source:
Returns:

Type: MessageBusObj

(static) messageBusesIds() → {Array.<MessageBusObj>}

Creates and returns a cloned copy of the internal array of message bus IDs currently in use

Source:
Returns:

Type: Array.<MessageBusObj>

Type Definitions

MessageHandlerCb(src, …messages) → {undefined}

Parameters:
Name Type Attributes Description
src string
messages * <repeatable>

Callback function definition for message bus event handlers

Type definition:

(src: string, ...messages: unknown[]) => void
Source:
Returns:

Returns undefined

Type: undefined