| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { Subject } from '../Subject'; |
| 3 |
import { OperatorFunction } from '../types'; |
| 4 |
import { operate } from '../util/lift'; |
| 5 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Branch out the source Observable values as a nested Observable with each |
| 9 |
* nested Observable emitting at most `windowSize` values. |
| 10 |
* |
| 11 |
* <span class="informal">It's like {@link bufferCount}, but emits a nested |
| 12 |
* Observable instead of an array.</span> |
| 13 |
* |
| 14 |
*  |
| 15 |
* |
| 16 |
* Returns an Observable that emits windows of items it collects from the source |
| 17 |
* Observable. The output Observable emits windows every `startWindowEvery` |
| 18 |
* items, each containing no more than `windowSize` items. When the source |
| 19 |
* Observable completes or encounters an error, the output Observable emits |
| 20 |
* the current window and propagates the notification from the source |
| 21 |
* Observable. If `startWindowEvery` is not provided, then new windows are |
| 22 |
* started immediately at the start of the source and when each window completes |
| 23 |
* with size `windowSize`. |
| 24 |
* |
| 25 |
* ## Examples |
| 26 |
* |
| 27 |
* Ignore every 3rd click event, starting from the first one |
| 28 |
* |
| 29 |
* ```ts |
| 30 |
* import { fromEvent, windowCount, map, skip, mergeAll } from 'rxjs'; |
| 31 |
* |
| 32 |
* const clicks = fromEvent(document, 'click'); |
| 33 |
* const result = clicks.pipe( |
| 34 |
* windowCount(3), |
| 35 |
* map(win => win.pipe(skip(1))), // skip first of every 3 clicks |
| 36 |
* mergeAll() // flatten the Observable-of-Observables |
| 37 |
* ); |
| 38 |
* result.subscribe(x => console.log(x)); |
| 39 |
* ``` |
| 40 |
* |
| 41 |
* Ignore every 3rd click event, starting from the third one |
| 42 |
* |
| 43 |
* ```ts |
| 44 |
* import { fromEvent, windowCount, mergeAll } from 'rxjs'; |
| 45 |
* |
| 46 |
* const clicks = fromEvent(document, 'click'); |
| 47 |
* const result = clicks.pipe( |
| 48 |
* windowCount(2, 3), |
| 49 |
* mergeAll() // flatten the Observable-of-Observables |
| 50 |
* ); |
| 51 |
* result.subscribe(x => console.log(x)); |
| 52 |
* ``` |
| 53 |
* |
| 54 |
* @see {@link window} |
| 55 |
* @see {@link windowTime} |
| 56 |
* @see {@link windowToggle} |
| 57 |
* @see {@link windowWhen} |
| 58 |
* @see {@link bufferCount} |
| 59 |
* |
| 60 |
* @param windowSize The maximum number of values emitted by each window. |
| 61 |
* @param startWindowEvery Interval at which to start a new window. For example |
| 62 |
* if `startWindowEvery` is `2`, then a new window will be started on every |
| 63 |
* other value from the source. A new window is started at the beginning of the |
| 64 |
* source by default. |
| 65 |
* @return A function that returns an Observable of windows, which in turn are |
| 66 |
* Observable of values. |
| 67 |
*/ |
| 68 |
export function windowCount<T>(windowSize: number, startWindowEvery: number = 0): OperatorFunction<T, Observable<T>> { |
| 69 |
const startEvery = startWindowEvery > 0 ? startWindowEvery : windowSize; |
| 70 |
|
| 71 |
return operate((source, subscriber) => { |
| 72 |
let windows = [new Subject<T>()]; |
| 73 |
let starts: number[] = []; |
| 74 |
let count = 0; |
| 75 |
|
| 76 |
// Open the first window. |
| 77 |
subscriber.next(windows[0].asObservable()); |
| 78 |
|
| 79 |
source.subscribe( |
| 80 |
createOperatorSubscriber( |
| 81 |
subscriber, |
| 82 |
(value: T) => { |
| 83 |
// Emit the value through all current windows. |
| 84 |
// We don't need to create a new window yet, we |
| 85 |
// do that as soon as we close one. |
| 86 |
for (const window of windows) { |
| 87 |
window.next(value); |
| 88 |
} |
| 89 |
// Here we're using the size of the window array to figure |
| 90 |
// out if the oldest window has emitted enough values. We can do this |
| 91 |
// because the size of the window array is a function of the values |
| 92 |
// seen by the subscription. If it's time to close it, we complete |
| 93 |
// it and remove it. |
| 94 |
const c = count - windowSize + 1; |
| 95 |
if (c >= 0 && c % startEvery === 0) { |
| 96 |
windows.shift()!.complete(); |
| 97 |
} |
| 98 |
|
| 99 |
// Look to see if the next count tells us it's time to open a new window. |
| 100 |
// TODO: We need to figure out if this really makes sense. We're technically |
| 101 |
// emitting windows *before* we have a value to emit them for. It's probably |
| 102 |
// more expected that we should be emitting the window when the start |
| 103 |
// count is reached -- not before. |
| 104 |
if (++count % startEvery === 0) { |
| 105 |
const window = new Subject<T>(); |
| 106 |
windows.push(window); |
| 107 |
subscriber.next(window.asObservable()); |
| 108 |
} |
| 109 |
}, |
| 110 |
() => { |
| 111 |
while (windows.length > 0) { |
| 112 |
windows.shift()!.complete(); |
| 113 |
} |
| 114 |
subscriber.complete(); |
| 115 |
}, |
| 116 |
(err) => { |
| 117 |
while (windows.length > 0) { |
| 118 |
windows.shift()!.error(err); |
| 119 |
} |
| 120 |
subscriber.error(err); |
| 121 |
}, |
| 122 |
() => { |
| 123 |
starts = null!; |
| 124 |
windows = null!; |
| 125 |
} |
| 126 |
) |
| 127 |
); |
| 128 |
}); |
| 129 |
} |