Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/flippiecoetser/events

Typescript Event Emitter complied as ES6 Module
https://github.com/flippiecoetser/events

Last synced: 4 days ago
JSON representation

Typescript Event Emitter complied as ES6 Module

Awesome Lists containing this project

README

        

# Events
Implementation of a Node.Events in Typescript compiled to an ES6 Browser Module

[API Documentation](https://flippiecoetser.github.io/Events/)

### Example
Open `./demo/index.html` by **right-click** on file in VS Code and select: **Open with Live Server**.
The console output in chrome developer tools should display: `event emitted: eventIdentifier`.

To reproduce this demo follow the steps below:
1. After `npm install` make sure `./demo/index.html` contains importmap for dependencies:
```html

{
"imports": {
"@browser-modules/dictionary":
"../node_modules/@browser-modules/dictionary/lib/dictionary.js",
"@browser-modules/events":
"../node_modules/@browser-modules/dictionary/lib/events.js"
}
}

```
note: *importmaps make bare imports possible, see use case in step 4.*

2. Add ES6 Modules in browser:

```html

```

3. Create `./demo/index.js` file
4. Import the module:

```javascript
import { Event } from '@browser-modules/events';
```

5. Create emitter:

```javascript
class Emitter extends Event {}
const emitter = new Emitter()
```

6. Register a listener:

```javascript
emitter.on('event', event =>
console.log(`event emitted: ${event}`)
)
```

7. Trigger event with eventIdentifier as argument:

```javascript
emitter.emit('event','eventIdentifier')
```