npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rx-pubsub

v0.2.1

Published

PubSub service based on RxJs ReplaySubject

Downloads

36

Readme

RxPubSub

  1. Description
  2. Installation
  3. Usage
  4. Methods
  5. Git repository
  6. Version

1. Description

rx-pubsub or RxPubSub is a "Publish and Subscribe" service based on RxJs ReplaySubject.

2. Installation

Install the module into your application and save it as a dev dependency in your package.json file

npm install rx-pubsub --save-dev

3. Usage

In order to use the RxPubSub service you have to include/import it into your application:

import {RxPubSub} from "rx-pubsub";

If you want to use it in a plain/vanilla Javascript project then you might just include the js file into your html/page application:

<script type="application/javascript" src="./node_modules/rx-pubsub/dist/rx-pubsub.min.js"></script>

Create new RxPubSub object and use it.

Example

let eventName = 'testEvent';
  
console.log('register 1st subscriber to the event');
let sub1 = RxPubSub.subscribe(eventName, (data) => {
    console.log('1st subscriber receives data: ', data);
});  
console.log('register 2nd subscriber to the same event');
let sub2 = RxPubSub.subscribe(eventName, (data) => {
    console.log('2nd subscriber receives data: ', data);
});
  
console.log('publish data to the event');
RxPubSub.publish(eventName, {testProp: 'test Value'});
  
console.log('register 3rd subscriber to the same event');
let sub3 = RxPubSub.subscribe(eventName, (data) => {
    console.log('3rd subscriber receives data: ', data);
});

Output

register 1st subscriber to the event
register 2nd subscriber to the same event
publish data to the event
  
1st subscriber receives data: 
Object {testProp: "test Value"}
  
2nd subscriber receives data: 
Object {testProp: "test Value"}
  
register 3rd subscriber to the same event
  
3rd subscriber receives data:
Object {testProp: "test Value"}

4. Methods

publish(eventName: string, data: any, previousMessagesNr: number = 1)

Publish data to an event

Parameters:
eventName - Event which should be fired
data - Data sent to all Subscribers of the event
previousMessagesNr - Maximum element count of the replay buffer

Return:
Method returns void.

subscribe(eventName: string, callback: (data?: any) => any, previousMessagesNr: number = 1)

Register a new subscriber/callback to an event

Parameters:
eventName - Event to subscribe to
callback - Callback to be called when the eventName is fired
previousMessagesNr - Maximum element count of the replay buffer

Return:
Method returns Subscription if callback and eventName is provided. FALSE if there is an error.

subscribeOnce(eventName: string, callback: (data?: any) => any)

Subscribe to an event only one single time.
After the first publish the Subscriber will be destroyed and will not receive any further data published to its eventName.

Parameters:
eventName - Event to subscribe to
callback - Callback to be called when the eventName is fired
previousMessagesNr - Maximum element count of the replay buffer

Return:
Method returns Subscription if callback and eventName is provided. FALSE if there is an error.

unsubscribe(subscriber: Subscription)

Unsubscribe a Subscriber from the event.

Parameters:
subscriber - the Subscriber which should be destroyed/unsubscribed

Return:
Method returns void.

As the subscribe() and subscribeOnce() methods returns back the RxJs/Subscription object you can use its available methods. The most important thing is the unsubscribe() method provided directly by the RxJs/Subscription.
Hence, if you have the Subscription object:

let eventName = 'testEvent';
  
let subscription = RxPubSub.subscribe(eventName, (data) => {
    console.log('received data: ', data);
});  

you can unsubscribe it using the RxPubSub method:

RxPubSub.unsubscribe(subscription);

or using the rxjs/Subscription method:

subscription.unsubscribe();

unsubscribeAll(subscribers: Subscription[])

Unsubscribe a list of Subscribers from the event

Parameters:
subscribers - The list of subscribers which should be destroyed/unsubscribed

Return:
Method returns void.

dispose(eventName: string)

Unsubscribe all observers from the event and release resources.

Parameters:
eventName - event which should be destroyed.

Return:
Method returns void.

hasSubscribers(eventName: string)

Check if a Subject attached to the event eventName has Subscribers.

Parameters:
eventName - Name of the event to be checked if it has Subscribers.

Return:
Method returns boolean:
true - if there is at least one Subscriber
false - if there are no Subscribers at all

getEvents()

Retrieve the entire list of the events and the Subjects attached to them.

Return:
Method returns an Object which contains the list of the events and the Subjects attached to them.

getSubjects()

An alias for the getEvents() method.

Return:
Method returns an Object which contains the list of the events and the Subjects attached to them.

5. Git repository

https://github.com/t0w5a/rx-pubsub

6. Version

0.2.1