@browser-modules/events
v1.1.1
Published
Implementation of Node.Event in Typescript compiled as ES6 Browser Module
Downloads
7
Readme
Events
Implementation of a Node.Events in Typescript compiled to an ES6 Browser Module
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:
- After
npm install
make sure./demo/index.html
contains importmap for dependencies:
<script type="importmap">
{
"imports": {
"@browser-modules/dictionary":
"../node_modules/@browser-modules/dictionary/lib/dictionary.js",
"@browser-modules/events":
"../node_modules/@browser-modules/dictionary/lib/events.js"
}
}
</script>
note: importmaps make bare imports possible, see use case in step 4.
- Add ES6 Modules in browser:
<script src="index.js" type="module"></script>
- Create
./demo/index.js
file - Import the module:
import { Event } from '@browser-modules/events';
- Create emitter:
class Emitter extends Event {}
const emitter = new Emitter()
- Register a listener:
emitter.on('event', event =>
console.log(`event emitted: ${event}`)
)
- Trigger event with eventIdentifier as argument:
emitter.emit('event','eventIdentifier')