GithubHelp home page GithubHelp logo

ngx-pubsub's Introduction

@fikani/ngx-pubsub

About

This library helps people to use a simple Publisher/Subscriber module taking advantage of the rxjs/Observable api present in Angular 2+.

Installation

To install this library, run:

$ npm install @fikani/ngx-pubsub --save

Usage

Add the PubSubModule to your Angular AppModule:

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";

import { AppComponent } from "./app.component";

// Import the module
import { PubSubModule } from "@fikani/ngx-pubsub";

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,

    // import here
    PubSubModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

Once it is imported, you can use the subscribe decorator in your Angular components. It works with: class members (including arrow functions), set property acessor and class methods.

import { Component, OnInit } from '@angular/core';
import { subscribe } from '@fikani/ngx-pubsub';

@Component({...})
export class MyComponent {
  @subscribe("test") myProp: string;

  @subscribe("test")
  handler = (data: any) => {
    console.log("arrow function", data);
  };

  @subscribe("test")
  set todo(data: any) {
    console.log("todo", data);
  }

  @subscribe("test")
  onTest(data: any) {
    console.log(this, "Event: test => ", data);
  }
}

Don't worry about unsubscribing the observables on components, it is done under the hood by using the component's lifecyle hooks. You also have to emit the notification to the observers. To do so, use the SubscriptionService:

import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/interval";
import { SubscriptionService } from "@fikani/ngx-pubsub";

@Injectable()
export class PublisherService {
  test$ = this.sub.subject("test").subscribe(data => {
    console.log("service: " + data);
  });
  constructor(private sub: SubscriptionService) {
    //example
    Observable.interval(1000).subscribe(n => {
      this.sub.emit("test", n);
    });
  }
}

License

MIT © Afif Fikani

ngx-pubsub's People

Contributors

fikani avatar berthertogen avatar dependabot[bot] avatar

Stargazers

Mateus Gondim avatar

Watchers

James Cloos avatar  avatar

ngx-pubsub's Issues

Upgrade rxjs package to ^6.0.0

We are using rxjs version^6.0.0, everything seems to be working fine after a minor change:

In subscription.service.ts the import statement is changed to:

import { Subject } from "rxjs";

Is it ok I make a PR for this?

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.