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

@yume-chan/event

v0.0.23

Published

Event/EventEmitter

Downloads

4,447

Readme

@yume-chan/event

Provides a strongly-typed EventEmitter/Event implementation.

Inspired by TypeScript, Visual Studio Code, and more.

Disposable

interface Disposable {
    dispose(): void;
}

Represents anything that need cleanup before the garbage collector recycle it.

AutoDisposable

class AutoDisposable implements Disposable {
    private disposables;
    constructor();
    protected addDisposable<T extends Disposable>(disposable: T): T;
    dispose(): void;
}

A base class for objects that need to manage multiple Disposables.

Calling dispose on it will automatically dispose all Disposables added via addDisposable.

Usually works like:

class MyObject extends AutoDisposable {
    private event1 = this.addDisposable(new EventEmitter<void>());

    private event2 = this.addDisposable(new EventEmitter<void>());

    public dispose() {
        // If the derived class has its own dispose logic
        // Don't forget to call super's `dispose`
        super.dispose();

        // Clean up itself.
    }
}

DisposableList

class DisposableList extends AutoDisposable {
    add<T extends Disposable>(disposable: T): T;
}

An AutoDisposable that can be used alone (i.e. not as a base class).

EventEmitter/Event

interface EventListener<TEvent, TThis, TArgs extends unknown[], TResult> {
    (this: TThis, e: TEvent, ...args: TArgs): TResult;
}

interface RemoveEventListener extends Disposable {
    (): void;
}

interface Event<TEvent, TResult = unknown> {
    (listener: EventListener<TEvent, unknown, [], TResult>): RemoveEventListener;
    <TThis, TArgs extends unknown[]>(listener: EventListener<TEvent, TThis, TArgs, TResult>, thisArg: TThis, ...args: TArgs): RemoveEventListener;
}

class EventEmitter<TEvent, TResult = unknown> implements Disposable {
    protected readonly listeners: EventListenerInfo<TEvent, TResult>[];
    constructor();
    protected addEventListener(info: EventListenerInfo<TEvent, TResult>): RemoveEventListener;
    event: Event<TEvent, TResult>;
    fire(e: TEvent): void;
    dispose(): void;
}

| | Node.js EventEmitter | This EventEmitter | | ----------------------------------- | ---------------------- | ------------------- | | Can emit multiple event types | Yes | No | | Only trusted source can emit events | No | Yes | | Strongly-typed | No | Yes |

One EventEmitter for one event type. So for classes that have multiple event types, multiple EventEmitter can be created and assigned to multiple fields.

Usually classes keep EventEmitters private (using TypeScript's private modifier, or ECMAScript private field), only expose the Event interface (via EventEmitter.event).

Everyone can subscribe to the event using the Event interface, but the event can only be emitted from the EventEmitter class.

const emitter = new EventEmitter<void>();
const subscribe = emitter.event;
const unsubscribe = subscribe(() => {});
emitter.fire();
unsubscribe();

The returned unsubscribe is both a function and a Disposable, so it can be used with AutoDisposable or DisposableList.