https://github.com/untemps/event-dispatcher
Abstract class to manage event dispatching
https://github.com/untemps/event-dispatcher
event-dispatcher
Last synced: 3 months ago
JSON representation
Abstract class to manage event dispatching
- Host: GitHub
- URL: https://github.com/untemps/event-dispatcher
- Owner: untemps
- License: mit
- Created: 2020-05-23T15:06:36.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-12-13T17:39:05.000Z (6 months ago)
- Last Synced: 2025-02-20T04:17:54.335Z (3 months ago)
- Topics: event-dispatcher
- Language: JavaScript
- Homepage:
- Size: 793 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @untemps/event-dispatcher
Abstract class that allows to internally dispatch events and attach subscribers to listen for them.

[](https://github.com/untemps/event-dispatcher/actions)
## Installation
```bash
yarn add @untemps/event-dispatcher
```## Usage
Import `EventDispatcher`:
```javascript
import { EventDispatcher } from '@untemps/event-dispatcher'
```Create a class that extends `EventDispatcher`:
```javascript
class Foo extends EventDispatcher {
constructor() {
super()
}foo() {
this.dispatchEvent(new Event('foo', { bubbles: false, cancelable: false, composed: false }))
}
}
```Each instance can now attach a subscriber to listen for events:
```javascript
const onFoo = (event) => console.log('foo has be triggered!')
const myFoo = new Foo()
myFoo.addEventListener('foo', onFoo)
```And detach it:
```javascript
myFoo.removeEventListener('foo', onFoo)
```All subscriptions for a specific event type can be detached in batches:
```javascript
const myFoo = new Foo()
myFoo.addEventListener('foo', onFoo1)
myFoo.addEventListener('foo', onFoo2)
myFoo.clearType('somethingDone')
```All instance subscriptions can be detached in batches:
```javascript
const myFoo = new Foo()
myFoo.addEventListener('foo', onFoo)
myFoo.addEventListener('bar', onBar)
myFoo.cleanup()
```## Todos
- Add examples
- Rewrite with TypeScript