{"id":21069079,"url":"https://github.com/styx11/emitter","last_synced_at":"2026-07-09T07:31:16.417Z","repository":{"id":57188026,"uuid":"179495175","full_name":"Styx11/emitter","owner":"Styx11","description":"🚀 a tiny but awesome event emitter","archived":false,"fork":false,"pushed_at":"2019-06-15T11:37:16.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-20T21:40:29.303Z","etag":null,"topics":["event","event-bus","event-emitter","node-api","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/Styx11.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":"2019-04-04T12:41:57.000Z","updated_at":"2019-06-15T11:37:18.000Z","dependencies_parsed_at":"2022-08-28T13:00:33.027Z","dependency_job_id":null,"html_url":"https://github.com/Styx11/emitter","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Styx11%2Femitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Styx11%2Femitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Styx11%2Femitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Styx11%2Femitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Styx11","download_url":"https://codeload.github.com/Styx11/emitter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243510062,"owners_count":20302297,"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","event-bus","event-emitter","node-api","typescript"],"created_at":"2024-11-19T18:31:41.272Z","updated_at":"2025-10-30T14:52:08.198Z","avatar_url":"https://github.com/Styx11.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Emitter\n\n\u003e a tiny but awesome event emitter\n\n![](https://img.shields.io/npm/types/awesome-emitter.svg) ![](https://img.shields.io/npm/v/awesome-emitter.svg) ![](https://img.shields.io/npm/dt/awesome-emitter.svg) ![](https://img.shields.io/github/license/Styx11/emitter.svg)\n\n## Installation\nInside your project folder do:\n```\nnpm install --save awesome-emitter\n```\n\n## Usage\nImport the `Emitter` and use its instance as a global object so that you can listen or emit a event everywhere\n```js\nimport Emitter from 'awesome-emitter';\n\nconst emitter = new Emitter();\n```\n\nAdds the `listener` function to the end of the listeners array for the event named `eventName`.\n```js\nemitter\n  .on('event1', msg =\u003e console.log(msg))\n  .once('event2', msg =\u003e console.log(msg));\n```\nReturns a reference to the `Emitter`, so that calls can be chained.\n\nThen you can `emit` the listener with arguments\n```js\nemitter.emit('event1', 'hello, world');\n```\n\nBenefit from polymorphic `this` types, you can create your own emitter class by extends `Emitter`:\n```js\nclass MyEmitter extends Emitter {\n  foo (msg: string): this {\n    console.log(msg);\n    return this;\n  };\n}\n\nconst emitter = new MyEmitter();\nemitter\n  .on('event', () =\u003e console.log('emitter'))\n  .foo('my emitter')\n  .emit('event');\n```\nIn your own emitter methods, return reference to `this`, so that calls can be chained.\n\n## API\n* [event: 'newListener'](#event-newListener)\n* [event: 'removeListener'](#event-removeListener)\n* [emitter.on(eventName, listener)](#emitteroneventname-listener)\n* [emitter.listenerCount(eventName)](#emitterlistenercounteventname)\n* [emitter.eventNames()](#emittereventNames)\n* [emitter.once(eventName, listener)](#emitteronceeventname-listener)\n* [emitter.emit(eventName[, ...args])](#emitteremiteventname-args)\n* [emitter.off([eventName[, listener]])](#emitteroffeventname-listener)\n\n### event: 'newListener'\n* `eventName` { string } The name of the event being listened for\n* `listener` { Function } The event handler function\n\nThe `Emitter` instance will emit its own `'newListener'` event *before* a listener is added to its internal array of listeners.\n\nListeners registered for the `'newListener'` event will be passed the event name and a reference to the listener being added.\n\nThe fact that the event is triggered before adding the listener has a subtle but important side effect: any additional listeners registered to the same `name` *within* the `'newListener'` callback will be inserted *before* the listener that is in the process of being added.\n```js\nconst cbOrder: Array\u003cstring\u003e = [];\nemitter.once('newListener', (eventName: string): void =\u003e {\n  if (eventName !== 'event') return;\n  emitter.on('event', () =\u003e {\n    cbOrder.push('B');\n  });\n});\nemitter.on('event', () =\u003e {\n  cbOrder.push('A');\n});\nemitter.emit('event');\nassert.deepStrictEqual(cbOrder, ['B', 'A']);// ok\n```\nNote that listen 'newListener' itself won't call the listener.\n\n### event: 'removeListener'\n* `[eventName]` { string } The name of the event\n* `[listener]` { Function } The event handler function\nThe `'removeListener'` event is emitted *after* the listener is removed.\n\nNote that:\n* off 'removeListener' itself won't call the listener function.\n* if you use `off()` without params, `'removeListener'` event will not be emitted.\n* if you use `off()` with `eventName` only, it will be provided in listener function.\n* otherwise, both of them will be provided.\n\n### emitter.on(eventName, listener)\n* `eventName` { string | Array\\\u003cstring\\\u003e } The name of the event\n* `listener` { Function } The callback function\n* Returns { Emitter }\n\nAdds the `listener` function to the end of the listeners array for the event named `eventName`. No checks are made to see if the listener has already been added. \n\n```js\nemitter\n  .on('event1', cb)\n  .on('event1', cb);\n```\nMultiple calls passing the same combination of `eventName` and `listener` will result in the `listener` being added, and called, multiple times.\n\nNote that when a listener is called, the standard `this` keyword is intentionally set to reference the `Emitter` instance to which the `listener` is attached.\n```js\nemitter.on('event', function () {\n  console.log(this === emitter \u0026\u0026 this instanceof Emitter);\n});\n\nemitter.emit('event')// Print: true\n```\n\nIt is possible to use ES6 Arrow Functions as listeners, however, when doing so, the `this` keyword will no longer reference the Emitter instance:\n```js\nemitter.on('event', () =\u003e {\n  console.log(this);\n});\n\nemitter.emit('event')// Print: {}\n```\n\n### emitter.listenerCount(eventName)\n* `eventName` { string } The name of the event\n* Returns { number }\n\nReturns the number of listeners listening to the event named `eventName`.\n\n### emitter.eventNames()\n* Returns { Array\\\u003cstring\\\u003e }\n\nReturns an array listing the events for which the emitter has registered listeners. The values in the array will be strings.\n```ts\nlet result: Array\u003cstring\u003e;\nconst events: Array\u003cstring\u003e = ['event1', 'event2', 'event3'];\n\nresult = emitter.eventNames();\nassert.ok(Array.isArray(result));\nassert.deepStrictEqual(result, events);\n```\n\n### emitter.once(eventName, listener)\n* `eventName` { string | Array\\\u003cstring\\\u003e } The name of the event\n* `listener` { Function } The callback function\n* Returns { Emitter }\n\nAdds a **one-time** `listener` function for the event named `eventName`. The next time `eventName` is triggered, this `listener` is removed and then invoked.\n```js\nemitter\n  .once('event', cb)\n  .emit('event', args);\n\nemitter.listenerCount('event');// 0\n```\nReturns a reference to the `Emitter`, so that calls can be chained.\n\nNote that when a listener is called, the standard `this` keyword is intentionally set to reference the `Emitter` instance to which the `listener` is attached.\n```js\nemitter.once('event', function () {\n  console.log(this === emitter \u0026\u0026 this instanceof Emitter);\n});\n\nemitter.emit('event');// Print: true\n```\n\nIt is possible to use ES6 Arrow Functions as listeners, however, when doing so, the `this` keyword will no longer reference the Emitter instance:\n```js\nemitter.once('event', () =\u003e {\n  console.log(this);\n});\n\nemitter.emit('event')// Print: {}\n```\n\n### emitter.emit(eventName[, ...args])\n* `eventName` { string | Array\\\u003cstring\\\u003e } The name of the event\n* `...args` { Array\\\u003cany\\\u003e } The arguments of listener\n* Returns { boolean }\n\nSynchronously calls each of the listeners registered for the event named `eventName`, in the order they were registered, passing the supplied arguments to each.\n\nReturns `true` if the event had listeners, `false` otherwise.\n\n### emitter.off([eventName[, listener]])\n* `[eventName]` { string | Array\\\u003cstring\\\u003e } The name of the event\n* `[listener]` { Function } The callback function\n* Returns { Emitter }\n\nRemoves the specified `listener` from the listener array for the event named `eventName`.\n```js\nemitter\n  .on('event', cb)\n  .off('event', cb);\n\nemitter.listenerCount('event1');// 0\n```\n`off()` will remove, at most, one instance of a `listener` from the listener array. If any single listener has been added multiple times to the listener array for the specified `eventName`, then `off()` must be called multiple times to remove each instance.\n\nNote that:\n\n* Without any params, `off()` will remove all listeners from each of the events\n* If provide `eventName` only, `off()` will remove all listeners from the event name `eventName`\n* If provide `eventName` and `listener` at the same time, `off()` will remove the specified `listener` from the event named `eventName`\n\nYou can see more actual cases in [test](test/unit)\n\n## Contributing\n[Pull requests](https://github.com/Styx11/emitter/pulls) are welcomed. For major changes, please open an [issue](https://github.com/Styx11/emitter/issues) first to discuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\n## Build\n```\n# install dependencies\nnpm install\n\n# build for production with minification\nnpm run build\n\n# build for test file\nnpm run build:test\n\n# run test\nnpm run test\n```\n\n## License\n[MIT](https://choosealicense.com/licenses/mit/)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstyx11%2Femitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstyx11%2Femitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstyx11%2Femitter/lists"}