{"versions":{"0.0.0":{"name":"event-target-bus","version":"0.0.0","description":"","scripts":{},"main":"","keywords":[],"license":"UNLICENSED","dependencies":{},"_id":"event-target-bus@0.0.0","_nodeVersion":"24.13.0","_npmVersion":"11.11.0","dist":{"integrity":"sha512-VbdNFZg0WbklSy+U9WeUh3xOZ86KxpEFpaQq9Prj1nBD2x7cyGnEX+xbi/q3JDfQHp4305Bf1MxZrZExchFQVw==","shasum":"c4e5136b6b571af10d05beef00803c241c5db6ef","tarball":"http://123.232.10.234:8212/nexus/content/groups/npm-public/event-target-bus/-/event-target-bus-0.0.0.tgz","fileCount":1,"unpackedSize":189,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIAHUXlo8co/dGMY2jCZWVnDLzR0X6wFI2sJCeowkc4RRAiAJuicLDbwH+jAMUiyP+iTuLk/mShERHXjgfcC1HgfU7A=="}],"size":223},"_npmUser":{"name":"anonymous","email":"npm@skk.moe"},"directories":{},"maintainers":[{"name":"anonymous","email":"npm@skk.moe"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/event-target-bus_0.0.0_1772274671529_0.8184368929555454"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2026-02-28T10:31:11.655Z","publish_time":1772274671655,"_source_registry_name":"default","contributors":[]},"1.0.0-beta.2":{"name":"event-target-bus","version":"1.0.0-beta.2","description":"A type-safe simple event redistribution bus implementation for `EventTarget` API.","repository":{"type":"git","url":"git+https://github.com/SukkaW/event-target-bus.git"},"main":"./dist/index.cjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.cjs","default":"./dist/index.cjs"},"./package.json":"./package.json"},"keywords":["EventTarget","event-target","event-emitter"],"author":{"name":"SukkaW","email":"https://skk.moe"},"license":"MIT","devDependencies":{"@istanbuljs/nyc-config-typescript":"^1.0.2","@swc-node/register":"^1.11.1","@swc/core":"^1.15.17","@types/mocha":"^10.0.10","@types/sinon":"^21.0.0","bumpp":"^10.4.1","bunchee":"^6.9.4","earl":"^2.0.0","eslint":"^10.0.2","eslint-config-sukka":"^8.7.0","eslint-formatter-sukka":"^8.7.0","mocha":"^11.7.5","nyc":"^18.0.0","sinon":"^21.0.1","typescript":"^5.9.3"},"scripts":{"lint":"eslint --format=sukka .","build":"bunchee --minify --no-sourcemap","test":"SWC_NODE_IGNORE_DYNAMIC=1 SWC_NODE_INLINE_SOURCE_MAP=1 nyc mocha --require @swc-node/register test/**/*.test.ts","prerelease":"pnpm run lint && pnpm run test && pnpm run build","release":"bumpp -r --all --commit \"release: %s\" --tag \"%s\""},"readmeFilename":"README.md","_id":"event-target-bus@1.0.0-beta.2","bugs":{"url":"https://github.com/SukkaW/event-target-bus/issues"},"homepage":"https://github.com/SukkaW/event-target-bus#readme","_integrity":"sha512-acUwyw459OyQ6oCbJUOmTgZxmp2Zs+sbvvR2B2rEviOyCJ/wF3T9MBWWuAtv/lCC8IJbj1mEBWDn023cajllDw==","_resolved":"/tmp/0c4ca6e27ed97810418ab1231d06a23f/event-target-bus-1.0.0-beta.2.tgz","_from":"file:event-target-bus-1.0.0-beta.2.tgz","_nodeVersion":"24.14.0","_npmVersion":"11.11.0","dist":{"integrity":"sha512-acUwyw459OyQ6oCbJUOmTgZxmp2Zs+sbvvR2B2rEviOyCJ/wF3T9MBWWuAtv/lCC8IJbj1mEBWDn023cajllDw==","shasum":"2b6f43370642751d25a44a1a63976d13e55da7a1","tarball":"http://123.232.10.234:8212/nexus/content/groups/npm-public/event-target-bus/-/event-target-bus-1.0.0-beta.2.tgz","fileCount":6,"unpackedSize":8944,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/event-target-bus@1.0.0-beta.2","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDH3Gpvp4A7eFvOsNNE1c1kdMM9W+NEooZ3zPwgydIM0gIhAMbS10P3XBDltB4hhyWEkyv0se3Z6TVT/rvBiG5xBA03"}],"size":3941},"_npmUser":{"name":"anonymous","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:3af1cc35-ccb5-4bfe-acd7-964a0b14c151"}},"directories":{},"maintainers":[{"name":"anonymous","email":"npm@skk.moe"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/event-target-bus_1.0.0-beta.2_1772281177993_0.9327591085879627"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2026-02-28T12:19:38.142Z","publish_time":1772281178142,"_source_registry_name":"default","contributors":[]},"1.0.0":{"name":"event-target-bus","version":"1.0.0","description":"A type-safe simple event redistribution bus implementation for `EventTarget` API.","repository":{"type":"git","url":"git+https://github.com/SukkaW/event-target-bus.git"},"main":"./dist/index.cjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.cjs","default":"./dist/index.cjs"},"./package.json":"./package.json"},"keywords":["EventTarget","event-target","event-emitter"],"author":{"name":"SukkaW","email":"https://skk.moe"},"license":"MIT","devDependencies":{"@istanbuljs/nyc-config-typescript":"^1.0.2","@swc-node/register":"^1.11.1","@swc/core":"^1.15.17","@types/mocha":"^10.0.10","@types/sinon":"^21.0.0","bumpp":"^10.4.1","bunchee":"^6.9.4","earl":"^2.0.0","eslint":"^10.0.2","eslint-config-sukka":"^8.7.0","eslint-formatter-sukka":"^8.7.0","mocha":"^11.7.5","nyc":"^18.0.0","sinon":"^21.0.1","typescript":"^5.9.3"},"scripts":{"lint":"eslint --format=sukka .","build":"bunchee --minify --no-sourcemap","test":"SWC_NODE_IGNORE_DYNAMIC=1 SWC_NODE_INLINE_SOURCE_MAP=1 nyc mocha --require @swc-node/register test/**/*.test.ts","prerelease":"pnpm run lint && pnpm run test && pnpm run build","release":"bumpp -r --all --commit \"release: %s\" --tag \"%s\""},"_id":"event-target-bus@1.0.0","bugs":{"url":"https://github.com/SukkaW/event-target-bus/issues"},"homepage":"https://github.com/SukkaW/event-target-bus#readme","_integrity":"sha512-uPcWKbj/BJU3Tbw9XqhHqET4/LBOhvv3/SJWr7NksxA6TC5YqBpaZgawE9R+WpYFCBFSAE4Vun+xQS6w4ABdlA==","_resolved":"/tmp/62e68ea98f1808f1cbfd2115d6634775/event-target-bus-1.0.0.tgz","_from":"file:event-target-bus-1.0.0.tgz","_nodeVersion":"24.14.0","_npmVersion":"11.11.0","dist":{"integrity":"sha512-uPcWKbj/BJU3Tbw9XqhHqET4/LBOhvv3/SJWr7NksxA6TC5YqBpaZgawE9R+WpYFCBFSAE4Vun+xQS6w4ABdlA==","shasum":"fb8e816df3bbce40bd3f985f8646b49590da0981","tarball":"http://123.232.10.234:8212/nexus/content/groups/npm-public/event-target-bus/-/event-target-bus-1.0.0.tgz","fileCount":6,"unpackedSize":8937,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/event-target-bus@1.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCTHXliRUrKuXh+lZNSn/AGyy7qKt8gYn0tCFmk+HCx4gIgRVAth7S7SBct4jt+dcSX2U2gBHEQGjpQmyFutXB+MWY="}],"size":3936},"_npmUser":{"name":"anonymous","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:3af1cc35-ccb5-4bfe-acd7-964a0b14c151"}},"directories":{},"maintainers":[{"name":"anonymous","email":"npm@skk.moe"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/event-target-bus_1.0.0_1772281253128_0.8479416519899168"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2026-02-28T12:20:53.288Z","publish_time":1772281253288,"_source_registry_name":"default","contributors":[]}},"dist-tags":{"latest":"1.0.0","next":"1.0.0-beta.2"},"name":"event-target-bus","time":{"created":"2026-02-28T10:31:23.478Z","modified":"2026-02-28T12:21:18.305Z","0.0.0":"2026-02-28T10:31:11.655Z","1.0.0-beta.2":"2026-02-28T12:19:38.142Z","1.0.0":"2026-02-28T12:20:53.288Z"},"readme":"# event-target-bus\n\nA type-safe simple event redistribution bus implementation for `EventTarget` API.\n\n## Installation\n\n```bash\npnpm add event-target-bus\nyarn add event-target-bus\nnpm install event-target-bus\n```\n\n## Why?\n\nWhen working with `EventTarget` API, typically a Web/DOM API, you often need to add and remove event listeners. A small number of listeners on event targets (say a few to a few dozen) has negligible memory and dispatch cost in modern browsers. The cost to add/remove a listener and to call a few listeners when an event fires is very small in normal apps.\n\nHowever, if you have a large number of listeners (say hundreds or more) on the same event target and event name, the cost can become significant.\n\nThis can happen in complex apps or when you have many components that need to listen to the same events. And it is especially true if you are creating a re-usable library/component (say a React/Vue hook/component, a custom web component, etc.) that will be used across the entire app.\n\nIn such cases, `event-target-bus` can be beneficial to have a single listener on the event target that redistributes the event to multiple subscribers. This way, you can reduce the overhead of adding/removing listeners and improve performance when dispatching events.\n\n## Usage\n\n**Basic Usage**\n\n```ts\nimport { createEventTargetBus } from 'event-target-bus';\n\nconst onlineBus = createEventTargetBus(\n  window,\n  'online' /** event name is typed and you get autocomplete and type checking */\n);\n\nconst handleOnline = (event: Event) => { console.log('The browser is online!'); };\n\n// subscribe to the event\nconst unsub = onlineBus.on(handleOnline);\n\n// later, when you want to unsubscribe\nubsub();\n// or\nonlineBus.off(handleOnline);\n\nconst mqlBus = createEventTargetBus(window.matchMedia('(prefers-color-scheme: dark)'), 'change');\n\nmqlBus.on((event /** event object is also typed and you will get proper typing `MediaQueryListEvent` here */) => {\n  if (event.matches) {\n    console.log('Dark mode is preferred!');\n  } else {\n    console.log('Light mode is preferred!');\n  }\n});\n\nmqlBus.on((evt) => {});\n\n// with multiple `on` calls, you will only result in one event listener actually attached to the EventTarget\n```\n\n**Usage with React `useSyncExternalStore`**\n\n```ts\n// React official docs `useSyncExternalStore` \"Subscribing to a browser API\" example, but\n// implemented with `event-target-bus` for better performance when there are many subscribers\n// https://react.dev/reference/react/useSyncExternalStore#subscribing-to-a-browser-api\n\nimport { createEventTargetBus } from 'event-target-bus';\nimport type { EventTargetBus } from 'event-target-bus';\n\nlet onlineBus: EventTargetBus<Window, 'online'> | null = null;\nlet offlineBus: EventTargetBus<Window, 'offline'> | null = null;\n\nfunction subscribe(callback) {\n  if (typeof window === 'undefined') {\n    // In SSR, we can return a no-op unsubscribe function\n    return () => {};\n  }\n\n  onlineBus ??= createEventTargetBus(window, 'online');\n  offlineBus ??= createEventTargetBus(window, 'offline');\n\n  const unsubOnline = onlineBus.on(callback);\n  const unsubOffline = offlineBus.on(callback);\n\n  return () => {\n    unsubOnline();\n    unsubOffline();\n  };\n}\n\nfunction getSnapshot() {\n  return navigator.onLine;\n}\n\nfunction useOnline() {\n  return useSyncExternalStore(subscribe, getSnapshot);\n}\n```\n\n----\n\n**event-target-bus** © [Sukka](https://github.com/SukkaW), Released under the [MIT](./LICENSE) License.\nAuthored and maintained by Sukka with help from contributors ([list](https://github.com/SukkaW/event-target-bus/graphs/contributors)).\n\n> [Personal Website](https://skk.moe) · [Blog](https://blog.skk.moe) · GitHub [@SukkaW](https://github.com/SukkaW) · Telegram Channel [@SukkaChannel](https://t.me/SukkaChannel) · Mastodon [@sukka@acg.mn](https://acg.mn/@sukka) · Twitter [@isukkaw](https://twitter.com/isukkaw) · BlueSky [@skk.moe](https://bsky.app/profile/skk.moe)\n\n<p align=\"center\">\n  <a href=\"https://github.com/sponsors/SukkaW/\">\n    <img src=\"https://sponsor.cdn.skk.moe/sponsors.svg\"/>\n  </a>\n</p>","users":{}}