https://github.com/ccnokes/dom-event-utils
Very small library that makes working with the DOM event emitters API (and others) a little easier to work with.
https://github.com/ccnokes/dom-event-utils
Last synced: about 1 year ago
JSON representation
Very small library that makes working with the DOM event emitters API (and others) a little easier to work with.
- Host: GitHub
- URL: https://github.com/ccnokes/dom-event-utils
- Owner: ccnokes
- Created: 2018-03-05T04:17:37.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-08T03:24:31.000Z (over 8 years ago)
- Last Synced: 2025-05-07T21:03:17.587Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 87.9 KB
- Stars: 28
- Watchers: 1
- Forks: 18
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
```javascript
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.
```javascript
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
```javascript
import { once } from 'event-utils';
once(document, 'DOMContentLoaded', () => {
//...do things here
});
```