API Docs for:
Show:

Utils.Mediator Class

Mediator utility

Methods

publish

(
  • topic
  • True
)

Allows to push/publish data over a channel to connected listeners

Parameters:

  • topic String

    Topic or channel to publish into

  • True Boolean

    if the topic exists

Example:

import mediator from './utils/mediator';

mediator.subscribe('some-channel', (data) => {
    console.log(data);
});

// ... later in code ...

mediator.publish('some-channel', 1); // -> logs "1" into the console

subscribe

(
  • topic
  • handler
  • [scope = undefined]
)
String

Subscribes a listener to a (optionally new) topic/channel on the mediator. Optionally accepts a third argument which defines the scope the listener is called upon (e. g. where "this" points to)

Parameters:

  • topic String

    Topic or channel to subscribe to

  • handler Function

    The listener function to call on activation

  • [scope = undefined] Any optional

    Optional scope for the handler function

Returns:

String:

Identifier key to use when unsubscribing

Example:

import mediator from './utils/mediator';

const someChannelID = mediator.subscribe('some-channel', (data) => {
    console.log(data);
});

unsubscribe

(
  • identifier
  • True
)

Removes a previously made subscription

Parameters:

  • identifier String

    Identifier key of channel to unsubscribe from

  • True Boolean

    on success

Example:

import mediator from './utils/mediator';

const someChannelID = mediator.subscribe('some-channel', (data) => {
    console.log(data);
});

// ... later in code ...

mediator.unsubscribe(someChannelID);

unsubscribeAll

() Undefined

Unsubscribes all listeners from all channels

Returns:

Undefined:

Example:

import mediator from './utils/mediator';

mediator.unsubscribeAll();