| 1 |
import { OperatorFunction } from '../types'; |
| 2 |
import { operate } from '../util/lift'; |
| 3 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 4 |
import { arrRemove } from '../util/arrRemove'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Buffers the source Observable values until the size hits the maximum |
| 8 |
* `bufferSize` given. |
| 9 |
* |
| 10 |
* <span class="informal">Collects values from the past as an array, and emits |
| 11 |
* that array only when its size reaches `bufferSize`.</span> |
| 12 |
* |
| 13 |
*  |
| 14 |
* |
| 15 |
* Buffers a number of values from the source Observable by `bufferSize` then |
| 16 |
* emits the buffer and clears it, and starts a new buffer each |
| 17 |
* `startBufferEvery` values. If `startBufferEvery` is not provided or is |
| 18 |
* `null`, then new buffers are started immediately at the start of the source |
| 19 |
* and when each buffer closes and is emitted. |
| 20 |
* |
| 21 |
* ## Examples |
| 22 |
* |
| 23 |
* Emit the last two click events as an array |
| 24 |
* |
| 25 |
* ```ts |
| 26 |
* import { fromEvent, bufferCount } from 'rxjs'; |
| 27 |
* |
| 28 |
* const clicks = fromEvent(document, 'click'); |
| 29 |
* const buffered = clicks.pipe(bufferCount(2)); |
| 30 |
* buffered.subscribe(x => console.log(x)); |
| 31 |
* ``` |
| 32 |
* |
| 33 |
* On every click, emit the last two click events as an array |
| 34 |
* |
| 35 |
* ```ts |
| 36 |
* import { fromEvent, bufferCount } from 'rxjs'; |
| 37 |
* |
| 38 |
* const clicks = fromEvent(document, 'click'); |
| 39 |
* const buffered = clicks.pipe(bufferCount(2, 1)); |
| 40 |
* buffered.subscribe(x => console.log(x)); |
| 41 |
* ``` |
| 42 |
* |
| 43 |
* @see {@link buffer} |
| 44 |
* @see {@link bufferTime} |
| 45 |
* @see {@link bufferToggle} |
| 46 |
* @see {@link bufferWhen} |
| 47 |
* @see {@link pairwise} |
| 48 |
* @see {@link windowCount} |
| 49 |
* |
| 50 |
* @param bufferSize The maximum size of the buffer emitted. |
| 51 |
* @param startBufferEvery Interval at which to start a new buffer. |
| 52 |
* For example if `startBufferEvery` is `2`, then a new buffer will be started |
| 53 |
* on every other value from the source. A new buffer is started at the |
| 54 |
* beginning of the source by default. |
| 55 |
* @return A function that returns an Observable of arrays of buffered values. |
| 56 |
*/ |
| 57 |
export function bufferCount<T>(bufferSize: number, startBufferEvery: number | null = null): OperatorFunction<T, T[]> { |
| 58 |
// If no `startBufferEvery` value was supplied, then we're |
| 59 |
// opening and closing on the bufferSize itself. |
| 60 |
startBufferEvery = startBufferEvery ?? bufferSize; |
| 61 |
|
| 62 |
return operate((source, subscriber) => { |
| 63 |
let buffers: T[][] = []; |
| 64 |
let count = 0; |
| 65 |
|
| 66 |
source.subscribe( |
| 67 |
createOperatorSubscriber( |
| 68 |
subscriber, |
| 69 |
(value) => { |
| 70 |
let toEmit: T[][] | null = null; |
| 71 |
|
| 72 |
// Check to see if we need to start a buffer. |
| 73 |
// This will start one at the first value, and then |
| 74 |
// a new one every N after that. |
| 75 |
if (count++ % startBufferEvery! === 0) { |
| 76 |
buffers.push([]); |
| 77 |
} |
| 78 |
|
| 79 |
// Push our value into our active buffers. |
| 80 |
for (const buffer of buffers) { |
| 81 |
buffer.push(value); |
| 82 |
// Check to see if we're over the bufferSize |
| 83 |
// if we are, record it so we can emit it later. |
| 84 |
// If we emitted it now and removed it, it would |
| 85 |
// mutate the `buffers` array while we're looping |
| 86 |
// over it. |
| 87 |
if (bufferSize <= buffer.length) { |
| 88 |
toEmit = toEmit ?? []; |
| 89 |
toEmit.push(buffer); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if (toEmit) { |
| 94 |
// We have found some buffers that are over the |
| 95 |
// `bufferSize`. Emit them, and remove them from our |
| 96 |
// buffers list. |
| 97 |
for (const buffer of toEmit) { |
| 98 |
arrRemove(buffers, buffer); |
| 99 |
subscriber.next(buffer); |
| 100 |
} |
| 101 |
} |
| 102 |
}, |
| 103 |
() => { |
| 104 |
// When the source completes, emit all of our |
| 105 |
// active buffers. |
| 106 |
for (const buffer of buffers) { |
| 107 |
subscriber.next(buffer); |
| 108 |
} |
| 109 |
subscriber.complete(); |
| 110 |
}, |
| 111 |
// Pass all errors through to consumer. |
| 112 |
undefined, |
| 113 |
() => { |
| 114 |
// Clean up our memory when we finalize |
| 115 |
buffers = null!; |
| 116 |
} |
| 117 |
) |
| 118 |
); |
| 119 |
}); |
| 120 |
} |