2.1.0 • Published 3 years ago

tiny-typed-emitter v2.1.0

Weekly downloads
4,373
License
MIT
Repository
github
Last release
3 years ago

tiny-typed-emitter

Have your events and their listeners type-checked with no overhead.

npm version

Install

Simply add the dependency using npm:

$ npm i tiny-typed-emitter

or using yarn:

$ yarn add tiny-typed-emitter

Usage

  1. import tiny-typed-emitter library:

    import { TypedEmitter } from 'tiny-typed-emitter';
  2. define events and their listener signatures (note: quotes around event names are not mandatory):

    interface MyClassEvents {
      'added': (el: string, wasNew: boolean) => void;
      'deleted': (deletedCount: number) => void;
    }
  3. on this step depending on your use case, you can:

  • define your custom class extending EventEmitter:
    class MyClass extends TypedEmitter<MyClassEvents> {
      constructor() {
        super();
      }
    }
  • create new event emitter instance:
    const emitter = new TypedEmitter<MyClassEvent>();

Generic events interface

To use with generic events interface:

interface MyClassEvents<T> {
  'added': (el: T, wasNew: boolean) => void;
}

class MyClass<T> extends TypedEmitter<MyClassEvents<T>> {

}

Compatible subclasses with different events

The type of eventNames() is a superset of the actual event names to make subclasses of a TypedEmitter that introduce different events type compatible. For example the following is possible:

class Animal<E extends ListenerSignature<E>=ListenerSignature<unknown>> extends TypedEmitter<{spawn: () => void} & E> {
  constructor() {
    super();
  }
}

class Frog<E extends ListenerSignature<E>> extends Animal<{jump: () => void} & E> {
}

class Bird<E extends ListenerSignature<E>> extends Animal<{fly: () => void} & E> {
}

const animals: Animal[] = [new Frog(), new Bird()];

No Overhead

Library adds no overhead. All it does is it simply reexports renamed EventEmitter with customized typings. You can check lib/index.js to see the exported code.

@bleah/discord-player@dysolix/hasagi@dysolix/lca@crawlee/browser-poolchinjowwchinjowbyondxr-viewer@casper124578/discord-playerdaydreamcmds-devdaydream-cmds@team-reflect/pixi-graphdaydreamcmds@jayxie/moderation@jayxie/music@jayxie/utilitydiscord-player-to-play-dldiscordjs-twitchdiscord-moderationnew-discord-leveling@infinitebrahmanuniverse/nolb-tiny-t@myno_21/time@ndml/dldiscord-player-play-dldiscord-applicationdiscord-application-launcherreact-hook-rss@djs-modules/giveaways@djs-modules/leveling@djs-modules/moderation@djs-modules/notifierlavalink.tsdiscord-player-now@everything-registry/sub-chunk-2949@apeiromont/opshop-server@bd-js/interpreterxiomi-leto-messaging@duinoapp/files-multitool@duxcore/interactive-discordyee-tswatched-objectwater-playerwebsocket13websocket13-jsonrpcwhatsapp-cloud.jswebauthn4jstora-protocoltowerverse.jsthreadzthrottle-batch-sizeticket-libtimeouts.ts-devtransaction-managerttstk-channelstwitchlib-chat@friendlyss/collection@friendlyss/disclosure@friendlyss/loaders@factoshi/zeetl@garzj/event-subscriberuimixvirtual-printervantjstrixi@inrixia/threads@hasagi/core@hasagi/extended@interval/server@homebridge-eufy-security/eufy-security-clientsysbot-net-api@eriengine/plugin-actor@eriengine/plugin-inventory@eriengine/plugin-isometric-cursor@gogovega/firebase-config-nodediscord-media-playerradar4mobrawrlinkreflect-nexusrextuberhino.dbrgb-navigation-apirosectionserialport-synchronoussamp-utilsshare.ioshuke.authlibsoroush.jssqlite_master.dbsteam-sessionstaboatstart-stop-state-machinesuperreactive@modum-io/modblue@minhdtb/pms-timeline@ones-table/server@ones-table/table@ones-editor/editor@mapeo/core@markxroberts/risco-lan-bridge@keiryo/common@keiryo/rest
2.1.0

3 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago