{"id":18879741,"url":"https://github.com/loilo/monomitter","last_synced_at":"2026-02-20T04:30:18.101Z","repository":{"id":57302546,"uuid":"316153885","full_name":"loilo/monomitter","owner":"loilo","description":"📡 A tiny, overly simplistic event bus","archived":false,"fork":false,"pushed_at":"2023-08-30T22:18:06.000Z","size":60,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-31T05:24:34.209Z","etag":null,"topics":["emitter","events","javascript","pubsub","signal"],"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/loilo.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-26T07:27:39.000Z","updated_at":"2023-08-30T22:14:21.000Z","dependencies_parsed_at":"2024-11-08T11:00:09.640Z","dependency_job_id":null,"html_url":"https://github.com/loilo/monomitter","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loilo%2Fmonomitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loilo%2Fmonomitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loilo%2Fmonomitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loilo%2Fmonomitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/loilo","download_url":"https://codeload.github.com/loilo/monomitter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239841742,"owners_count":19705981,"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":["emitter","events","javascript","pubsub","signal"],"created_at":"2024-11-08T06:39:06.423Z","updated_at":"2026-02-20T04:30:18.063Z","avatar_url":"https://github.com/loilo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cbr\u003e\n  \u003cbr\u003e\n\n![monomitter logo showing a radio station symbol](logo.svg)\n\n# monomitter\n\n  \u003cbr\u003e\n\u003c/div\u003e\n\n[![Tests](https://badgen.net/github/checks/loilo/monomitter/master)](https://github.com/loilo/monomitter/actions)\n[![Version on npm](https://badgen.net/npm/v/monomitter)](https://www.npmjs.com/package/monomitter)\n\nThe monomitter is a tiny (125 bytes minzipped), generic notification helper — a kind of topic-free pub/sub mechanism or a single-event event bus — designed to be used as a building block for reactive functionality.\n\n\u003e **Note:** This is an [ESM-only package](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). Install version 1.x if you need CommonJS support.\n\n## Basic Usage\n\n```js\nimport monomitter from 'monomitter'\n\n// create a monomitter pub/sub pair\nconst [publish, subscribe] = monomitter()\n\n// log payload on publish\nsubscribe((...payload) =\u003e {\n  console.log(payload)\n})\n\npublish(1, 2, 3) // logs [1, 2, 3]\npublish('Hello world!') // logs [\"Hello world!\"]\n```\n\n### Unsubscribe\n\nThe subscribe function returns a callback to be used for unsubscribing:\n\n```js\nimport monomitter from 'monomitter'\n\nconst [publish, subscribe] = monomitter()\n\n// subscribe and get unsubscribe callback\nconst stop = subscribe((...payload) =\u003e {\n  console.log(payload)\n})\n\npublish(1, 2, 3) // logs [1, 2, 3]\n\n// unsubscribe\nstop()\n\npublish(42) // does not log\n```\n\n### Clear All Subscribers\n\nThe `monomitter` function returns a third item, a \"clear-all\" callback:\n\n```js\nimport monomitter from 'monomitter'\n\nconst [publish, subscribe, clear] = monomitter()\n\n// subscribe and get unsubscribe callback\nsubscribe(() =\u003e {\n  console.log('hi from subscriber 1')\n})\nsubscribe(() =\u003e {\n  console.log('hi from subscriber 2')\n})\n\npublish(1, 2, 3) // logs \"hi from subscriber 1\" and \"hi from subscriber 2\"\n\n// clear all subscribers\nclear()\n\npublish(42) // does not log\n```\n\n## Examples\n\nmonomitter is suitable for a variety of notification-related tasks. Here are some examples:\n\n### Observable\n\nCreate a watchable value wrapper:\n\n```js\nimport monomitter from 'monomitter'\n\n// Implementation\n\nfunction observable(inititalValue) {\n  let currentValue = inititalValue\n\n  const [notify, watch] = monomitter()\n\n  return {\n    watch,\n    get: () =\u003e currentValue,\n    set: newValue =\u003e {\n      if (newValue !== currentValue) {\n        const oldValue = currentValue\n        currentValue = newValue\n        notify(newValue, oldValue)\n      }\n    }\n  }\n}\n\n// Usage\n\nconst value = observable(5)\nvalue.watch((newValue, oldValue) =\u003e {\n  console.log('Changed from %o to %o', oldValue, newValue)\n})\nvalue.get() // returns 5\nvalue.set(10) // logs \"Changed from 5 to 10\"\nvalue.get() // returns 10\n```\n\n### Signaling\n\nCreate a controller with a signal (not unlike the [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)):\n\n```js\nimport monomitter from 'monomitter'\n\n// Implementation\n\nfunction Signal(subscribe) {\n  this.addListener = subscribe\n}\n\nfunction SignalController() {\n  const [publish, subscribe] = monomitter()\n  this.trigger = publish\n  this.signal = new Signal(subscribe)\n}\n\n// Usage\n\nconst controller = new SignalController()\n\n// Pass the controller.signal to a consumer who may be interested\ncontroller.signal.addListener(() =\u003e {\n  console.log('Got a signal!')\n})\n\n// Trigger the signal\ncontroller.trigger() // logs \"Got a signal!\"\n```\n\n## Event Emitter\n\nIt's even possible to build a fully-fledged event emitter with this. (But why would you if there's [mitt](https://npmjs.com/package/mitt) — this example is very much just a proof of concept.)\n\n```js\nimport monomitter from 'monomitter'\n\n// Implementation\n\nclass EventEmitter {\n  constructor() {\n    this.eventData = new Map()\n  }\n\n  getEventData(event) {\n    if (!this.eventData.has(event)) {\n      const [emit, listen] = monomitter()\n      this.eventData.set(event, {\n        emit,\n        listen,\n        unsubscribers: new WeakMap()\n      })\n    }\n\n    return this.eventData.get(event)\n  }\n\n  on(event, callback) {\n    const eventData = this.getEventData(event)\n    const unsubscriber = eventData.listen(callback)\n    eventData.unsubscribers.set(callback, unsubscriber)\n  }\n\n  once(event, callback) {\n    const eventData = this.getEventData(event)\n    const unsubscriber = eventData.listen((...args) =\u003e {\n      callback(...args)\n      unsubscriber()\n    })\n    eventData.unsubscribers.set(callback, unsubscriber)\n  }\n\n  off(event, callback) {\n    this.getEventData(event).unsubscribers.get(callback)?.()\n  }\n\n  emit(event, ...payload) {\n    this.getEventData(event).emit(...payload)\n  }\n}\n\n// Usage\n\nconst ee = new EventEmitter()\n\nee.on('load', function onload() {\n  console.log('load!')\n})\nee.once('load', function onloadOnce() {\n  console.log('load once!')\n})\n\nee.emit('load') // logs \"load!\" and \"load once!\"\nee.emit('load') // logs \"load!\"\n\nee.off('load', onload)\n\nee.emit('load') // does not log\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floilo%2Fmonomitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Floilo%2Fmonomitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floilo%2Fmonomitter/lists"}