/*!
 * Custom elements for Nanopore-Digital Web applications
 */
'use strict';

/**
 * Add multiple event listeners with the same callback function
 * @param el the element which emits events
 * @param events the event names to listen to (string separated by a space)
 * @param fn the function to call
 * @param opts the event options to apply
 */
function addListeners(el, events, fn, opts) {
    events
        .split(' ')
        .forEach((e) => el.addEventListener(e, fn, opts));
}
/**
 * Waits for a specific event to be emitted from an element.
 * Ignores events that bubble up from child elements.
 * @param el an html element
 * @param eventName name of the event
 */
function waitForEvent(el, eventName) {
    return new Promise((resolve) => {
        function done(event) {
            if (event.target === el) {
                el.removeEventListener(eventName, done);
                resolve();
            }
        }
        el.addEventListener(eventName, done);
    });
}

exports.addListeners = addListeners;
exports.waitForEvent = waitForEvent;
