1- import type { Subscriber } from '@rxjs/observable' ;
1+ import type { Subscriber } from '@rxjs/observable' ;
22import { operate , Observable , from } from '@rxjs/observable' ;
33import type { ObservableInput , OperatorFunction } from '../types.js' ;
44import { noop } from '../util/noop.js' ;
@@ -43,14 +43,18 @@ import { noop } from '../util/noop.js';
4343 */
4444export function bufferWhen < T > ( closingSelector : ( ) => ObservableInput < any > ) : OperatorFunction < T , T [ ] > {
4545 return ( source ) =>
46- new Observable ( ( subscriber ) => {
46+ new Observable ( ( destination ) => {
4747 // The buffer we keep and emit.
4848 let buffer : T [ ] | null = null ;
4949 // A reference to the subscriber used to subscribe to
5050 // the closing notifier. We need to hold this so we can
5151 // end the subscription after the first notification.
5252 let closingSubscriber : Subscriber < T > | null = null ;
5353
54+ destination . add ( ( ) => {
55+ buffer = closingSubscriber = null ! ;
56+ } ) ;
57+
5458 // Ends the previous closing notifier subscription, so it
5559 // terminates after the first emission, then emits
5660 // the current buffer if there is one, starts a new buffer, and starts a
@@ -62,12 +66,12 @@ export function bufferWhen<T>(closingSelector: () => ObservableInput<any>): Oper
6266 // emit the buffer if we have one, and start a new buffer.
6367 const b = buffer ;
6468 buffer = [ ] ;
65- b && subscriber . next ( b ) ;
69+ b && destination . next ( b ) ;
6670
6771 // Get a new closing notifier and subscribe to it.
6872 from ( closingSelector ( ) ) . subscribe (
6973 ( closingSubscriber = operate ( {
70- destination : subscriber ,
74+ destination,
7175 next : openBuffer ,
7276 complete : noop ,
7377 } ) )
@@ -80,17 +84,15 @@ export function bufferWhen<T>(closingSelector: () => ObservableInput<any>): Oper
8084 // Subscribe to our source.
8185 source . subscribe (
8286 operate ( {
83- destination : subscriber ,
87+ destination,
8488 // Add every new value to the current buffer.
8589 next : ( value ) => buffer ?. push ( value ) ,
8690 // When we complete, emit the buffer if we have one,
8791 // then complete the result.
8892 complete : ( ) => {
89- buffer && subscriber . next ( buffer ) ;
90- subscriber . complete ( ) ;
93+ buffer && destination . next ( buffer ) ;
94+ destination . complete ( ) ;
9195 } ,
92- // Release memory on finalization
93- finalize : ( ) => ( buffer = closingSubscriber = null ! ) ,
9496 } )
9597 ) ;
9698 } ) ;
0 commit comments