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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@swindle/event-emitter

v1.0.6

Published

An event emitter implementation for typescript.

Readme

Swindle Event Emitter

An event emitter implementation for TypeScript.

Installation

To install Swindle Event Emitter, use the following command with npm:

npm i @swindle/event-emitter

or with Yarn:

yarn add @swindle/event-emitter

Usage

Creating the Emitter

To create our event emitter, we instanciate the EventEmitter class.

import { EventEmitter } from "@swindle/event-emitter";

const emitter = new EventEmitter();

The EventEmitter class takes some optional arguments to customize its behavior. subscribers: The subscribers argument is an array of Subscriber objects that will serve as the initial event listeners. onBeforeHandlersExecute: An asynchronous function to be executed before the event handlers are run. onAfterHandlersExcecute: An asynchronous function to be executed after all the event handlers are executed. onHandlerError: An asynchronous function to be executed when a handler encounters an error.

Emitting an Event

To emit an event, we use the EventEmitter class' emit() method.

await emitter.emit(new SomeEvent(someArguments));

The emit() will accept an instance of an event, and execute the relevant listeners (Subscribers).

Defining an Event

To define an event, we extend the CompositeEvent class.

import { CompositeEvent } from "@swindle/event-emitter";
import { DateTime } from "@swndle/core";

export class UserCreated extends CompositeEvent {

    constructor(occuredOn: DateTime = DateTime.Now(), id: string|undefined = undefined) {
        super(occuredOn, id);
    }
}

The CompositeEvent class takes two arguments. The first is the timestamp of the event. Here, we set a default to the current datetime for when the event is instanciated. The second argument is the unique ID for every instance of the event. We can either set this to a string value. However, if we set it to undefined, a unique ID will be generated for us every time the event is instanciated.

Our next step is to define the event name. To do this, we override the CompositeEvent.EventName() static method.

import { CompositeEvent } from "@swindle/event-emitter";
import { DateTime } from "@swndle/core";

export class UserCreated extends CompositeEvent {

    constructor(occuredOn: DateTime = DateTime.Now(), id: string|undefined = undefined) {
        super(occuredOn, id);
    }

    public static EventName(): string {
        return "user-created";
    }
}

Here, we define an event name as user-created. You can name events whatever you like. However, the recommended convention is to use paramcase format (i.e. event-name).

The last requirement is to override the serializeData() method to specify how we want our event data to be serialized.

import { CompositeEvent } from "@swindle/event-emitter";
import { DateTime } from "@swndle/core";

export class UserCreated extends CompositeEvent {

    constructor(occuredOn: DateTime = DateTime.Now(), id: string|undefined = undefined) {
        super(occuredOn, id);
    }

    public static EventName(): string {
        return "user-created";
    }

    public serializeData(): string {
        return JSON.stringify({
            // serialize event data here.
        });
    }
}

Once this is complete, we can define the event however we want. Below is a sample implementation of a UserCreated event.

import { CompositeEvent } from "@swindle/event-emitter";
import { DateTime } from "@swndle/core";
import { User } from "./path/to/user/model";

export class UserCreated extends CompositeEvent {

    private createdUser: User;

    constructor(user: User, occuredOn: DateTime = DateTime.Now(), id: string|undefined = undefined) {
        super(occuredOn, id);
        this.createdUser = user;
    }

    public static EventName(): string {
        return "user-created";
    }

    public user(): User {
        return this.createdUser;
    }

    public serializeData(): string {
        return JSON.stringify({
            user: this.user().serialize()
        });
    }
}

Event Subscribers

Event Subscribers are objects that execute in response to events. To define an event subscriber, we use the Subscriber class.

import { Subscriber, SubscriberId } from "@swindle/event-emitter';

const sub = new Subscriber(
    SubscriberId.Generate(),
    "some-event",
    2,
    "print-event-content",
    async (event: CompositeEvent): Promise<void> => {
        constole.log(event.serialize());
    },
    false
);

The Subscriber class takes five arguments. subscriberId: The unique id for the event subscriber. eventName: The name of the event we want to subscribe to. priority: The priority of the event subscriber. The lower the priority, the earlier the subscriber will be executed. handler: The event handler function to execute. stopPropogationOnError: A flag to indicate whether or not event propogation to subsequent subscribers should stop if this subscriber's handler function throws an error. By default, it is set to false.

To add a subscriber, we use the addSubscriber() method.

emitter.addSubscriber(subscriber);

To remove a subscriber, we use the removeSubscriber() method.

emitter.removeSubscriber(subscriber);