{"id":13403788,"url":"https://github.com/tbreuss/eventbus","last_synced_at":"2025-08-11T00:06:20.896Z","repository":{"id":47999234,"uuid":"144621756","full_name":"tbreuss/eventbus","owner":"tbreuss","description":":bus: Dead simple ES6-ready JavaScript EventBus","archived":false,"fork":false,"pushed_at":"2023-02-28T03:42:34.000Z","size":535,"stargazers_count":16,"open_issues_count":5,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-03T20:35:38.575Z","etag":null,"topics":["dispatcher","es6","es6-javascript","es6-modules","eventbus","events","javascript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tbreuss.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}},"created_at":"2018-08-13T19:00:45.000Z","updated_at":"2024-02-06T04:35:45.000Z","dependencies_parsed_at":"2024-01-16T10:36:41.986Z","dependency_job_id":"b15cb5a1-a2f0-482a-851f-076dfb2b34ed","html_url":"https://github.com/tbreuss/eventbus","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tbreuss/eventbus","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tbreuss%2Feventbus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tbreuss%2Feventbus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tbreuss%2Feventbus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tbreuss%2Feventbus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tbreuss","download_url":"https://codeload.github.com/tbreuss/eventbus/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tbreuss%2Feventbus/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269809476,"owners_count":24478545,"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","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["dispatcher","es6","es6-javascript","es6-modules","eventbus","events","javascript"],"created_at":"2024-07-30T19:01:34.621Z","updated_at":"2025-08-11T00:06:20.836Z","avatar_url":"https://github.com/tbreuss.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# :bus: Simple ES6-ready class for managing events in JavaScript\n\n## Installation\n\n### In a browser\n\nDownload this repo and copy `src/eventbus.js` into your project's JavaScript asset folder.\nImport the class instance using ES6 module import.\n\n```js\nimport { global as EventBus } from '/your_js_assets_folder/eventbus.js';\n```\n\nYou're ready to go.\n\n## API\n\n### EventBus.`on`\n\n```js\n// @type - string\n// @callback - function\n// @scope - the scope where the @callback is defined\n// @args - pass additional arguments as you like\nEventBus.on(type, callback, scope, ...args)\n```\n\n### EventBus.`off`\n\n```js\n// @type - string\n// @callback - function\n// @scope - the scope where the @callback is defined\nEventBus.off(type, callback, scope)\n```\n\n### EventBus.`has`\n\n```js\n// @type - string\n// @callback - function\n// @scope - the scope where the @callback is defined\nEventBus.has(type, callback, scope)\n```\n\n### EventBus.`emit`\n\n```js\n// @type - string\n// @target - the caller\n// @args - pass as many arguments as you want\nEventBus.emit(type, target, ...args)\n```\n\n### EventBus.`debug`\n\nFor debugging purpose only, it returns the added events as a string.\n\n```js\nconsole.log(EventBus.debug());\n```\n\n## Usage\n\n```js\nfunction myHandler(event) {\n  console.log(\"myHandler type=\" + event.type);\n}\nEventBus.on(\"my_event\", myHandler);\nEventBus.emit(\"my_event\");\n```\n\n## Keeping the scope\n\n```js\nclass TestClass1 {\n    constructor() {\n        this.className = \"TestClass1\";\n        EventBus.on(\"callback_event\", this.callback, this);\n    }\n    callback(event) {\n        console.log(this.className + \" / type: \" + event.type + \" / dispatcher: \" + event.target.className);\n    }\n}\n\nclass TestClass2 {\n    constructor() {\n        this.className = \"TestClass2\";\n    }\n    dispatch() {\n        EventBus.emit(\"callback_event\", this);\n    }\n}\n\nlet t1 = new TestClass1();\nlet t2 = new TestClass2();\nt2.dispatch();\n```\n\n## Passing additional parameters\n\n```js\nclass TestClass1 {\n    constructor() {\n        this.className = \"TestClass1\";\n        EventBus.on(\"custom_event\", this.doSomething, this);\n    }\n    doSomething(event, param1, param2) {\n        console.log(this.className + \".doSomething\");\n        console.log(\"type=\" + event.type);\n        console.log(\"params=\" + param1 + param2);\n        console.log(\"coming from=\" + event.target.className);\n    }\n}\n\nclass TestClass2 {\n    constructor() {\n        this.className = \"TestClass2\";\n    }\n    ready() {\n        EventBus.emit(\"custom_event\", this, \"javascript events\", \" are really useful\");\n    }\n}\n\nlet t1 = new TestClass1();\nlet t2 = new TestClass2();\n\nt2.ready();\n```\n\n## Using EventBus.off\n\nTo remove an event handler you have to pass the same callback instance.\n\nThis is wrong and won't work because callback functions are different functions.\n\n```js\nEventBus.on('EXAMPLE_EVENT', function() {\n    console.log('example callback');\n});\nEventBus.off('EXAMPLE_EVENT', function() {\n    console.log('example callback');\n});\n```\n\nThis is correct. Our callback function is the same function.\n\n```js\nvar handler = function() {\n    console.log('example callback');\n};\nEventBus.on('EXAMPLE_EVENT', handler);\nEventBus.emit('EXAMPLE_EVENT');\nEventBus.off('EXAMPLE_EVENT', handler);\n// Not emitted since event was removed\nEventBus.emit('EXAMPLE_EVENT');\n```\n\n## Examples\n\nTo run the examples you have to start a webserver at the root of this repository.\n\nFor example the built in PHP server:\n\n    $ cd eventbus\n    $ php -S localhost:9999\n    \nNow, open \u003chttp://localhost:9999/examples/\u003e in your browser.\n\nIn the Console of Chrome DevTools you should see a few log entries.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftbreuss%2Feventbus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftbreuss%2Feventbus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftbreuss%2Feventbus/lists"}