@peerlancers/ngx-event-bus
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

NgxEventBus

An angular library for dispatching event to easily communicate between component-component, service-component, and many more.

Install

npm install @peerlancers/ngx-event-bus

or

yarn add @peerlancers/ngx-event-bus

Usage

Installation

import { EventBusModule } from '@peerlancers/ngx-event-bus';

@NgModule({
  imports: [
    EventBusModule.forRoot(),
  ]
})

Creation of custom event. Inherit the EventBusState class

import { EventBusState } from '@peerlancers/ngx-event-bus';

export class ShowLoaderEvent extends EventBusState<void> {
  constructor() {
    super('ShowLoaderEvent');
  }
}

Register event listener

import { Subscription } from 'rxjs';
import { EventBusDispatcherService } from '@peerlancers/ngx-event-bus';

export class EventClassSample {
  private _showLoaderHandler: Subscription;

  constructor(private _eventDispatcher: EventBusDispatcherService) {
    this._showLoaderHandler = this._eventDispatcher.addEventListener(
      new ShowLoaderEvent(), this._onShowLoader.bind(this)
    );
  }

  public ngOnDestroy(): void {
    if (this._showLoaderHandler) {
      this._showLoaderHandler.unsubscribe();
      this._showLoaderHandler = null;
    }
  }

  private _onShowLoader(): void {
    console.log('Show loader.');
  }
}

Note don't forget to unsubscribe your handler, to prevent memory leak.

Dispatching an event

import { EventBusDispatcherService } from '@peerlancers/ngx-event-bus';

export class EventClassSample {
  constructor(private _eventDispatcher: EventBusDispatcherService) {
    this._eventDispatcher.dispatch(new ShowLoaderEvent());
  }
}

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @peerlancers/ngx-event-bus

Weekly Downloads

1

Version

1.0.5

License

none

Unpacked Size

179 kB

Total Files

38

Last publish

Collaborators

  • apascual0511