@@ -432,6 +432,59 @@ test('once() - returns a promise with an unsubscribe method', async t => {
432432 t . pass ( ) ;
433433} ) ;
434434
435+ test ( 'once() - supports filter predicate' , async t => {
436+ const emitter = new Emittery ( ) ;
437+
438+ const oncePromise = emitter . once ( 'data' , data => data . ok === true ) ;
439+ await emitter . emit ( 'data' , { ok : false , foo : 'bar' } ) ;
440+
441+ const payload = { ok : true , value : 42 } ;
442+
443+ await emitter . emit ( 'data' , payload ) ;
444+ await emitter . emit ( 'data' , { ok : true , other : 'value' } ) ;
445+
446+ t . is ( await oncePromise , payload ) ;
447+ } ) ;
448+
449+ test ( 'once() - filter predicate must be a function' , t => {
450+ const emitter = new Emittery ( ) ;
451+ t . throws (
452+ ( ) => emitter . once ( 'data' , 'not a function' ) ,
453+ {
454+ instanceOf : TypeError ,
455+ message : 'predicate must be a function' ,
456+ } ,
457+ ) ;
458+ } ) ;
459+
460+ test ( 'once() - filter predicate with multiple event names' , async t => {
461+ const emitter = new Emittery ( ) ;
462+ const payload = { ok : true , value : 42 } ;
463+
464+ const oncePromise = emitter . once ( [ 'data1' , 'data2' ] , data => data . ok === true ) ;
465+ await emitter . emit ( 'data1' , { ok : false } ) ;
466+ await emitter . emit ( 'data2' , payload ) ;
467+
468+ t . is ( await oncePromise , payload ) ;
469+ } ) ;
470+
471+ test ( 'once() - filter predicate can be unsubscribed' , async t => {
472+ const emitter = new Emittery ( ) ;
473+ const oncePromise = emitter . once ( 'data' , data => data . ok === true ) ;
474+
475+ oncePromise . off ( ) ;
476+ await emitter . emit ( 'data' , { ok : true } ) ;
477+
478+ const testPromise = Promise . race ( [
479+ oncePromise ,
480+ new Promise ( resolve => {
481+ setTimeout ( ( ) => resolve ( 'timeout' ) , 100 ) ;
482+ } ) ,
483+ ] ) ;
484+
485+ t . is ( await testPromise , 'timeout' ) ;
486+ } ) ;
487+
435488test ( 'emit() - one event' , async t => {
436489 const emitter = new Emittery ( ) ;
437490 const eventFixture = { foo : true } ;
0 commit comments