Skip to content

Commit 51be653

Browse files
committed
notjQuery.js: Return a promise in trigger()
This allows callers to e.g. cleanup after the bubbling phase. It is now also possible to disallow cancellation and prevent bubbling. Cancellation makes only sense now to begin with, as the resolved value will indicate whether a listener called `preventDefault()` or not.
1 parent f5b19dc commit 51be653

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

asset/js/notjQuery.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,30 @@ define(function () {
8181
/**
8282
* Trigger a custom event on the element, asynchronously
8383
*
84-
* The event will bubble and is not cancelable.
84+
* The returned promise will be resolved with `false` only if the event
85+
* is cancelable and was canceled by a listener; otherwise `true`.
8586
*
8687
* @param {string} type
8788
* @param {{}} detail
89+
* @param {boolean} cancelable
90+
* @param {boolean} bubbles
91+
* @return {Promise<boolean>}
8892
*/
89-
trigger(type, detail = null) {
90-
setTimeout(() => {
91-
this.element.dispatchEvent(new CustomEvent(type, {
92-
cancelable: true, // TODO: this should depend on whether it's a native or custom event
93-
bubbles: true,
94-
detail: detail
95-
}));
96-
}, 0);
93+
trigger(type, detail = null, cancelable = true, bubbles = true) {
94+
return new Promise((resolve, reject) => {
95+
setTimeout(() => {
96+
try {
97+
const proceed = this.element.dispatchEvent(new CustomEvent(type, {
98+
cancelable: cancelable,
99+
bubbles: bubbles,
100+
detail: detail
101+
}));
102+
resolve(proceed);
103+
} catch (error) {
104+
reject(error);
105+
}
106+
}, 0);
107+
});
97108
}
98109

99110
/**

0 commit comments

Comments
 (0)