{"id":13605134,"url":"https://github.com/choojs/nanobus","last_synced_at":"2025-04-12T02:32:47.415Z","repository":{"id":43642935,"uuid":"83854134","full_name":"choojs/nanobus","owner":"choojs","description":"🚎 - Tiny message bus","archived":false,"fork":false,"pushed_at":"2021-02-18T16:45:11.000Z","size":109,"stargazers_count":227,"open_issues_count":3,"forks_count":24,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-09T09:03:13.624Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/choojs.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":"2017-03-04T00:44:50.000Z","updated_at":"2025-02-08T11:06:50.000Z","dependencies_parsed_at":"2022-08-31T06:10:16.883Z","dependency_job_id":null,"html_url":"https://github.com/choojs/nanobus","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choojs%2Fnanobus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choojs%2Fnanobus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choojs%2Fnanobus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choojs%2Fnanobus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/choojs","download_url":"https://codeload.github.com/choojs/nanobus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248458261,"owners_count":21107041,"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-08-01T19:00:55.004Z","updated_at":"2025-04-12T02:32:47.172Z","avatar_url":"https://github.com/choojs.png","language":"JavaScript","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"readme":"# nanobus [![stability][0]][1]\n[![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7]\n[![downloads][8]][9] [![js-standard-style][10]][11]\n\nTiny message bus.\n\n## Usage\n```js\nvar nanobus = require('nanobus')\nvar bus = nanobus()\n\nbus.on('foo', function (color) {\n  console.log('color is', color)\n})\n\nbus.emit('foo', 'blue')\n```\n\n## FAQ\n### Why not use the Node API?\nWe had the requirement for a `*` event to catch all calls, and figured we could\nimprove the file size at the same time. This library is about 1/3rd the size of\nNode's version. And it was easy to build, so yeah good enough of an excuse hah.\n\n### How do I listen for replies?\nYou can do this by using the `.once()` listener and establishing a convention\naround naming schemas.\n\n```js\nbus.on('foo', function (color) {\n  console.log('foo called')\n  bus.emit('foo:res')\n})\n\nbus.once('foo:res', function () {\n  console.log('response received')\n})\nbus.emit('foo')\n```\n\n### When shouldn't I use this package?\nIf you're only writing code that runs inside Node and don't need a `'*'`\nlistener, consider using the built-in event emitter API instead.\n\n### Are the emitters asynchronous?\nNo. If you're interested in doing that, use something like\n[nanotick](https://github.com/yoshuawuyts/nanotick) to batch events and ensure\nthey run asynchronously.\n\n## API\n### `bus = nanobus([name])`\nCreate a new `nanobus` instance. Optionally takes a name that will be used for\ntracing in the browser using the `performance.mark` / `performance.measure`\nAPI.\n\n### `bus.emit(eventName, [data])`\nEmit an event. Arbitrary data can optionally be passed as an argument. `'*'`\nlisteners run after named listeners.\n\n### `bus.on(eventName, listener([data]))`\n### `bus.addListener(eventName, listener([data]))`\nListen to an event. If the event name is `'*'` the listener signature is\n`listener(eventName, [data], [performanceTimingId])`.\n\n### `bus.prependListener(eventName, listener([data]))`\nListen to an event, but make sure it's pushed to the start of the listener\nqueue. If the event name is `'*'` the listener signature is\n`listener(eventName, [data])`.\n\n### `bus.once(eventName, listener([data]))`\nListen to an event, and clear it after it's been called once.  If the event\nname is `'*'` the listener signature is\n`listener(eventName, [data], [performanceTimingId])`.\n\n### `bus.prependOnceListener(eventName, listener([data]))`\nListen to an event, and clear it after it's been called once.  If the event\nname is `'*'` the listener signature is `listener(eventName, [data])`.\n\n### `bus.removeListener(eventName, listener)`\nRemove a specific listener to an event.\n\n### `listeners = bus.listeners(eventName)`\nReturn all listeners for a given event. `'*'` listeners are not included in\nthis list. Use `bus.listeners('*')` to get a list of `'*'` listeners.\n\n### `bus.removeAllListeners([eventName])`\nRemove all listeners to an event. If no event name is passed, removes all\nlisteners on the message bus. `'*'` listeners are not removed unless\n`eventName` is `*` or no event name is passed.\n\n## TypeScript\n\nOptional event typing is available in TypeScript by passing an object type with \nevent names as keys and event listener function signatures as values.\n\n```ts\n// if compilerOptions.esModuleInterop = true\nimport Nanobus from \"nanobus\"\n// else\nimport Nanobus = require(\"nanobus\") \n\ntype Events = {\n    foo: (color: string) =\u003e void\n    bar: (count: number) =\u003e void\n}\n\nconst bus = new Nanobus\u003cEvents\u003e()\n\nbus.on(\"foo\", color =\u003e {\n    // color: string\n    console.log(\"color is\", color)\n})\n\nbus.on(\"bar\", count =\u003e {\n    // count: number\n    console.log(\"count is\", count)\n})\n\nbus.on(\"*\", (eventName, data) =\u003e {\n    // eventName: \"foo\" | \"bar\"\n    // data: any[]\n    if (eventName === \"foo\") {\n        const [color] = data as Parameters\u003cEvents[\"foo\"]\u003e\n        // color: string\n    } else if (eventName === \"bar\") {\n        const [count] = data as Parameters\u003cEvents[\"bar\"]\u003e\n        // count: number\n    }\n})\n\nbus.emit(\"foo\", \"blue\")  // required arguments: [string]\nbus.emit(\"bar\", 100)  // required arguments: [number]\n```\n\n## License\n[MIT](https://tldrlegal.com/license/mit-license)\n\n[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square\n[1]: https://nodejs.org/api/documentation.html#documentation_stability_index\n[2]: https://img.shields.io/npm/v/nanobus.svg?style=flat-square\n[3]: https://npmjs.org/package/nanobus\n[4]: https://img.shields.io/travis/choojs/nanobus/master.svg?style=flat-square\n[5]: https://travis-ci.org/choojs/nanobus\n[6]: https://img.shields.io/codecov/c/github/choojs/nanobus/master.svg?style=flat-square\n[7]: https://codecov.io/github/choojs/nanobus\n[8]: http://img.shields.io/npm/dm/nanobus.svg?style=flat-square\n[9]: https://npmjs.org/package/nanobus\n[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square\n[11]: https://github.com/feross/standard\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoojs%2Fnanobus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchoojs%2Fnanobus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoojs%2Fnanobus/lists"}