Socket
Socket
Sign inDemoInstall

suub

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

suub

A simple pub/sub written in Typescript


Version published
Weekly downloads
7
increased by16.67%
Maintainers
1
Weekly downloads
 
Created
Source

suub logo

📫 Suub Build Status codecov

A simple pub/sub written in Typescript

Gist

import { Subscription } from '../dist';

const mySub = Subscription.create<number>();

const unsub = mySub.subscribe(num => {
  console.log('num: ' + name);
});

mySub.call(45); // num: 45

unsub();

Reference / SubId

You have two ways to subscribe / unsubscribe.

  • Using the reference of the listener
const mySub = () => {
  /*...*/
};
subscription.subscribe(mySub);
// later
subscription.unsubscribe(mySub);
  • Using a SubId
subscription.subscribe('mySubId', () => {
  /*...*/
});
// later
subscription.unsubscribe('mySubId');

In both case the subscribe return a function that will unsubscribe:

const unsub = subscription.subscribe(/*...*/);
// later
unsub();

A few details

Listeners are called in the order they are subscribed.
If you re-subscribe the same listener it will not re-do a subscription but instead move the subscription to the end.

In other words, calling subscribe on an already subscribed listener is the same as calling unsubscribe then subscribe on that listener except you get the same unsubscribe reference back.

If you call unsubscribe in a listener it will have effect immediatly.

If the listener you unsubscribe is supposed to run after the current listener, it will not be called.

If you subscribe in a listener it will not be called immediatly.

But it will be in the next call.

If you call() in a listener it will defer the call to after the current call is done.

Examples

Take a look at the Examples folder.

API

Subscription.create(options: Options): Subscription

Create a new Subscription

  • options.onFirstSubscription?: A function called when the listener count goes from 0 to 1
  • options.onLastUnsubscribe?: A function called when the listener count goes from 1 to 0
  • options.maxListenerCount?: The maximum number of listeners (default is 10000)
  • options.maxRecursiveCall?: The maximum number of recursive call (calling call in a listener) (default is 1000)

Note: by default the T type is void meaning the Subscription has no data.

Subscription<T>

A subscription object

Subscription<T>.subscribe([subId, ] listener): Unsubscribe

Add a listener

  • subId (optional): Associate an id with the listener to be able to unsubscribe by this same id.

  • listener: The function that will be called when you call, this function receive a value as parameter (of type T)

  • return Unsubscribe: returns a function that will unsubscribe the listener.

Subscription<T>.call(value: T)

Call all listeners in the same order they were subscribed

  • value: The value (of type T) that will be passed to the listeners

Note: If T is void this function does not take any arguments.

Subscription<T>.unsubscribeAll()

Unsubscribe all listeners

Subscription<T>.unsubscribe(listener)

Unsubscribe a listener either by reference or by id

  • listener: Either an id (string) or a reference to a listener

Subscription<T>.isSubscribed(listener): boolean

Test wether a listener id subscribed or not

  • listener: Either an id (string) or a reference to a listener

Keywords

FAQs

Package last updated on 14 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc