{"id":16625804,"url":"https://github.com/aleclarson/ee-ts","last_synced_at":"2025-04-09T14:08:44.780Z","repository":{"id":56616532,"uuid":"148246606","full_name":"aleclarson/ee-ts","owner":"aleclarson","description":"Type-safe, isomorphic event emitters","archived":false,"fork":false,"pushed_at":"2022-08-19T17:35:47.000Z","size":181,"stargazers_count":125,"open_issues_count":8,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-05-01T22:24:23.229Z","etag":null,"topics":["event-emitter","events","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aleclarson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-11T02:09:31.000Z","updated_at":"2023-09-09T18:54:38.000Z","dependencies_parsed_at":"2022-08-15T22:00:54.218Z","dependency_job_id":null,"html_url":"https://github.com/aleclarson/ee-ts","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aleclarson%2Fee-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aleclarson%2Fee-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aleclarson%2Fee-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aleclarson%2Fee-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aleclarson","download_url":"https://codeload.github.com/aleclarson/ee-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247625891,"owners_count":20969196,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["event-emitter","events","typescript"],"created_at":"2024-10-12T04:07:18.431Z","updated_at":"2025-04-09T14:08:44.752Z","avatar_url":"https://github.com/aleclarson.png","language":"TypeScript","funding_links":["https://paypal.me/alecdotbiz"],"categories":["Built with TypeScript"],"sub_categories":["Libraries"],"readme":"# ee-ts\n\n[![npm](https://img.shields.io/npm/v/ee-ts.svg)](https://www.npmjs.com/package/ee-ts)\n[![Build status](https://travis-ci.org/aleclarson/ee-ts.svg?branch=master)](https://travis-ci.org/aleclarson/ee-ts)\n[![codecov](https://codecov.io/gh/aleclarson/ee-ts/branch/master/graph/badge.svg)](https://codecov.io/gh/aleclarson/ee-ts)\n[![Bundle size](https://badgen.net/bundlephobia/min/ee-ts)](https://bundlephobia.com/result?p=ee-ts)\n[![Install size](https://packagephobia.now.sh/badge?p=ee-ts)](https://packagephobia.now.sh/result?p=ee-ts)\n[![Code style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/alecdotbiz)\n\nType-safe event emitters (for TypeScript)\n\n### Features\n\n- strict event names\n- type-checking for emitted data\n- flexible `listeners()` generator method\n- add/remove listeners during emit\n- great for sub-classing\n- one-time listeners\n- default handlers\n\n\u0026nbsp;\n\n### Example\n\n```ts\nimport { EventEmitter as EE } from 'ee-ts'\n\ntype User = { name: string }\n\n// All possible events must be explicitly defined as methods here.\n// The return type can be non-void because the `emit` method returns the last non-void value.\n// The return type can never be required, because `void` is implicitly added to every event.\ninterface Events {\n  login(user: User): void\n  logout(): string\n}\n\n// Make your subclass generic to let users add their own events.\nclass App\u003cT = {}\u003e extends EE\u003cT \u0026 Events\u003e {\n  // You _cannot_ emit user-added events from here, though.\n  someMethod(this: App) {\n    this.emit('logout')\n  }\n}\n\ntype UserEvents = { test(): void }\nlet app = new App\u003cUserEvents\u003e()\n\n// Emit your custom event.\napp.emit('test')\n\n// The type of `user` is inferred.\napp.on('login', user =\u003e {\n  console.log(user.name) // user.name is string\n})\n\n// Invalid argument types are caught.\napp.one('login', (invalid: boolean) =\u003e {}) // [ts] Type 'User' is not assignable to type 'boolean'.\n\n// Invalid return values are caught.\napp.one('logout', () =\u003e true) // [ts] Type 'boolean' is not assignable to type 'string | void'.\n\n// Unknown event names are caught.\napp.emit('invalid') // [ts] Argument of type '\"invalid\"' is not assignable to parameter of type '\"login\" | \"logout\"'.\n```\n\n\u0026nbsp;\n\n### Subclassing\n\nThis library was designed with subclassing in mind.\n\n- The internal cache is non-enumerable\n- Few public methods: `on`, `one`, `off`, `emit`, `listeners`\n- Override `_onEventHandled(key: string)` to know when an event has at least one listener\n- Override `_onEventUnhandled(key: string)` to know when an event has no listeners\n\n\u0026nbsp;\n\n### Disposables\n\nWhen you pass an array as the last argument of `on`, `one`, or `EE.unhandle`,\nan object is pushed onto it. This object has a `dispose(): void` method, which\nyou should call to remove the associated listener from its event.\n\nThis is a useful way of grouping listeners together.\n\n```ts\nimport { EventEmitter, Disposable } from 'ee-ts'\n\nconst ee = new EventEmitter\u003c{ foo(): void }\u003e()\n\nconst disposables: Disposable[] = []\n\nlet count = 0\nconst fn = ee.on('foo', () =\u003e count++, disposables)\n\nassert(disposables.length == 1)\n\ndisposables[0].dispose()\nee.emit('foo')\n\nassert(count == 0)\n```\n\n\u0026nbsp;\n\n## API Reference\n\nThe type signatures below are _not_ 100% accurate. They're here to give you a general idea of the API. Find the real type signatures in [the source code](./src/ee.ts) or [VS Code](https://code.visualstudio.com/docs/editor/intellisense).\n\n\u0026nbsp;\n\n#### `on(key: string, fn: Function, disposables?: Disposable[]): Function`\n\nAdd a listener to the given event key.\n\nUse the `one` method to add a one-time listener.\n\n_Returns:_ the `fn` argument\n\n\u0026nbsp;\n\n#### `on(map: { [key: string]: Function }, disposables?: Disposable[]): this`\n\nAdd every listener value to its associated event key.\n\nUse the `one` method to add one-time listeners.\n\n\u0026nbsp;\n\n#### `off(key: string, fn?: Function): this`\n\nRemove a listener for the given event key.\n\nOmit the `fn` argument to remove all listeners for the given event key.\n\nCall `off('*')` to remove all listeners for every event key.\n\n\u0026nbsp;\n\n#### `emit(key: string, ...args: any[]): any`\n\nEmit an event to listeners associated with the given event key.\n\nYou can safely add/remove listeners from inside a listener.\n\n_Returns:_ last non-void value returned by a listener\n\n\u0026nbsp;\n\n#### `listeners(key: string): IterableIterator\u003cFunction\u003e`\n\nCreate a generator of the listeners for the given event key.\n\nUse this with `for..of` or spread it into an array. Read more about generators [here](https://medium.com/javascript-scene/the-hidden-power-of-es6-generators-observable-async-flow-control-cfa4c7f31435).\n\n\u0026nbsp;\n\n## Static methods\n\n\u0026nbsp;\n\n#### `unhandle(ee: EventEmitter, key: string, fn: Function, disposables?: Disposable[]): Function`\n\nSet the default handler for an event key.\n\nThe default handler is called when no other listeners exist for the same event key.\n\n\u0026nbsp;\n\n#### `keys(ee: EventEmitter): string[]`\n\nGet an array of event keys that have listeners.\n\n\u0026nbsp;\n\n#### `count(ee: EventEmitter, key: string): number`\n\nGet the number of listeners an event has.\n\n\u0026nbsp;\n\n#### `has(ee: EventEmitter, key: string): boolean`\n\nCheck if an event has listeners.\n\n_Returns:_ true when the given event key has `\u003e= 1` listener.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faleclarson%2Fee-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faleclarson%2Fee-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faleclarson%2Fee-ts/lists"}