@@ -27,26 +27,31 @@ export interface AsyncNotifier {
2727 waitForNotification ( signal : AbortSignal ) : Promise < void > ;
2828
2929 /**
30- * Notifies any pending listener, or makes the next {@link waitForNotification} complete immediately if no listener
30+ * Notifies a pending listener, or makes the next {@link waitForNotification} complete immediately if no listener
3131 * is currently active.
3232 */
3333 notify ( ) : void ;
3434}
3535
3636export function asyncNotifier ( ) : AsyncNotifier {
37- let waitingConsumers : ( ( ) => void ) [ ] = [ ] ;
37+ let waitingConsumer : ( ( ) => void ) | null = null ;
3838 let hasPendingNotification = false ;
3939
4040 return {
4141 notify ( ) {
42- if ( waitingConsumers . length > 0 ) {
43- waitingConsumers . splice ( 0 , 1 ) [ 0 ] ( ) ;
42+ if ( waitingConsumer != null ) {
43+ waitingConsumer ( ) ;
44+ waitingConsumer = null ;
4445 } else {
4546 hasPendingNotification = true ;
4647 }
4748 } ,
4849 waitForNotification ( signal : AbortSignal ) {
4950 return new Promise ( ( resolve ) => {
51+ if ( waitingConsumer != null ) {
52+ throw new Error ( 'Illegal call to waitForNotification, already has a waiter.' ) ;
53+ }
54+
5055 if ( signal . aborted ) {
5156 resolve ( ) ;
5257 } else if ( hasPendingNotification ) {
@@ -59,14 +64,11 @@ export function asyncNotifier(): AsyncNotifier {
5964 }
6065
6166 function onAbort ( ) {
62- const i = waitingConsumers . indexOf ( complete ) ;
63- if ( i > - 1 ) {
64- waitingConsumers . splice ( i , 1 ) ;
65- }
67+ waitingConsumer = null ;
6668 resolve ( ) ;
6769 }
6870
69- waitingConsumers . push ( complete ) ;
71+ waitingConsumer = complete ;
7072 signal . addEventListener ( 'abort' , onAbort ) ;
7173 }
7274 } ) ;
0 commit comments