{"id":22733502,"url":"https://github.com/burdiuz/js-event-dispatcher","last_synced_at":"2025-10-24T07:02:07.810Z","repository":{"id":57094498,"uuid":"51391602","full_name":"burdiuz/js-event-dispatcher","owner":"burdiuz","description":"JavaScript class for creating custom objects with events support","archived":false,"fork":false,"pushed_at":"2018-10-15T18:08:01.000Z","size":286,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-16T17:02:49.077Z","etag":null,"topics":["dispatch-events","event","event-dispatcher","event-listener","event-propagation","fire-events","javascript","js","listener","priority"],"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/burdiuz.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":"2016-02-09T19:07:55.000Z","updated_at":"2018-10-15T18:07:40.000Z","dependencies_parsed_at":"2022-08-22T22:01:01.543Z","dependency_job_id":null,"html_url":"https://github.com/burdiuz/js-event-dispatcher","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/burdiuz%2Fjs-event-dispatcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/burdiuz%2Fjs-event-dispatcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/burdiuz%2Fjs-event-dispatcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/burdiuz%2Fjs-event-dispatcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/burdiuz","download_url":"https://codeload.github.com/burdiuz/js-event-dispatcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246266250,"owners_count":20749754,"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":["dispatch-events","event","event-dispatcher","event-listener","event-propagation","fire-events","javascript","js","listener","priority"],"created_at":"2024-12-10T20:14:49.380Z","updated_at":"2025-10-24T07:02:07.689Z","avatar_url":"https://github.com/burdiuz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EventDispatcher\n\n[![Build Status](https://travis-ci.org/burdiuz/js-event-dispatcher.svg?branch=master)](https://travis-ci.org/burdiuz/js-event-dispatcher)\n[![Coverage Status](https://coveralls.io/repos/github/burdiuz/js-event-dispatcher/badge.svg?branch=master)](https://coveralls.io/github/burdiuz/js-event-dispatcher?branch=master)\n\nJust another EventDispatcher/[EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) implementation.\n\n## Installation\nEasy to install with [npm](https://www.npmjs.com/) package manager\n```javascript\nnpm install --save @actualwave/event-dispatcher\n```\nwith [yarn](https://yarnpkg.com/) package manager\n```javascript\nyarn add @actualwave/event-dispatcher\n```\n\n## Usage\n\n\u003e Note: EventDispatcher distribution package contains `dist/` folder with package wrapped into UMD wrapper, so it can be used with any AMD module loader, nodejs `require()` or without any.\n\nTo start using EventDispatcher, just instantiate it on its own\n```javascript\nclass MyClass {\n  constructor() {\n    this._dispatcher = new EventDispatcher();\n  }\n  addListener(handler) {\n    this._dispatcher.addEventListener('didSomething', handler);\n  }\n  doSomething() {\n\t  this._dispatcher.dispatchEvent('didSomething');\n  }\n}\n```\nextend it with your class\n```javascript\nclass MyClass extends EventDispatcher {\n  doSomething() {\n    this.dispatchEvent('didSomething');\n  }\n}\n```\nAfter instantiating `MyClass`, every call of `doSomething()` will fire event `didSomething` and every listener attached to this event will be called with event object as argument.\n```javascript\nvar myObj = new MyClass();\nmyObj.addEventListener('didSomething', function(event) {\n  console.log('My Listener', event.type);\n});\nmyObj.doSomething();\n```\nWhen adding listeners they will be executed in same order as they where added. To change order you can use optional `priority` argument to `addEventListener()` method.\n```javascript\nmyObj.addEventListener('didSomething', function(event) {\n  console.log('Prioritized Listener', event.type);\n}, 1);\nmyObj.doSomething();\n```\nBy default priority is set to 0, and you can specify higher priority \u003e0 or lower \u003c0, only integer values are allowed.\n\nTo fire event you should use `dispatchEvent()` method, it can be used in three ways:\nYou can pass only event type string. In this case event object will be created by `EventDispatcher`\n```javascript\nvar dispatcher = new EventDispatcher();\ndispatcher.dispatchEvent('eventType');\n```\nWith event type you can specify any data, as second argument, that should be passed with event\n```javascript\ndispatcher.addEventListener('eventType', function(event) {\n  console.log('My Listener', event.type, event.data);\n});\ndispatcher.dispatchEvent('eventType', {myData: 'something'});\n```\nAlso you can pass event object, it must contain `type:String` property\n```javascript\ndispatcher.dispatchEvent({type: 'eventType', data: 'data is optional'});\n```\n\nIf you want full control of events that fire from your EventDispatcher, you can specify event pre-processor function that will be called for each event before it fired. This function should return same or new event object.\n```javascript\nfunction eventPreprocessor(event){\n  event.data = event.data || {};\n  return event;\n}\nvar dispatcher = new EventDispatcher(eventPreprocessor);\ndispatcher.dispatchEvent('eventType');\n```\n`eventPreprocessor()` function will be called with event object and returned object will be used.\n\nExample available in project's `example` folder. To try example first run server\n```javascript\nnpm run server\n```\nAnd then go to [http://localhost:8081/index.html](http://localhost:8081/index.html)\n\n## API\n\n### EventDispatcher\n* **addEventListener**(eventType:String, listener:Function, priority:int=0):void - Add listener to event type. Additionally priority can be set, higher values allow call listeners before others, lower -- after. Same listener can be added to event type using different priorities. By default, 0.\n* **hasEventListener**(eventType:String):void - Check if listener was added to event type.\n* **removeEventListener**(eventType:String, listener:Function):void - Remove event listener from event of specified type.\n* **removeAllEventListeners**(eventType:String):void - Remove all listeners from event of specified type.\n* **dispatchEvent**(eventType:String, data:Object=null):void - Dispatch event of `eventType` and pass `data`. Will create object of built-in class Event and pass it as first argument  to listeners.\n* **dispatchEvent**(event:Object):void - Event object that should be fired, can be any object. Only requirement -- it must contain field `type` with event type.\n\nEventDispatcher constructor accepts optional argument `eventPreprocessor(event:Object):Object`, function that receive event object as argument and should return same or new/changed event object that will be passed to event listeners.\n\n### Event\nBuilt-in class to represent dispatched event.\nObjects of Event class are used when dispatchEvent() method is called with `eventType`.\n* **type**:String - Event type.\n* **data**:Object - Data object passed to `EventDispatcher.dispatchEvent()` method.\n* **preventDefault**():void - Will change \"prevented\" flag from FALSE to TRUE, it can be requested via `isDefaultPrevented()` method.\n* **isDefaultPrevented**():Boolean - Will return TRUE if `preventDefault()` was called.\n\nAny event(instance of built-in Event class or any other object passed as event) gains additional methods when its being dispatched. After cycle finished, these methods will be removed from event object.\n* **stopPropagation**():void - Stop event propagation after processing all listeners of same priority.\n* **stopImmediatePropagation**():void - Stop event propagation on current listener.\n\n\n\u003e Written with [StackEdit](https://stackedit.io/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fburdiuz%2Fjs-event-dispatcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fburdiuz%2Fjs-event-dispatcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fburdiuz%2Fjs-event-dispatcher/lists"}