Utils.Mediator Class
Mediator utility
Item Index
Methods
Methods
publish
(
-
topic -
True
Allows to push/publish data over a channel to connected listeners
Parameters:
-
topicStringTopic or channel to publish into
-
TrueBooleanif 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
(
String
-
topic -
handler -
[scope = undefined]
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:
-
topicStringTopic or channel to subscribe to
-
handlerFunctionThe listener function to call on activation
-
[scope = undefined]Any optionalOptional 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:
-
identifierStringIdentifier key of channel to unsubscribe from
-
TrueBooleanon 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();
