2018-06-19 11:23:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview A set of helper functions to make it easier to work
|
|
|
|
* with events in async/await manner.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!EventTarget} target
|
|
|
|
* @param {string} eventName
|
|
|
|
* @return {!Promise<!Event>}
|
|
|
|
*/
|
|
|
|
const waitForEvent = (target, eventName) => {
|
|
|
|
return new Promise(resolve => {
|
2018-09-13 16:10:51 +00:00
|
|
|
target.addEventListener(eventName, resolve, { once: true })
|
2018-06-19 11:23:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!EventEmitter} emitter
|
|
|
|
* @param {string} eventName
|
|
|
|
* @return {!Promise<!Array>} With Event as the first item.
|
|
|
|
*/
|
|
|
|
const emittedOnce = (emitter, eventName) => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
emitter.once(eventName, (...args) => resolve(args))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.emittedOnce = emittedOnce
|
|
|
|
exports.waitForEvent = waitForEvent
|