PubSub

PubSub provides queued event channels.

Any string is acceptable as a channel identifier.

Example

import { PubSub } from 'dripcap';

PubSub.pub('core:message-added', 'hello')

PubSub.sub('core:message-added', (msg) => {
  console.log(msg);
});

let top = PubSub.get('core:message-added')

Class Methods

PubSub.pub(channel,message[,queue=1])

  • channel String - Channel identifier
  • message Any - Message
  • queue Integer (optional) - Queue size

Enqueues the given message to the channel and notifies the message to listeners.

You can specify the maximum queue size with the queue parameter. 0 means no queuing.

PubSub.emit(channel,message[,queue=0])

  • channel String - Channel identifier
  • message Any - Message
  • queue Integer (optional) - Queue size

An alias of PubSub.pub except the default value of the queue is 0.

PubSub.sub([holder,]channel,callback)

  • holder Object (optional) - Holder object
  • channel String - Channel identifier
  • callback Function - Listener function

Register the given callback as a listener to the channel.

If the holder is given, you can remove all listeners associated with the holder at once later. Refer to PubSub.removeHolder.

PubSub.on([holder,]channel,callback)

An alias of PubSub.sub.

PubSub.get(channel[,index=0])

  • channel String - Channel identifier
  • index Integer (optional) - Element index

Returns the front element in the channel queue.

If the index is specified, returns the element at the index instead.

If the queue is empty, returns undefined.

PubSub.removeListener(channel,callback)

  • channel String - Channel identifier
  • callback Function - Listener function

Removes the given callback from the channel.

PubSub.removeHolder(holder)

  • holder Object - Holder object

Removes all listeners associated with the given holder.

Predefined channels

core:package-list-updated

  • Parameters

    • list Object - Package list

Triggered when the package list is updated.

results matching ""

    No results matching ""