🚀 DAY 2 OF LAUNCH WEEK: Unify Your Security Stack with Socket Basics.Learn more →
Socket
Book a DemoInstallSign in
Socket

@fikani/ngx-pubsub

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fikani/ngx-pubsub

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

Source
npmnpm
Version
0.1.3
Version published
Weekly downloads
10
Maintainers
1
Weekly downloads
 
Created
Source

@fikani/ngx-pubsub

About

This library helps people to use a simple Publisher/Subscriber module taking advantage of the rxjs/Observable api poresent 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 your library
import { PubSubModule } from '@fikani/ngx-pubsub';

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

    // Specify your library as an import
    PubSubModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Once it is imported, you can use the subscribe decorator in your Angular components:

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

@Component({...})
export class MyComponent {
  @subscribe("test")
  myMethod(data) {
    console.log("comp1 : " + 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

Keywords

angular

FAQs

Package last updated on 14 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts