event-registrar

This library provides a simple and convenient way to register multiple DOM event handlers, including multiple handlers on the same event

Usage no npm install needed!

<script type="module">
  import eventRegistrar from 'https://cdn.skypack.dev/event-registrar';
</script>

README

event-registrar

Provides an easy way to provide callbacks for when the browser crosses the threshold of a media query. Uses the Observable API to allow for composable event streams

getting the code

install from npm: npm install event-registrar

use in your project:

import {EventRegistrar} from 'event-registar';
const registrar = new EventRegistrar();

Or

var EventRegistrar = require('event-registrar');
var registrar = new EventRegistrar();

running the tests

Install the dependencies using npm install

Run the tests using npm test

Transpile to ES5 using npm run build

Build the docs using npm run doc

Generate the docs for the readme with npm run build-readme

API Reference

EventRegistrar

Kind: global class

new EventRegistrar()

provides a simple and consistent way to attach DOM event listeners, particularly when the goal is to attach multiple to a single event handler, such as window.onscroll

eventRegistrar.register(target, event, callback) ⇒

Provides a way to register a callback to be fired when the specified event is triggered. Generally should be used as a Singleton through a dependency injection container.

For more information about the EventTarget type, check out the MDN docs.

Kind: instance method of EventRegistrar
Returns: Function an unsubscribe function

Param Type Description
target EventTarget the target to which the listener will be attached
event String the name of the event to which the listener will be bound
callback function the callback function to be executed when the event occurs

Example

const registrar = new EventRegistrar();
// register
const unreg = registrar.register(window, 'onscroll', (ev) => console.log(ev) );
.
.
.
// cleanup
unreg();