add-promise-listener
Advanced tools
Comparing version 0.1.0 to 0.1.1
13
index.js
@@ -9,2 +9,6 @@ const { get } = Reflect; | ||
// avoid event.preventDefault throwing due illegal Proxy invocation | ||
const bound = (e, value) => typeof value === 'function' ? value.bind(e) : value; | ||
// traps the `event.currentTarget` to be sure it's available later on | ||
class Handler { | ||
@@ -18,6 +22,13 @@ #currentTarget; | ||
// next tick, which is why this proxy handler needs to exist. | ||
return name === 'currentTarget' ? this.#currentTarget : get(e, name); | ||
return name === 'currentTarget' ? this.#currentTarget : bound(e, get(e, name)); | ||
} | ||
} | ||
/** | ||
* Add a listener that result as a Promise, fulfilled when the event happens once or rejected if the optional provided signal is aborted. | ||
* @param {Element} element | ||
* @param {string} type | ||
* @param {{ signal?:AbortSignal, capture?:boolean, passive?:boolean, preventDefault?:boolean, stopPropagation?:boolean, stopImmediatePropagation?:boolean }?} options | ||
* @returns {Promise<Event,Event>} | ||
*/ | ||
export default (element, type, options = null) => new Promise( | ||
@@ -24,0 +35,0 @@ (resolve, reject) => { |
{ | ||
"name": "add-promise-listener", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "directories": { |
@@ -14,6 +14,12 @@ # add-promise-listener | ||
{ | ||
// optional: stopPropagation, stopImmediatePropagation or ... | ||
// this is optionally needed to be sure the operation is performed | ||
// when it's needed and not during the next tick: | ||
// stopPropagation: true | ||
// stopImmediatePropagation: true | ||
preventDefault: true, | ||
// optional signal to eventually catch rejections | ||
signal: ac.signal | ||
// other standard options are allowed as well | ||
// capture: true | ||
// passive: true | ||
} | ||
@@ -20,0 +26,0 @@ ).then( |
4408
43
39