{"id":13457815,"url":"https://github.com/ai/nanoevents","last_synced_at":"2025-05-13T17:07:48.709Z","repository":{"id":11246233,"uuid":"68678413","full_name":"ai/nanoevents","owner":"ai","description":"Simple and tiny (107 bytes) event emitter library for JavaScript","archived":false,"fork":false,"pushed_at":"2024-12-10T07:00:27.000Z","size":2149,"stargazers_count":1550,"open_issues_count":0,"forks_count":48,"subscribers_count":19,"default_branch":"main","last_synced_at":"2025-04-23T23:16:50.131Z","etag":null,"topics":[],"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/ai.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"ai"}},"created_at":"2016-09-20T05:47:44.000Z","updated_at":"2025-04-21T03:50:27.000Z","dependencies_parsed_at":"2023-02-18T01:16:29.305Z","dependency_job_id":"7d47cd50-d0d3-4d0a-935b-7cba70cccd76","html_url":"https://github.com/ai/nanoevents","commit_stats":{"total_commits":334,"total_committers":20,"mean_commits":16.7,"dds":"0.11976047904191611","last_synced_commit":"6fe5741a60967d3c87cdedf5bfc7789abad2adce"},"previous_names":[],"tags_count":43,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fnanoevents","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fnanoevents/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fnanoevents/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fnanoevents/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ai","download_url":"https://codeload.github.com/ai/nanoevents/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250528902,"owners_count":21445519,"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":[],"created_at":"2024-07-31T09:00:37.703Z","updated_at":"2025-04-23T23:16:56.516Z","avatar_url":"https://github.com/ai.png","language":"TypeScript","funding_links":["https://github.com/sponsors/ai"],"categories":["TypeScript","Event Emitters"],"sub_categories":[],"readme":"# Nano Events\n\nSimple and tiny event emitter library for JavaScript.\n\n* Only **108 bytes** (minified and brotlied).\n  It uses [Size Limit] to control size.\n* The `on` method returns `unbind` function. You don’t need to save\n  callback to variable for `removeListener`.\n* TypeScript and ES modules support.\n* No aliases, just `emit` and `on` methods.\n  No Node.js [EventEmitter] compatibility.\n\n```js\nimport { createNanoEvents } from 'nanoevents'\n\nconst emitter = createNanoEvents()\n\nconst unbind = emitter.on('tick', volume =\u003e {\n  summary += volume\n})\n\nemitter.emit('tick', 2)\nsummary //=\u003e 2\n\nunbind()\nemitter.emit('tick', 2)\nsummary //=\u003e 2\n```\n\n[EventEmitter]: https://nodejs.org/api/events.html\n[Size Limit]:   https://github.com/ai/size-limit\n\n---\n\n\u003cimg src=\"https://cdn.evilmartians.com/badges/logo-no-label.svg\" alt=\"\" width=\"22\" height=\"16\" /\u003e  Made at \u003cb\u003e\u003ca href=\"https://evilmartians.com/devtools?utm_source=nanoevents\u0026utm_campaign=devtools-button\u0026utm_medium=github\"\u003eEvil Martians\u003c/a\u003e\u003c/b\u003e, product consulting for \u003cb\u003edeveloper tools\u003c/b\u003e.\n\n---\n\n\n## Table of Contents\n\n- [Table of Contents](#table-of-contents)\n- [Install](#install)\n- [TypeScript](#typescript)\n- [Mixing to Object](#mixing-to-object)\n- [Add Listener](#add-listener)\n- [Remove Listener](#remove-listener)\n- [Execute Listeners](#execute-listeners)\n- [Events List](#events-list)\n- [Once](#once)\n- [Remove All Listeners](#remove-all-listeners)\n\n\n## Install\n\n```sh\nnpm install nanoevents\n```\n\n\n## TypeScript\n\nNano Events accepts interface with event name\nto listener argument types mapping.\n\n```ts\ninterface Events {\n  set: (name: string, count: number) =\u003e void,\n  tick: () =\u003e void\n}\n\nconst emitter = createNanoEvents\u003cEvents\u003e()\n\n// Correct calls:\nemitter.emit('set', 'prop', 1)\nemitter.emit('tick')\n\n// Compilation errors:\nemitter.emit('set', 'prop', '1')\nemitter.emit('tick', 2)\n```\n\n\n## Mixing to Object\n\nBecause Nano Events API has only just 2 methods,\nyou could just create proxy methods in your class\nor encapsulate them entirely.\n\n```js\nclass Ticker {\n  constructor () {\n    this.emitter = createNanoEvents()\n    this.internal = setInterval(() =\u003e {\n      this.emitter.emit('tick')\n    }, 100)\n  }\n\n  stop () {\n    clearInterval(this.internal)\n    this.emitter.emit('stop')\n  }\n\n  on (event, callback) {\n    return this.emitter.on(event, callback)\n  }\n}\n```\n\nWith Typescript:\n\n```ts\nimport { createNanoEvents, Emitter } from \"nanoevents\"\n\ninterface Events {\n  start: (startedAt: number) =\u003e void\n}\n\nclass Ticker {\n  emitter: Emitter\n\n  constructor () {\n    this.emitter = createNanoEvents\u003cEvents\u003e()\n  }\n\n  on\u003cE extends keyof Events\u003e(event: E, callback: Events[E]) {\n    return this.emitter.on(event, callback)\n  }\n}\n```\n\n\n## Add Listener\n\nUse `on` method to add listener for specific event:\n\n```js\nemitter.on('tick', number =\u003e {\n  console.log(number)\n})\n\nemitter.emit('tick', 1)\n// Prints 1\nemitter.emit('tick', 5)\n// Prints 5\n```\n\nIn case of your listener relies on some particular context\n(if it uses `this` within itself) you have to bind required\ncontext explicitly before passing function in as a callback.\n\n```js\nvar app = {\n  userId: 1,\n  getListener () {\n    return () =\u003e {\n      console.log(this.userId)\n    }\n  }\n}\nemitter.on('print', app.getListener())\n```\n\nNote: binding with use of the `.bind()` method won’t work as you might expect\nand therefore is not recommended.\n\n\n## Remove Listener\n\nMethods `on` returns `unbind` function. Call it and this listener\nwill be removed from event.\n\n```js\nconst unbind = emitter.on('tick', number =\u003e {\n  console.log('on ' + number)\n})\n\nemitter.emit('tick', 1)\n// Prints \"on 1\"\n\nunbind()\nemitter.emit('tick', 2)\n// Prints nothing\n```\n\n\n## Execute Listeners\n\nMethod `emit` will execute all listeners. First argument is event name, others\nwill be passed to listeners.\n\n```js\nemitter.on('tick', (a, b) =\u003e {\n  console.log(a, b)\n})\nemitter.emit('tick', 1, 'one')\n// Prints 1, 'one'\n```\n\n\n## Events List\n\nYou can get used events list by `events` property.\n\n```js\nconst unbind = emitter.on('tick', () =\u003e { })\nemitter.events //=\u003e { tick: [ [Function] ] }\n```\n\n\n## Once\n\nIf you need add event listener only for first event dispatch,\nyou can use this snippet:\n\n```js\nclass Ticker {\n  constructor () {\n    this.emitter = createNanoEvents()\n  }\n  …\n  once (event, callback) {\n    const unbind = this.emitter.on(event, (...args) =\u003e {\n      unbind()\n      callback(...args)\n    })\n    return unbind\n  }\n}\n```\n\n\n## Remove All Listeners\n\n```js\nemitter.on('event1', () =\u003e { })\nemitter.on('event2', () =\u003e { })\n\nemitter.events = { }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fai%2Fnanoevents","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fai%2Fnanoevents","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fai%2Fnanoevents/lists"}