Coder Social home page Coder Social logo

riteshwaghela / ngx-pub-sub Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paritosh64ce/rxjs-pub-sub

0.0 1.0 0.0 186 KB

Event publish - subscribe mechanism as Angular service 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.16% HTML 10.52% TypeScript 74.54% CSS 3.78%

ngx-pub-sub's Introduction

NgxPubSub

Event publish - subscribe mechanism as Angular service 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).

Build Status npm npm npm

Dependency Status devDependency Status

How to use

Install the module.

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

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

Import NgxPubSub module into your module

import { NgxPubSubModule } from '@pscoped/ngx-pub-sub';

@NgModule({
....
imports: [
    .....
    NgxPubSubModule
],
....
})
export class AppModule {}

Use NgxPubSubService and subscribe to your event.

export class SubscriberComponent implements OnDestroy {
  
  subscription: Subscription;
  myNumber: number;

  constructor(private pubSub: NgxPubSubService) { }

  ngOnInit() {
    this.subscription = this.pubSub.getEventObservable('randomNumber')
      .subscribe(data => this.myNumber = data);
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}

And publish the event.

export class PublisherComponent {

  eventName = 'randomNumber';
  random: number;
  constructor(private pubsub: NgxPubSubService) { }

  publish() {
    this.random = Math.floor(Math.random() * 100);
    this.pubsub.publishEvent(this.eventName, this.random);
  }
}

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

Development server

After having the source code, run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

Below is how the demo application looks like.

Demo Screenshot

Running unit tests

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

This project was generated with Angular CLI using Nrwl Nx.

License

MIT @ paritosh64ce

ngx-pub-sub's People

Contributors

paritosh64ce avatar

Watchers

Ritesh 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.