| 1 |
import { Subscription } from '../Subscription'; |
| 2 |
import { OperatorFunction, ObservableInput } from '../types'; |
| 3 |
import { operate } from '../util/lift'; |
| 4 |
import { innerFrom } from '../observable/innerFrom'; |
| 5 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 6 |
import { noop } from '../util/noop'; |
| 7 |
import { arrRemove } from '../util/arrRemove'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Buffers the source Observable values starting from an emission from |
| 11 |
* `openings` and ending when the output of `closingSelector` emits. |
| 12 |
* |
| 13 |
* <span class="informal">Collects values from the past as an array. Starts |
| 14 |
* collecting only when `opening` emits, and calls the `closingSelector` |
| 15 |
* function to get an Observable that tells when to close the buffer.</span> |
| 16 |
* |
| 17 |
*  |
| 18 |
* |
| 19 |
* Buffers values from the source by opening the buffer via signals from an |
| 20 |
* Observable provided to `openings`, and closing and sending the buffers when |
| 21 |
* a Subscribable or Promise returned by the `closingSelector` function emits. |
| 22 |
* |
| 23 |
* ## Example |
| 24 |
* |
| 25 |
* Every other second, emit the click events from the next 500ms |
| 26 |
* |
| 27 |
* ```ts |
| 28 |
* import { fromEvent, interval, bufferToggle, EMPTY } from 'rxjs'; |
| 29 |
* |
| 30 |
* const clicks = fromEvent(document, 'click'); |
| 31 |
* const openings = interval(1000); |
| 32 |
* const buffered = clicks.pipe(bufferToggle(openings, i => |
| 33 |
* i % 2 ? interval(500) : EMPTY |
| 34 |
* )); |
| 35 |
* buffered.subscribe(x => console.log(x)); |
| 36 |
* ``` |
| 37 |
* |
| 38 |
* @see {@link buffer} |
| 39 |
* @see {@link bufferCount} |
| 40 |
* @see {@link bufferTime} |
| 41 |
* @see {@link bufferWhen} |
| 42 |
* @see {@link windowToggle} |
| 43 |
* |
| 44 |
* @param openings A Subscribable or Promise of notifications to start new |
| 45 |
* buffers. |
| 46 |
* @param closingSelector A function that takes |
| 47 |
* the value emitted by the `openings` observable and returns a Subscribable or Promise, |
| 48 |
* which, when it emits, signals that the associated buffer should be emitted |
| 49 |
* and cleared. |
| 50 |
* @return A function that returns an Observable of arrays of buffered values. |
| 51 |
*/ |
| 52 |
export function bufferToggle<T, O>( |
| 53 |
openings: ObservableInput<O>, |
| 54 |
closingSelector: (value: O) => ObservableInput<any> |
| 55 |
): OperatorFunction<T, T[]> { |
| 56 |
return operate((source, subscriber) => { |
| 57 |
const buffers: T[][] = []; |
| 58 |
|
| 59 |
// Subscribe to the openings notifier first |
| 60 |
innerFrom(openings).subscribe( |
| 61 |
createOperatorSubscriber( |
| 62 |
subscriber, |
| 63 |
(openValue) => { |
| 64 |
const buffer: T[] = []; |
| 65 |
buffers.push(buffer); |
| 66 |
// We use this composite subscription, so that |
| 67 |
// when the closing notifier emits, we can tear it down. |
| 68 |
const closingSubscription = new Subscription(); |
| 69 |
|
| 70 |
const emitBuffer = () => { |
| 71 |
arrRemove(buffers, buffer); |
| 72 |
subscriber.next(buffer); |
| 73 |
closingSubscription.unsubscribe(); |
| 74 |
}; |
| 75 |
|
| 76 |
// The line below will add the subscription to the parent subscriber *and* the closing subscription. |
| 77 |
closingSubscription.add(innerFrom(closingSelector(openValue)).subscribe(createOperatorSubscriber(subscriber, emitBuffer, noop))); |
| 78 |
}, |
| 79 |
noop |
| 80 |
) |
| 81 |
); |
| 82 |
|
| 83 |
source.subscribe( |
| 84 |
createOperatorSubscriber( |
| 85 |
subscriber, |
| 86 |
(value) => { |
| 87 |
// Value from our source. Add it to all pending buffers. |
| 88 |
for (const buffer of buffers) { |
| 89 |
buffer.push(value); |
| 90 |
} |
| 91 |
}, |
| 92 |
() => { |
| 93 |
// Source complete. Emit all pending buffers. |
| 94 |
while (buffers.length > 0) { |
| 95 |
subscriber.next(buffers.shift()!); |
| 96 |
} |
| 97 |
subscriber.complete(); |
| 98 |
} |
| 99 |
) |
| 100 |
); |
| 101 |
}); |
| 102 |
} |