Code Monkey home page Code Monkey logo

pubsub's Introduction

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

Name Params Returns Description
publish topic: String, data: any, sync: Boolean - Publishes an event with the given topic name.
subscribe topic: String | RegExp, callback: Function token: Function Invokes callback on all events published with matching topic names. Callback is called with topic and data.
subscribeOnce topic: String | RegExp, callback: Function token: Function Invokes callback on the first event published with a matching topic name.
unsubscribe token: Function - Removes a subscription.
unsubscribeAll topic?: String | RegExp - Removes all subscriptions for matching topic names.
hasSubscribers topic: String | RegExp Boolean Checks if any subscription to the given topic exist.
isolate - instance: Object Creates 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');

pubsub's People

Contributors

kkstrk avatar roblan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.