1.1.1 • Published 2 years ago

@browser-modules/events v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Events

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

API Documentation

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:

<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.

  1. Add ES6 Modules in browser:
<script src="index.js" type="module"></script>
  1. Create ./demo/index.js file
  2. Import the module:
import { Event } from '@browser-modules/events';
  1. Create emitter:
class Emitter extends Event {}
const emitter = new Emitter()
  1. Register a listener:
emitter.on('event', event => 
    console.log(`event emitted: ${event}`)
)
  1. Trigger event with eventIdentifier as argument:
emitter.emit('event','eventIdentifier')