@@ -475,6 +475,50 @@ export abstract class AsyncReadonlyQueue<T> {
475
475
return conflatedQueue ;
476
476
}
477
477
478
+ conflateWithSeedFn < U > (
479
+ seedFn : ( next : T , signal : AbortSignal ) => Promise < U > | U ,
480
+ reducer : ( prior : U , next : T , signal : AbortSignal ) => Promise < U > | U ,
481
+ ) : AsyncReadonlyQueue < U > {
482
+ let accumulator : U | undefined ;
483
+
484
+ const abortController = new AbortController ( ) ;
485
+ const signal = abortController . signal ;
486
+ const conflatedQueue = new AsyncKeepLastQueue < U > (
487
+ "conflateWithSeedFn" ,
488
+ this . _maxBufferSize ,
489
+ ( ) => abortController . abort ( ) ,
490
+ undefined ,
491
+ ( ) => {
492
+ accumulator = undefined ;
493
+ } ,
494
+ ) ;
495
+
496
+ ( async ( ) => {
497
+ for await ( const item of this . items ( ) ) {
498
+ if ( conflatedQueue . isCompleted ) return ;
499
+ try {
500
+ if ( accumulator === undefined ) {
501
+ accumulator = await seedFn ( item , signal ) ;
502
+ } else {
503
+ accumulator = await reducer ( accumulator , item , signal ) ;
504
+ }
505
+ } catch ( e ) {
506
+ if ( e . name === "AbortError" ) {
507
+ return ;
508
+ }
509
+ throw e ;
510
+ }
511
+
512
+ conflatedQueue . accumulate ( accumulator ) ;
513
+ if ( conflatedQueue . isCompleted ) return ;
514
+ }
515
+
516
+ conflatedQueue . complete ( ) ;
517
+ } ) ( ) ;
518
+
519
+ return conflatedQueue ;
520
+ }
521
+
478
522
debounce ( durationMs : number ) : AsyncReadonlyQueue < T > {
479
523
if ( durationMs < 1 ) {
480
524
throw new Error ( "debounce() durationMs must be greater than 0, got " + durationMs ) ;
@@ -701,6 +745,16 @@ export class AsyncKeepLastQueue<T> extends AsyncReadonlyQueue<T> {
701
745
protected _isItemPendingRead = false ;
702
746
protected _isPulling = false ;
703
747
748
+ constructor (
749
+ protected _name : string ,
750
+ protected _maxBufferSize : number ,
751
+ protected _onComplete ?: ( ) => void ,
752
+ protected _parents ?: AsyncReadonlyQueue < unknown > [ ] ,
753
+ protected _onEmit ?: ( ) => void ,
754
+ ) {
755
+ super ( _name , _maxBufferSize , _onComplete , _parents ) ;
756
+ }
757
+
704
758
accumulate ( item : T ) {
705
759
this . _isItemPendingRead = true ;
706
760
@@ -722,6 +776,7 @@ export class AsyncKeepLastQueue<T> extends AsyncReadonlyQueue<T> {
722
776
723
777
this . _isItemPendingRead = false ;
724
778
779
+ this . _onEmit ?.( ) ;
725
780
yield next ;
726
781
}
727
782
}
0 commit comments