Skip to content
Very small library that makes working with the DOM event emitters API (and others) a little easier to work with.
TypeScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src support more event emitter types Mar 8, 2018
test support more event emitter types Mar 8, 2018
.gitignore initial Mar 5, 2018
.npmignore fix stuff for TS publishing Mar 5, 2018
README.md initial Mar 5, 2018
jest.config.json initial Mar 5, 2018
package-lock.json support more event emitter types Mar 8, 2018
package.json add prepublish script Mar 8, 2018
tsconfig.json fix stuff for TS publishing Mar 5, 2018

README.md

dom-event-utils

Very small library that makes working with DOM event emitters API a little easier to work with. These are all common utilities that I find myself reaching for, regardless of the framework I'm in.

Methods

eventToPromise - convert an event to a promise that resolves once it's called. For example:

import { eventToPromise } from 'dom-event-utils';

function createPopup() {
  const popup = window.open('/my-popup');
  return eventToPromise(popup, 'load', () => {
    // do stuff here
    return popup;
  }, 10000); //timeout is optional. If reached, it'll reject the promise
}

on - just like addEventListener, but returns an unsubscribe function.

import { on } from 'dom-event-utils';

const off = on(document.querySelector('.btn'), 'click', () => { /* click handler */ });
//...later, somewhere else...
off();

once - only called once and then unsubscribed from the event source

import { once } from 'event-utils';

once(document, 'DOMContentLoaded', () => {
  //...do things here
});
You can’t perform that action at this time.