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 🙏

© 2025 – Pkg Stats / Ryan Hefner

event-ag

v1.0.6

Published

Typed event aggregator implementation for Typescript.

Readme

Event-Ag

Typed event aggregator implementation for Typescript.

Installation

Install via npm using npm i --save-dev event-ag

Usage

Creating an event

First create an event that extends PubSubBase as shown below; this event will be used as event arguments when publishing.

export default class NewUserEvent extends PubSubBase<NewUserEvent> {
  private userId: number;
  
  public get UserId: number {
    return this.userId;
  }
  
  public set PackId(value: number) {
    this.userId = value;
  }
}

Creating a Query

A query should be used when a return value is required, queries should not edit the state of the application i.e. be free of side effects.

To create a query, create a class that extends Query as shown below; this query class will represent return type of the query.

export default class UserQuery extends Query<UsernameQuery> {
  private user: IUser;

  public get User: IUser {
    return this.user;
  }

  public set User(value: IUser){
    this.user = value;
  }
}

Args can be passed when making a query, to do this, create an interface that extends the QueryArgs interface.

export interface UserQueryArgs implements QueryArgs<TestQuery> {
  Username: string;
}

The Event Aggregator

The EventAggregator is responsible for managing all of the available events, events are added to the EventAggregator the first time GetEvent is called with a new event. To get started with using the EventAggregator, create an instance of EventAggregator, then get the newly created PubSubBase event from the EventAggregator.

const eventAggregator = new EventAggregator();
const newUserEvent = eventAggregator.GetEvent(NewUserEvent);

Subscribing to events

Events can be subscribed to using the following method.

const eventAggregator = new EventAggregator();
const newUserEvent = eventAggregator.GetEvent(NewUserEvent);

newUserEvent.Subscribe((eventArgs: NewUserEvent) => console.log(`New user - UserId: ${eventArgs.UserId}`);

Publishing events

To publish an event, an instance of the event must be passed as event arguments.

const eventAggregator = new EventAggregator();
const newUserEvent = eventAggregator.GetEvent(NewUserEvent);

newUserEvent.Subscribe((eventArgs: NewUserEvent) => console.log(`New user - UserId: ${eventArgs.UserId}`);
newUserEvent.Publish({UserId: 0} as NewUserEvent);

Handling Queries

Unlike events, a query can only have one handler. A function can be registered as a handler of a query with the following code.

  const eventAggregator = new EventAggregator();
  eventAggregator.GetQuery(UserQuery).Handle((query: QueryArgs<UserQuery> | undefined) => {
    const userQueryArgs = query as UserQueryArgs;
    const user = // Code to find user with **userQuery.Username** goes here.
    return {
      User: user
    } as UserQuery;
  });

NOTE: parameter of handler must inlude Query<T> | undefined as query args may not always be passed.

Executing Queries

Executing a query is similar to publishing an event.

  const userQuery = eventAggregator.GetQuery(UserQuery);

  const user = userQuery.Execute({
    Username: 'test_username'
  } as UserQueryArgs);