npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-event-broker

v1.1.3

Published

EventBrokerService provide application level communication via events.

Downloads

166

Readme

Angular Event Broker Service

Introduction

In Angular application, we often require to pass data and messages between components and classes. If parent/child component hierarchy is present then we can pass data using @Input() and @Output() decorators. If not then we can create custom event service and pass events/data. This library exports EventBrokerService which is available through application level and can be injected in any declarable classes. Using EventBrokerService application events can flow in streamline fashion without mixing component's logic with event handling and publishing code.

Setup

  • Install library in your Angular project using command npm install --save ng-event-broker

  • Import EventBrokerModule from ng-event-broker library

import { EventBrokerModule } from 'ng-event-broker';

@NgModule({
  declarations: [
  ],
  imports: [
    EventBrokerModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
  • Define your application level events in separate file events.model.ts. For example,
export const Events = {
  loginFailed: 'loginFailed',
  loginSuccessful: 'loginSuccessful',
  logout:'logout',
  productAdded:'productAdded',
  productRemoved:'productRemoved',
  cartCountUpdated: 'cartCountUpdated'
}
  • Register application wide events in ngOnInit() of AppComponent. Before publishing or subscribing to any events, they must be registered. As AppComponent is root component, we should define our application wide events here.

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  constructor(private eventService: EventBrokerService) {
  }

  ngOnInit() {
    this.registerAppEvents();
  }

  private registerAppEvents() {
    this.eventService.registerEvent(Events.loginSuccessful);
    this.eventService.registerEvent(Events.logout);
    this.eventService.registerEvent(Events.productAdded);
    this.eventService.registerEvent(Events.productRemoved);
    this.eventService.registerEvent(Events.cartCountUpdated);
  }
  • Once events are registered, we can publish and subscribe them in rest of application. For example, in below code HeaderComponent subscribe to event cartCountUpdated. Whenever this event is published, onCartUpdated() method will be called. Similarly on Logout button click, HeaderComponent will publish logout event for subscribers to handle it.
<li><a href="javascript:void(0);" (click)="logout()">Logout</a></li>

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {

  cartItemCount = 0;

  constructor(private eventService: EventBrokerService) { }

  ngOnInit() {
    this.eventService.subscribeEvent(Events.cartCountUpdated).subscribe((cartCount: number) => this.onCartUpdated(cartCount))
  }

  onCartUpdated(cartCount: number) {
    this.cartItemCount = cartCount;
  }

  logout() {
    this.eventService.publishEvent(Events.logout);
  }

}

Publish event

To publish event, simply call publishEvent(payload) where paylaod can be value of type any.

  this.eventService.publishEvent(Events.logout);

Make sure, event is registered first using registerEvent() before publishing.

Subscribe event

To subscribe event, simply call subscribeEvent() function which returns EventEmitter<any>. You can call subscribe() on this event emitter and handle event.

this.eventService.subscribeEvent(Events.cartCountUpdated).subscribe((cartCount: number) => this.onCartUpdated(cartCount))
  }

Clear all events

We can clear all events using clearEvents() function.

Basic eShop example with EventBrokerService

You can find complete source code of very basic eShop demo application and see how events are passed and handled throughout application.

https://github.com/ultrasonicsoft/ng-event-broker-service-example

Demo

You can play around with Demo here.

Contribution

Feel free to contribute to this project by raising PR in GitHub repository.