| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { Subject } from '../Subject'; |
| 3 |
import { Subscription } from '../Subscription'; |
| 4 |
import { ObservableInput, OperatorFunction } from '../types'; |
| 5 |
import { operate } from '../util/lift'; |
| 6 |
import { innerFrom } from '../observable/innerFrom'; |
| 7 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 8 |
import { noop } from '../util/noop'; |
| 9 |
import { arrRemove } from '../util/arrRemove'; |
| 10 |
|
| 11 |
/** |
| 12 |
* Branch out the source Observable values as a nested Observable starting from |
| 13 |
* an emission from `openings` and ending when the output of `closingSelector` |
| 14 |
* emits. |
| 15 |
* |
| 16 |
* <span class="informal">It's like {@link bufferToggle}, but emits a nested |
| 17 |
* Observable instead of an array.</span> |
| 18 |
* |
| 19 |
*  |
| 20 |
* |
| 21 |
* Returns an Observable that emits windows of items it collects from the source |
| 22 |
* Observable. The output Observable emits windows that contain those items |
| 23 |
* emitted by the source Observable between the time when the `openings` |
| 24 |
* Observable emits an item and when the Observable returned by |
| 25 |
* `closingSelector` emits an item. |
| 26 |
* |
| 27 |
* ## Example |
| 28 |
* |
| 29 |
* Every other second, emit the click events from the next 500ms |
| 30 |
* |
| 31 |
* ```ts |
| 32 |
* import { fromEvent, interval, windowToggle, EMPTY, mergeAll } from 'rxjs'; |
| 33 |
* |
| 34 |
* const clicks = fromEvent(document, 'click'); |
| 35 |
* const openings = interval(1000); |
| 36 |
* const result = clicks.pipe( |
| 37 |
* windowToggle(openings, i => i % 2 ? interval(500) : EMPTY), |
| 38 |
* mergeAll() |
| 39 |
* ); |
| 40 |
* result.subscribe(x => console.log(x)); |
| 41 |
* ``` |
| 42 |
* |
| 43 |
* @see {@link window} |
| 44 |
* @see {@link windowCount} |
| 45 |
* @see {@link windowTime} |
| 46 |
* @see {@link windowWhen} |
| 47 |
* @see {@link bufferToggle} |
| 48 |
* |
| 49 |
* @param openings An observable of notifications to start new windows. |
| 50 |
* @param closingSelector A function that takes the value emitted by the |
| 51 |
* `openings` observable and returns an Observable, which, when it emits a next |
| 52 |
* notification, signals that the associated window should complete. |
| 53 |
* @return A function that returns an Observable of windows, which in turn are |
| 54 |
* Observables. |
| 55 |
*/ |
| 56 |
export function windowToggle<T, O>( |
| 57 |
openings: ObservableInput<O>, |
| 58 |
closingSelector: (openValue: O) => ObservableInput<any> |
| 59 |
): OperatorFunction<T, Observable<T>> { |
| 60 |
return operate((source, subscriber) => { |
| 61 |
const windows: Subject<T>[] = []; |
| 62 |
|
| 63 |
const handleError = (err: any) => { |
| 64 |
while (0 < windows.length) { |
| 65 |
windows.shift()!.error(err); |
| 66 |
} |
| 67 |
subscriber.error(err); |
| 68 |
}; |
| 69 |
|
| 70 |
innerFrom(openings).subscribe( |
| 71 |
createOperatorSubscriber( |
| 72 |
subscriber, |
| 73 |
(openValue) => { |
| 74 |
const window = new Subject<T>(); |
| 75 |
windows.push(window); |
| 76 |
const closingSubscription = new Subscription(); |
| 77 |
const closeWindow = () => { |
| 78 |
arrRemove(windows, window); |
| 79 |
window.complete(); |
| 80 |
closingSubscription.unsubscribe(); |
| 81 |
}; |
| 82 |
|
| 83 |
let closingNotifier: Observable<any>; |
| 84 |
try { |
| 85 |
closingNotifier = innerFrom(closingSelector(openValue)); |
| 86 |
} catch (err) { |
| 87 |
handleError(err); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
subscriber.next(window.asObservable()); |
| 92 |
|
| 93 |
closingSubscription.add(closingNotifier.subscribe(createOperatorSubscriber(subscriber, closeWindow, noop, handleError))); |
| 94 |
}, |
| 95 |
noop |
| 96 |
) |
| 97 |
); |
| 98 |
|
| 99 |
// Subscribe to the source to get things started. |
| 100 |
source.subscribe( |
| 101 |
createOperatorSubscriber( |
| 102 |
subscriber, |
| 103 |
(value: T) => { |
| 104 |
// Copy the windows array before we emit to |
| 105 |
// make sure we don't have issues with reentrant code. |
| 106 |
const windowsCopy = windows.slice(); |
| 107 |
for (const window of windowsCopy) { |
| 108 |
window.next(value); |
| 109 |
} |
| 110 |
}, |
| 111 |
() => { |
| 112 |
// Complete all of our windows before we complete. |
| 113 |
while (0 < windows.length) { |
| 114 |
windows.shift()!.complete(); |
| 115 |
} |
| 116 |
subscriber.complete(); |
| 117 |
}, |
| 118 |
handleError, |
| 119 |
() => { |
| 120 |
// Add this finalization so that all window subjects are |
| 121 |
// disposed of. This way, if a user tries to subscribe |
| 122 |
// to a window *after* the outer subscription has been unsubscribed, |
| 123 |
// they will get an error, instead of waiting forever to |
| 124 |
// see if a value arrives. |
| 125 |
while (0 < windows.length) { |
| 126 |
windows.shift()!.unsubscribe(); |
| 127 |
} |
| 128 |
} |
| 129 |
) |
| 130 |
); |
| 131 |
}); |
| 132 |
} |