| 1 |
import { ObservableInputTuple, OperatorFunction, Cons } from '../types'; |
| 2 |
import { combineLatest } from './combineLatest'; |
| 3 |
|
| 4 |
/** |
| 5 |
* Create an observable that combines the latest values from all passed observables and the source |
| 6 |
* into arrays and emits them. |
| 7 |
* |
| 8 |
* Returns an observable, that when subscribed to, will subscribe to the source observable and all |
| 9 |
* sources provided as arguments. Once all sources emit at least one value, all of the latest values |
| 10 |
* will be emitted as an array. After that, every time any source emits a value, all of the latest values |
| 11 |
* will be emitted as an array. |
| 12 |
* |
| 13 |
* This is a useful operator for eagerly calculating values based off of changed inputs. |
| 14 |
* |
| 15 |
* ## Example |
| 16 |
* |
| 17 |
* Simple concatenation of values from two inputs |
| 18 |
* |
| 19 |
* ```ts |
| 20 |
* import { fromEvent, combineLatestWith, map } from 'rxjs'; |
| 21 |
* |
| 22 |
* // Setup: Add two inputs to the page |
| 23 |
* const input1 = document.createElement('input'); |
| 24 |
* document.body.appendChild(input1); |
| 25 |
* const input2 = document.createElement('input'); |
| 26 |
* document.body.appendChild(input2); |
| 27 |
* |
| 28 |
* // Get streams of changes |
| 29 |
* const input1Changes$ = fromEvent(input1, 'change'); |
| 30 |
* const input2Changes$ = fromEvent(input2, 'change'); |
| 31 |
* |
| 32 |
* // Combine the changes by adding them together |
| 33 |
* input1Changes$.pipe( |
| 34 |
* combineLatestWith(input2Changes$), |
| 35 |
* map(([e1, e2]) => (<HTMLInputElement>e1.target).value + ' - ' + (<HTMLInputElement>e2.target).value) |
| 36 |
* ) |
| 37 |
* .subscribe(x => console.log(x)); |
| 38 |
* ``` |
| 39 |
* |
| 40 |
* @param otherSources the other sources to subscribe to. |
| 41 |
* @return A function that returns an Observable that emits the latest |
| 42 |
* emissions from both source and provided Observables. |
| 43 |
*/ |
| 44 |
export function combineLatestWith<T, A extends readonly unknown[]>( |
| 45 |
...otherSources: [...ObservableInputTuple<A>] |
| 46 |
): OperatorFunction<T, Cons<T, A>> { |
| 47 |
return combineLatest(...otherSources); |
| 48 |
} |