Giter VIP home page Giter VIP logo

paritosh64ce / rxjs-pub-sub Goto Github PK

View Code? Open in Web Editor NEW
22.0 2.0 5.0 1.94 MB

:bell: 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).

License: MIT License

JavaScript 11.74% HTML 6.00% TypeScript 79.32% SCSS 2.65% CSS 0.28%
events publish-subscribe component-communication pub-sub publish subscribe javascript observables rxjs

rxjs-pub-sub's Introduction

๐Ÿ”” 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.

Build Status npm npm License: MIT PayPal Donate

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: Just for example here Angular code is show. However, you could use this library with any of your Javascript project (React, Vue, etc).

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

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.0:
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

rxjs-pub-sub's People

Contributors

dependabot[bot] avatar paritosh64ce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rxjs-pub-sub's Issues

Entry point '@pscoped/ngx-pub-sub' contains deep imports into...

Entry point '@pscoped/ngx-pub-sub' contains deep imports into

  • rxjs/internal/BehaviorSubject
  • rxjs/internal/ReplaySubject
  • rxjs/internal/Subject
  • rxjs/internal/Observable
  • rxjs/internal/Subscription
  • rxjs/internal/types

This is probably not a problem, but may cause the compilation of entry points to be out of order.

Well in my case it is a problem because I use your ngxPubSub in an Angular library which is used by another Angular App.
That angular App won't build because it complains about missing dependencies, see next error:

An error occurred during the build:
Error: The target entry-point "name of my library" has missing dependencies:

  • rxjs/internal/BehaviorSubject
  • rxjs/internal/ReplaySubject
  • rxjs/internal/Subject
  • rxjs/internal/Observable
  • rxjs/internal/Subscription
  • rxjs/internal/types

NgxPubSubModule for angular 14 typescript

I have implemented #NgxPubSubModule in my project but it was not accepted by #NgModule imports.

I am getting this error while I import #NgxPubSubModule.

**#NgxPubSubModule' does not appear to be an #NgModule class.(-996002)

ngx-pub-sub.module.d.ts(1, 22): This likely means that the library (@pscoped/ngx-pub-sub) which declares #NgxPubSubModule has not been processed correctly by ngcc, or is not compatible with Angular Ivy. Check if a newer version of the library is available, and update if so. Also, consider checking with the library's authors to see if the library is expected to be compatible with Ivy.**

What is the solution to this problem

Do not depreciate 'getEventObservable

Hello,
Thanks for your wonderful work. I see that you are deprecating the 'getEventObservable'. However, i needed an access to underlying observable since my requirement was to denounce the event. The event i am publishing is from different sources. But i would to subscribe only once in a specific time. Thats why i added a denounce functionality like below:


`    this.pubsubSvc.getEventObservable(SyncConstant.EVENT_SYNC_DATA_PUSH_COMPLETE)
      .pipe(debounceTime(300))
      .subscribe(() => {
        if(AppConstant.DEBUG) {
          console.log('ExpenseListingPage:Event received: EVENT_SYNC_DATA_PUSH_COMPLETE');
        }
    });`

I would like to know if you remove the getEventObservable method, what will be alternative to this ?? Since i dont want my application break after certain time i.e when you remove it...

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.