@@ -622,6 +622,7 @@ export class TableUtils {
622622 * @param eventName Event to listen for
623623 * @param timeout Event timeout in milliseconds, defaults to 0
624624 * @param matcher Optional function to determine if the promise can be resolved or stays pending
625+ * @deprecated Use `makeCancelableEventPromise` instead.
625626 * @returns Resolves with the event data
626627 */
627628 static makeCancelableTableEventPromise < TEventDetails = unknown > (
@@ -630,6 +631,30 @@ export class TableUtils {
630631 timeout = 0 ,
631632 matcher : ( ( event : DhType . Event < TEventDetails > ) => boolean ) | null = null
632633 ) : CancelablePromise < DhType . Event < TEventDetails > > {
634+ return TableUtils . makeCancelableEventPromise ( table , eventName , {
635+ timeout,
636+ matcher : matcher ?? undefined ,
637+ } ) ;
638+ }
639+
640+ /**
641+ * Make a cancelable promise for a one-shot event with a timeout.
642+ * @param emitter The object emitting the event
643+ * @param eventName Event to listen for
644+ * @param options Optional options for listening
645+ * @param options.timeout Event timeout in milliseconds, defaults to 0
646+ * @param options.matcher Optional function to determine if the promise can be resolved or stays pending
647+ * @returns Resolves with the event data
648+ */
649+ static makeCancelableEventPromise < TEventDetails = unknown > (
650+ emitter : DhType . HasEventHandling ,
651+ eventName : string ,
652+ options : {
653+ timeout ?: number ;
654+ matcher ?: ( event : DhType . Event < TEventDetails > ) => boolean ;
655+ } = { }
656+ ) : CancelablePromise < DhType . Event < TEventDetails > > {
657+ const { timeout = 0 , matcher } = options ;
633658 let eventCleanup : ( ) => void ;
634659 let timeoutId : ReturnType < typeof setTimeout > ;
635660 let isPending = true ;
@@ -639,17 +664,20 @@ export class TableUtils {
639664 isPending = false ;
640665 reject ( new TimeoutError ( `Event "${ eventName } " timed out.` ) ) ;
641666 } , timeout ) ;
642- eventCleanup = table . addEventListener < TEventDetails > ( eventName , event => {
643- if ( matcher != null && ! matcher ( event ) ) {
644- log . debug2 ( 'Event triggered, but matcher returned false.' ) ;
645- return ;
667+ eventCleanup = emitter . addEventListener < TEventDetails > (
668+ eventName ,
669+ event => {
670+ if ( matcher != null && ! matcher ( event ) ) {
671+ log . debug2 ( 'Event triggered, but matcher returned false.' ) ;
672+ return ;
673+ }
674+ log . debug2 ( 'Event triggered, resolving.' ) ;
675+ eventCleanup ( ) ;
676+ clearTimeout ( timeoutId ) ;
677+ isPending = false ;
678+ resolve ( event ) ;
646679 }
647- log . debug2 ( 'Event triggered, resolving.' ) ;
648- eventCleanup ( ) ;
649- clearTimeout ( timeoutId ) ;
650- isPending = false ;
651- resolve ( event ) ;
652- } ) ;
680+ ) ;
653681 } ) as CancelablePromise < DhType . Event < TEventDetails > > ;
654682 wrappedPromise . cancel = ( ) => {
655683 if ( isPending ) {
0 commit comments