| 1 |
import { Subscriber } from '../Subscriber'; |
| 2 |
import { ObservableInput, OperatorFunction } from '../types'; |
| 3 |
import { operate } from '../util/lift'; |
| 4 |
import { noop } from '../util/noop'; |
| 5 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 6 |
import { innerFrom } from '../observable/innerFrom'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Buffers the source Observable values, using a factory function of closing |
| 10 |
* Observables to determine when to close, emit, and reset the buffer. |
| 11 |
* |
| 12 |
* <span class="informal">Collects values from the past as an array. When it |
| 13 |
* starts collecting values, it calls a function that returns an Observable that |
| 14 |
* tells when to close the buffer and restart collecting.</span> |
| 15 |
* |
| 16 |
*  |
| 17 |
* |
| 18 |
* Opens a buffer immediately, then closes the buffer when the observable |
| 19 |
* returned by calling `closingSelector` function emits a value. When it closes |
| 20 |
* the buffer, it immediately opens a new buffer and repeats the process. |
| 21 |
* |
| 22 |
* ## Example |
| 23 |
* |
| 24 |
* Emit an array of the last clicks every [1-5] random seconds |
| 25 |
* |
| 26 |
* ```ts |
| 27 |
* import { fromEvent, bufferWhen, interval } from 'rxjs'; |
| 28 |
* |
| 29 |
* const clicks = fromEvent(document, 'click'); |
| 30 |
* const buffered = clicks.pipe( |
| 31 |
* bufferWhen(() => interval(1000 + Math.random() * 4000)) |
| 32 |
* ); |
| 33 |
* buffered.subscribe(x => console.log(x)); |
| 34 |
* ``` |
| 35 |
* |
| 36 |
* @see {@link buffer} |
| 37 |
* @see {@link bufferCount} |
| 38 |
* @see {@link bufferTime} |
| 39 |
* @see {@link bufferToggle} |
| 40 |
* @see {@link windowWhen} |
| 41 |
* |
| 42 |
* @param closingSelector A function that takes no arguments and returns an |
| 43 |
* Observable that signals buffer closure. |
| 44 |
* @return A function that returns an Observable of arrays of buffered values. |
| 45 |
*/ |
| 46 |
export function bufferWhen<T>(closingSelector: () => ObservableInput<any>): OperatorFunction<T, T[]> { |
| 47 |
return operate((source, subscriber) => { |
| 48 |
// The buffer we keep and emit. |
| 49 |
let buffer: T[] | null = null; |
| 50 |
// A reference to the subscriber used to subscribe to |
| 51 |
// the closing notifier. We need to hold this so we can |
| 52 |
// end the subscription after the first notification. |
| 53 |
let closingSubscriber: Subscriber<T> | null = null; |
| 54 |
|
| 55 |
// Ends the previous closing notifier subscription, so it |
| 56 |
// terminates after the first emission, then emits |
| 57 |
// the current buffer if there is one, starts a new buffer, and starts a |
| 58 |
// new closing notifier. |
| 59 |
const openBuffer = () => { |
| 60 |
// Make sure to finalize the closing subscription, we only cared |
| 61 |
// about one notification. |
| 62 |
closingSubscriber?.unsubscribe(); |
| 63 |
// emit the buffer if we have one, and start a new buffer. |
| 64 |
const b = buffer; |
| 65 |
buffer = []; |
| 66 |
b && subscriber.next(b); |
| 67 |
|
| 68 |
// Get a new closing notifier and subscribe to it. |
| 69 |
innerFrom(closingSelector()).subscribe((closingSubscriber = createOperatorSubscriber(subscriber, openBuffer, noop))); |
| 70 |
}; |
| 71 |
|
| 72 |
// Start the first buffer. |
| 73 |
openBuffer(); |
| 74 |
|
| 75 |
// Subscribe to our source. |
| 76 |
source.subscribe( |
| 77 |
createOperatorSubscriber( |
| 78 |
subscriber, |
| 79 |
// Add every new value to the current buffer. |
| 80 |
(value) => buffer?.push(value), |
| 81 |
// When we complete, emit the buffer if we have one, |
| 82 |
// then complete the result. |
| 83 |
() => { |
| 84 |
buffer && subscriber.next(buffer); |
| 85 |
subscriber.complete(); |
| 86 |
}, |
| 87 |
// Pass all errors through to consumer. |
| 88 |
undefined, |
| 89 |
// Release memory on finalization |
| 90 |
() => (buffer = closingSubscriber = null!) |
| 91 |
) |
| 92 |
); |
| 93 |
}); |
| 94 |
} |