Skip to content

Commit

Permalink
Wait for multiple flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mesca committed Feb 20, 2024
1 parent 33a05dc commit 49c6bae
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions timeflux_ui/www/common/assets/js/timeflux.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,27 +516,32 @@ function key(code) {
/**
* Resolve promise on event
*
* @param {string} name - event name
* @param {string|Array.<string>} events - event name(s)
* @returns {Promise}
*/
function flag(name) {
function flag(events) {
if (!Array.isArray(events)) events = [events];
return new Promise((resolve) => {
const handler = (event) => {
document.removeEventListener(event.type, handler);
resolve(event.detail);
for (const name of events) {
document.removeEventListener(name, handler);
}
resolve(event);
};
document.addEventListener(name, handler);
for (const name of events) {
document.addEventListener(name, handler);
}
});
}

/**
* Trigger an event
*
* @param {string} name - Event name
* @param {string} event - Event name
* @param {object} [data] - Optional data
*/
function trigger(name, data) {
document.dispatchEvent(new CustomEvent(name, { detail: data }));
function trigger(event, data) {
document.dispatchEvent(new CustomEvent(event, { detail: data }));
}

/**
Expand Down

0 comments on commit 49c6bae

Please sign in to comment.