Git Product home page Git Product logo

pub-sub's Introduction

Simple ES6-based pub-sub service

Build Status

A simple pub-sub service for custom events written in ES6 or whatever the latest version of EcmaScript is called.

Install

npm -i -D pub-sub-es

Get Started

import createPubSub from 'pub-sub-es';

// Creates a new pub-sub event listener stack.
const pubSub = createPubSub();

// Subscribe to an event
const myEmojiEventHandler = msg => console.log(`๐ŸŽ‰ ${msg} ๐Ÿ˜`);
pubSub.on('my-emoji-event', myEmojiEventHandler);

// Publish an event
pubSub.publish('my-emoji-event', 'Yuuhuu');  // >> ๐ŸŽ‰ Yuuhuu ๐Ÿ˜

// Unsubscribe
pubSub.unsubscribe('my-emoji-event', myEmojiEventHandler);

API

pub-sub-es exports the factory function (createPubSub) for creating a new pub-sub service by default and a global pub-sub service (globalPubSub). The API is the same.

createPubSub(stack = { __times__: {} })

  • stack is an object holding for holding the event listeners and defaults to a new stack when being omitted.

Returns: a new pub-sub service

pubSub.publish(event, news)

  • event is the name of the event to be published.
  • news is an arbitrary value that is being broadcasted.

pubSub.subscribe(event, handler, times)

  • event is the name of the event to be published.
  • handler is the handler function that is being called together with the broadcasted value.
  • times is the number of times the handler is invoked before it's automatically unsubscribed.

Returns: an object of form { event, handler }. This object can be used to automatically unsubscribe, e.g., pubSub.unsubscribe(pubSub.subscribe('my-event', myHandler)).

pubSub.unsubscribe(event, news)

  • event is the name of the event to be published. Optionally, unsubscribe accepts an object of form { event, handler} coming from subscribe.
  • news is an arbitrary value that is being broadcasted.

Development

Some handy commands:

  • npm build: builds the UMD library
  • npm run watch: continuously builds the UMD library
  • npm run lint: checks the code style
  • npm test: runs the test suite

pub-sub's People

Contributors

flekschas avatar

Watchers

James Cloos avatar  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.