Skip to content

Instantly share code, notes, and snippets.

@sod
Last active September 7, 2020 10:25
Show Gist options
  • Save sod/a34fc7705b396f61d0ef00713b807209 to your computer and use it in GitHub Desktop.
Save sod/a34fc7705b396f61d0ef00713b807209 to your computer and use it in GitHub Desktop.
import {combineLatest, ObservableInput, OperatorFunction} from 'rxjs';
import {map, mergeMap, take} from 'rxjs/operators';
// if you need some extra values from other observables - preferably synchronous like ngrx state
//
// Example:
// this.action.pipe(
// ofType(myActionType),
// tap((myActionType) => {}), // <-- only has the action value
// mergeTakeOne(this.store.select(myStoreValue), /* andAnotherObservable$, andEvenMoreObservables$ */),
// tap(([myActionType, myStoreValue]) => {}), // <-- has both, the action value and the value from the store observable
//
export function mergeTakeOne<SOURCE, MERGE extends ObservableInput<any>[]>(
...toBeMerged$: [...MERGE]
): OperatorFunction<SOURCE, [SOURCE, ...{[P in keyof MERGE]: MERGE[P] extends ObservableInput<infer U> ? U : never}]> {
return mergeMap((source) =>
combineLatest(toBeMerged$).pipe(
take(1),
map((source2): any => [source, ...source2]),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment