{"id":20514239,"url":"https://github.com/webreflection/broadcast","last_synced_at":"2025-07-31T05:04:24.155Z","repository":{"id":65993124,"uuid":"85199443","full_name":"WebReflection/broadcast","owner":"WebReflection","description":"Notification channel for the past, the present, and the future.","archived":false,"fork":false,"pushed_at":"2024-09-24T08:45:09.000Z","size":157,"stargazers_count":40,"open_issues_count":3,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-14T00:11:33.910Z","etag":null,"topics":["broadcast","callback","channel","notification","promise"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WebReflection.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}},"created_at":"2017-03-16T13:32:45.000Z","updated_at":"2025-02-22T04:52:42.000Z","dependencies_parsed_at":"2024-02-01T17:57:17.062Z","dependency_job_id":null,"html_url":"https://github.com/WebReflection/broadcast","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fbroadcast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fbroadcast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fbroadcast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fbroadcast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebReflection","download_url":"https://codeload.github.com/WebReflection/broadcast/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248799954,"owners_count":21163404,"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":["broadcast","callback","channel","notification","promise"],"created_at":"2024-11-15T21:15:27.977Z","updated_at":"2025-04-14T00:11:56.415Z","avatar_url":"https://github.com/WebReflection.png","language":"JavaScript","readme":"# broadcast\n\n[![build status](https://github.com/WebReflection/broadcast/actions/workflows/node.js.yml/badge.svg)](https://github.com/WebReflection/broadcast/actions) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/broadcast/badge.svg)](https://coveralls.io/github/WebReflection/broadcast)\n\nPreviously known as [notify-js](https://www.webreflection.co.uk/blog/2015/08/14/the-line-between-events-and-promises),\n`broadcast` is a private or public notification chanel inspired by standards.\n\nUseful for loaders, components bootstrap, geo position updates, and all other asynchronous or on demand user granted privileges operations, `broadcast` works on every browser and every platform, it's 100% tests covered, and it weights about 0.3K.\n\n### V4 Release\n\n  * **Breaking**\n    * removed `new` method; the export now is `broadcast` and the `Broadcast` class\n    * changed `when` signature; it now always returns a *Promise*\n    * no transpilation anymore, usable by ES2015+ compatible engines\n  * **New**\n    * smaller\n    * faster\n    * better\n    * stronger\n\n### API\n\n  * `.all(type:any, callback:Function):void` to be notified every time a specific _type_ changes (i.e. each `.that(type, value)` call in the future).\n  * `.drop(type:any[, callback:Function]):void` remove a specific _callback_ from all future changes. If the callback is omitted, it removes _type_ from the internal _Map_ (drop all callbacks and value).\n  * `.that(type:any[, value:any]):Function|void` broadcast to all callbacks and resolves all promises with `value`. If omitted, it returns a callback that will broadcast, once invoked, the received `value` (i.e. `thing.addListener(any, broadcast.that(type))`).\n  * `.when(type:any):Promise` returns a _Promise_ that will resolve once _type_ is known.\n\n### Examples\n\n```js\nimport {broadcast} from 'broadcast';\n\n// as Promise,\n//  inspired by customRegistry.whenDefined(...).then(...)\n// will you ever ask for a geo position or\n// have you asked for it already ?\nbroadcast.when('geo:position').then(info =\u003e {\n  showOnMap(info.coords);\n});\n\n// as one-off Event (Promise or Callback)\nbroadcast\n  .when('dom:DOMContentLoaded')\n  .then(boostrapMyApp);\n```\n\nIt doesn't matter if a channel was resolved, updated, or never asked for,\nwhenever that happens, broadcasts will follow.\n\n```js\n// that position? only once asked for it\nnavigator.geolocation.getCurrentPosition(info =\u003e {\n  // manual broadcast\n  broadcast.that('geo:position', info);\n});\n\n// update the position each change? same\nnavigator.geolocation.watchPosition(\n  // implicit broadcast once executed\n  broadcast.that('geo:position')\n);\n\n// the file? You got it.\nfs.readFile(\n  'README.md',\n  // will broadcast once executed\n  (err, data) =\u003e broadcast.that('fs:README.md', err || data)\n);\n\n// top of the page\ndocument.addEventListener(\n  'DOMContentLoaded',\n  broadcast.that('dom:DOMContentLoaded')\n);\n```\n\n#### one broadcast VS all broadcasts\n\nA `broadcast` happens only once asked for it, and it will receive the latest resolution.\n\nIf you'd like to listen to all broadcasted changes, you can use `broadcast.all(type, callback)`,\nand eventually stop listening to it via `broadcast.drop(type, callback)`.\n\n```js\n\nlet watchId;\n\nfunction updatePosition(info) {\n  mapTracker.setCoords(info.coords);\n}\n\nbutton.addEventListener('click', e =\u003e {\n  if (watchId) {\n    navigator.geolocation.clearWatch(watcher);\n    watchId = 0;\n    broadcast.drop('geo:position', updatePosition);\n  } else {\n    watchId = navigator.geolocation.watchPosition(\n      // updates the latest position info on each call\n      broadcast.that('geo:position')\n    );\n    broadcast.all('geo:position', updatePosition);\n  }\n});\n```\n\n\n#### private broadcasts\nThere are two different ways to have a private broadcasts:\n\n  * using a secret `type` as channel, like in `broadcast.when(privateSymbol)`\n  * create an instance a part via `import {Broadcast} from 'broadcast';` and `const bc = new Broadcast;`\n\nThe first way enables shared, yet private, resolutions while the second one would be unreachable outside its scope.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fbroadcast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebreflection%2Fbroadcast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fbroadcast/lists"}