fileoverview param { ! EventTarget } target param { string } eventName return { ! Promise < ! Event > } export const waitForEvent = ( target , eventName ) => { return new Promise ( resolve => { target . addEventListener ( eventName , resolve , { once : true } ) ; } ) ; } ; param { ! EventEmitter } emitter param { string } eventName return { ! Promise < ! Array > } export const emittedOnce = ( emitter , eventName , trigger ? ) => { return emittedNTimes ( emitter , eventName , 1 , trigger ) . then ( ( [ result ] ) => result ) ; } ; export const emittedNTimes = async ( emitter , eventName , times , trigger ? ) => { const events = [ ] ; const p = new Promise < any [ ] [ ] > ( resolve => { const handler = ( ... args ) => { events . push ( args ) ; if ( events . length === times ) { emitter . removeListener ( eventName , handler ) ; resolve ( events ) ; } } ; emitter . on ( eventName , handler ) ; } ) ; if ( trigger ) { await Promise . resolve ( trigger ( ) ) ; } return p ; } ; export const emittedUntil = async ( emitter , eventName , untilFn ) => { const p = new Promise < any [ ] [ ] > ( resolve => { const handler = ( ... args ) => { if ( untilFn ( ... args ) ) { emitter . removeListener ( eventName , handler ) ; resolve ( args ) ; } } ; emitter . on ( eventName , handler ) ; } ) ; return p ; } ;