1.0.0 • Published 2 years ago

@trans.eu/pubsub v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

pubsub

A topic-based publish/subscribe library.

Table of Contents

Usage

import createPubsub from '@trans.eu/pubsub';

const pubsub = createPubsub();
const token = pubsub.subscribe(topic, callback);
pubsub.publish(topic); // callback is invoked
pubsub.unsubscribe(token);
pubsub.publish(topic); // callback is not invoked

API

NameParamsReturnsDescription
publishtopic: String, data: any, sync: Boolean-Publishes an event with the given topic name.
subscribetopic: String | RegExp, callback: Functiontoken: FunctionInvokes callback on all events published with matching topic names. Callback is called with topic and data.
subscribeOncetopic: String | RegExp, callback: Functiontoken: FunctionInvokes callback on the first event published with a matching topic name.
unsubscribetoken: Function-Removes a subscription.
unsubscribeAlltopic?: String | RegExp-Removes all subscriptions for matching topic names.
hasSubscriberstopic: String | RegExpBooleanChecks if any subscription to the given topic exist.
isolate-instance: ObjectCreates an isolated scope for publishing events and topic subscription.

Examples

publish

// publish an event with the topic name 'test'
pubsub.publish('test');

// publish an event with the topic name 'test' and data (asynchronous)
pubsub.publish('test', 'data');

// publish an event with the topic name 'test' and data (synchronous)
pubsub.publish('test', 'data', true);

subscribe

// invoke callback on events published with the topic name 'test'
pubsub.subscribe('test', (topic, data) => {});

// invoke callback on events published with topic names that match the /^test.*/ RegExp pattern
pubsub.subscribe(/^test.*/, (topic, data) => {});

subscribeOnce

// invoke callback on the first event published with the topic name 'test'
pubsub.subscribeOnce('test', (topic, data) => {});

// invoke callback on the first event published with a topic name that matches the /^test.*/ RegExp pattern
pubsub.subscribeOnce(/^test.*/, (topic, data) => {});

unsubscribe

// remove a subscription to events published with the topic name 'test'
const token = pubsub.subscribe('test', (topic, data) => {});
pubsub.unsubscribe(token);

unsubscribeAll

// remove all subscriptions to events published with the topic name 'test'
pubsub.unsubscribeAll('test');

// remove all subscriptions to events published with topic names that match the /^test.*/ RegExp pattern
pubsub.unsubscribeAll(/test.*/);

// remove all subscriptions
pubsub.unsubscribeAll();

isolate

// create a pubsub instance
const pubsub = createPubsub();

// create an isolated pubsub scope 'scope_1'
const scope_1 = pubsub.isolate();

// create an isolated pubsub scope 'scope_2'
const scope_2 = pubsub.isolate();

// subscribe to events published with the topic name 'test'
pubsub.subscribe('test', (topic, data) => console.log('global'));

// subscribe to events published with the topic name 'test' within 'scope_1'
scope_1.subscribe('test', (topic, data) => console.log('scope_1'));

// subscribe to events published with the topic name 'test' within 'scope_2'
scope_2.subscribe('test', (topic, data) => console.log('scope_2'));

// publish events
pubsub.publish('test'); // global
scope_1.publish('test'); // global, scope_1
scope_2.publish('test'); // global, scope_2

// remove subscriptions to events with the topic name 'test' only on scope_1
scope_1.unsubscribeAll('test');
pubsub.publish('test'); // global
scope_1.publish('test'); // global
scope_2.publish('test'); // global, scope_2

// remove subscriptions to events with the topic name 'test' on all scopes
pubsub.unsubscribeAll('test');