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

@pscoped/rxjs-pub-sub

v1.0.1

Published

Event publish - subscribe mechanism as JavaScript library using Observable. You can publish your event along with any data to all the subscribers of your event. This library also supports historical published values for the subscribers.

Downloads

28

Readme

🔔 rxjs-pub-sub 🔔

Event publish - subscribe mechanism as Javascript library using Observable. You can publish your event along with any data to all the subscribers of your event (event identification is being done using event-name as string). This library also supports historical published values for the new subscribers. This library can work with any of your JavaScript code. You just need RxJs along with it.

npm npm License: MIT PayPal Donate

Build Status Static Badge

Live Demo Link

What makes this package special?

  1. Simplicity

    • Publish you data
    rxjsPubSub.publishEvent('eventName', data)
    • Subscribe to your event
    rxjsPubSub.subscribe('eventName', (data: any) => { /* your callback */ })
  2. Unique feature

    • This service also supports historical values even for new subscribers.
    rxjsPubSub.publishWithHistory('eventName', data)   /* new subscribers can have historical values */
    rxjsPubSub.publishWithLast('eventName', data)      /* new subscribers can have last published values */

How to use

  1. Install the package.

    npm i @pscoped/rxjs-pub-sub --save

    I had to scope ( @pscoped ) my package with something, because another package having similar name was already published.

    • Import the service in your project; be it Angular, React, Vue, or even Vanilla JavaScript code
    import { rxjsPubSub } from '@pscoped/rxjs-pub-sub';
  2. Register the events if you'd like to support events with last or historical values.

    const latestEvent = 'randomLast';
    const historicalEvent = 'randomHistory';
    
    rxjsPubSub.registerEventWithHistory(historicalEvent, 6);
    rxjsPubSub.registerEventWithLastValue(latestEvent, undefined);
  3. Use rxjsPubSub and subscribe to your event.

    export class SubscriberComponent implements OnDestroy {
           
        subscription1: Subscription;
        subscription2: Subscription;
        subscription3: Subscription;
        myNumber1: number;
        myNumber2: number;
        myNumber3: number;
    
        constructor() { }
    
        ngOnInit() {
            this.subscription1 = rxjsPubSub.subscribe('randomNormal', data => this.myNumber1 = data);
            this.subscription2 = rxjsPubSub.subscribe('randomHistory', data => this.myNumber2 = data);
            this.subscription3 = rxjsPubSub.subscribe('randomLast', data => this.myNumber3 = data);
        }
    
        ngOnDestroy() {
            this.subscription1.unsubscribe();
            this.subscription2.unsubscribe();
            this.subscription3.unsubscribe();
        }
    }
  4. And publish the event.

    export class PublisherComponent {
    
        normalEvent = 'randomNormal';
        historicalEvent = 'randomHistory';
        latestEvent = 'randomLast';
    
        random: number;
        constructor() { }
    
        publish() {
            this.random = Math.floor(Math.random() * 100);
    
            rxjsPubSub.publishEvent(this.normalEvent, this.random);
            rxjsPubSub.publishWithHistory(this.historicalEvent, this.random);
            rxjsPubSub.publishWithLast(this.latestEvent, this.random);
        }
    }

Note: Here Angular code is shown just for the sake of an example. You could use this library with any of your Javascript project (React, Vue, etc) including vanilla JavaScript and TypeScript.

Ground Rules

Note: Here normal event means event's data will be vanished if no subscriber is there at the time of publishing the event. Historical values or last value will not be provided to the subscribers for such events.

  1. An event has to be registered if last value or historical values have to be supported.
  2. Once event name is registered for a type (to support either normal, last value support or historical value support), the same name cannot be used to publish/subscribe for another type unless it is completed by someone.
  3. Normal events need not to be registered. If event is not found at the time of publishing or subscribing, the same will be registered as a normal event.
  4. You can register the events anywhere in your code, however, we recommand to have it at one place only, i.e. inside the root component of your application, like what you see in app.component.ts

If an event having name 'randomHistory' is registered to support historical values, the same event name cannot be used to register or publish event with other type (i.e. last value support or normal event) unless it is completed programmatically.

Below is how the demo application looks like.

Demo Screenshot
@pscoped/ngx-pub-sub or @pscoped/rxjs-pub-sub - both's demo apps are kind of same

About the library

  • Motivation: https://www.npmjs.com/package/@pscoped/ngx-pub-sub
    • This library has been used by many Angular developers.
    • I wanted to make it available to even broader audience.

Developing and Contributing

The repository also comes with the demo application. Check the Github repo link.

Development server

git clone https://github.com/paritosh64ce/rxjs-pub-sub.git
cd rxjs-pub-sub
npm i
npm start

This will start the server for the demo application.

Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

Running unit tests

  1. Run npm run test:lib to execute the rxjs-pub-sub library test cases.
  2. Run npm run coverage:lib to generate the code-coverage report.

Make sure to update the tests if you submit a PR, the CI will be affected if any of the tests fails.

This project was generated with Angular CLI using Nrwl Nx.

TODO:

  1. Coverage badge for README
  2. Lint integration

Change Log

0.0.1 - 1.0.1:
Basic functionality from @pscoped/ngx-pub-sub and README file updates

Like this work? Star this repository on GitHub

Support

Donate

Motivate, Become a sponsor and get your logo on README with a link to your site. Become a sponsor

Got any issue or some feature request on your mind? Post it here!

License

MIT @ paritosh64ce