π Custom RxJS operators 7/7: "
#angular #rxjs #toLatestFrom
The built-in
operator
toLatestFrom operator"#angular #rxjs #toLatestFrom
The built-in
withLatestFrom operator allows to add data from supplementary streams to the source stream. However, sometimes the source stream is just a trigger to perform a certain action for which data from supplementary streams is needed. As a result, the array of elements in the output stream contains a dummy first element. A better solution is to neglect the value from the trigger.operator
function toLatestFrom<T, D1>(d1$: ObservableInput<D1>): OperatorFunction<T, D1>;
function toLatestFrom<T, D1, D extends unknown[]>(
d1$: ObservableInput<D1>,
...data$: [...ObservableInputTuple<D>]
): OperatorFunction<T, [D1, ...D]>;
function toLatestFrom<D1, D extends unknown[]>(
d1$: ObservableInput<D1>,
...data$: [...ObservableInputTuple<D>]
) {
return pipe(
withLatestFrom(d1$, ...data$),
map(([_, ...data]) => (data.length === 1 ? data[0] : data))
);
}
π₯1