{"id":19994214,"url":"https://github.com/omranjamal/event-multiplexer","last_synced_at":"2025-05-04T13:31:02.075Z","repository":{"id":42838212,"uuid":"263972063","full_name":"omranjamal/event-multiplexer","owner":"omranjamal","description":"Basically multiplex events; Listen to many JavaScript event emitters through one emitter. ","archived":false,"fork":false,"pushed_at":"2023-01-06T05:59:00.000Z","size":396,"stargazers_count":4,"open_issues_count":8,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-02T20:04:06.640Z","etag":null,"topics":["event","javascript","multiplexer","mux"],"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/omranjamal.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":"2020-05-14T16:47:36.000Z","updated_at":"2023-05-20T20:56:31.000Z","dependencies_parsed_at":"2023-02-05T11:46:55.030Z","dependency_job_id":null,"html_url":"https://github.com/omranjamal/event-multiplexer","commit_stats":null,"previous_names":["hedronium/event-multiplexer"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omranjamal%2Fevent-multiplexer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omranjamal%2Fevent-multiplexer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omranjamal%2Fevent-multiplexer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omranjamal%2Fevent-multiplexer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/omranjamal","download_url":"https://codeload.github.com/omranjamal/event-multiplexer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224392270,"owners_count":17303659,"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":["event","javascript","multiplexer","mux"],"created_at":"2024-11-13T04:54:20.746Z","updated_at":"2024-11-13T04:57:44.775Z","avatar_url":"https://github.com/omranjamal.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Event Multiplexer\n\n\nIf you ever had to register or de-register listeners in bulk and resorted to using arrays, sets, maps and dictionaries to achieve it; This library will take that responsibility off your shoulders.\n\nThis is useful for example in a game or user generated graphics application (which was the original motivation behind this library).\n\n![Artwork](https://i.imgur.com/gpP5l00.png)\n\nFeatures:\n\n- Listen on multiple event emitters\n- Automatic listening to existing listeners after adding to multiplexer\n- Automatic listener removal after removal from multiplexer\n- Multiplexer events\n\n  \n  \n\n## Installation\nIf you're here, you already know.\n\n```\nnpm install --save event-multiplexer\n```\n\nor\n\n```\nyarn add event-multiplexer\n```\n\n## Usage\n\nThe `EventMultiplexer` is also itself an implementation of `EventEmitter`\nhence any calls to `on`, `off`, `addListener`, `removeListener`\n\n  \n\nThe new additions are `add(...objects)` and `remove(...objects)` for adding and removing emitters from the multiplexer.\n\n  \n### Initialize\nFirst create the multiplexer.\n\n```js\n// Import the `EventMultiplexer` class\nimport { EventMultiplexer } from  'event-multiplexer';\n\n// Initialize a multiplexer instance.\nconst  mux  =  new EventMultiplexer();\n```\n\nLet's make a few test emitters.\n\n```js\n// This is for demo purposes, any emitter or\n// child implementation will work.\nimport { EventEmitter } from 'events';\n\n// Our Test objects.\nconst  obj_a = new EventEmitter();\nobj_a.name = \"Apple\";\n\nconst  obj_b = new EventEmitter();\nobj_b.name = \"Bose\";\n\nconst  obj_c  =  new EventEmitter();\nobj_c.name = \"Cisco\";\n\nconst  obj_d  =  new EventEmitter();\nobj_d.name  =  \"Dell\";\n```\n\n  \n\n### `add(...objects)` to the MUX\nComplexity:\n\n- `O(n*m)` where `n = |objects|` and `m = |distinct events being listened to|`\n\nAdd the objects to the mux\n\n```js\n// You can add objects before you add listeners\nmux.add(obj_a);\n\n// This is a listener (lol duh)\n// More on this later.\nmux.on('EVENT', () =\u003e {\n\tconsole.log(\"I like trains.\");\n});\n\n// ... and add objects after you add listeners.\nmux.add(obj_b);\n\n// ... or add multiple wherever\nmux.add(obj_b, obj_c, obj_d);\n```\n\n  \n\nDon't worry about repeating add operations, it will only listen on the object once.\n\n#### Listen on the MUX\nAdd listeners on the mux and wait!\n\n```js\nmux.on('HELLO', (object, greeting, ...args) =\u003e {\n\t// The first argument to a handler is always the object\n\t// producing the event.\n\tconsole.log(`${object.name} says: ${greeting}`);\n});\n\n// We have added the objects to the mux before.\nobj_d.emit('HELLO', 'Bonjour');\nobj_a.emit('HELLO', 'Salam');\nobj_c.emit('HELLO', 'Namaste');\n```\n\nThe above will produce the output\n\n```\nDell says: Bonjour\nApple says: Salam\nCisco says: Namaste\n```\n\n\n### `remove(...objects)` from the MUX\n\nComplexity:\n\n- `O(n*m)` where `n = |objects|` and `m = |distinct events being listened to|`\n\n  \n\n```js\nconst  handler  = () =\u003e {}\nmux.on('EVENT', handler);\n\n// Remove.\nmux.off('EVENT', handler);\n```\n\n### MUX without the object being passed.\n```js\nconst mux = new EventMultiplexer(false);\nmux.on('EVENT', (...args) =\u003e {});\n```\n\nThe first argument to the constructor configures it to pass (on default `true`)\nor alternatively not pass (on `false`) the object producing the event to the\nevent handlers.\n  \n## Events\nThe library also exports `OBJECT_ADDED` and `OBJECT_REMOVED` symbols. These can be used to listen for object changes on the multiplexer.\n\n```js\nimport {\n\tOBJECT_ADDED,\n\tOBJECT_REMOVE,\n\tEventMultiplexer\n} from `event-multiplexer`;\n\nconst obj_a = {name: \"A\"};\nconst obj_b = {name: \"B\"};\n\nconst mux = new EventMultiplexer();\n\nmux.on(OBJECT_ADDED, (object) =\u003e {\n\tconsole.log(`${object.name} added.`);\n});\n\nmux.on(OBJECT_REMOVED, (object) =\u003e {\n\tconsole.log(`${object.name} removed.`);\n});\n\nmux.add(obj_a, obj_b);\nmux.remove(obj_a);\n```\n\nwill produce\n\n```\nA added.\nB added.\nA removed.\n```\n\n## Requirements\nAny event emitter implementation that had the `on(name, listener)` and `off(name, listener)` methods that work similar to node's implementation should work.\n\nInternally it uses the `EventEmitter` export from the environment provided `events` module. That means you will need so setup your packager to provide that module.\n\n_Webpack provides this by default._\n\n\n## License\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomranjamal%2Fevent-multiplexer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomranjamal%2Fevent-multiplexer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomranjamal%2Fevent-multiplexer/lists"}