Last active
September 7, 2020 10:25
-
-
Save sod/a34fc7705b396f61d0ef00713b807209 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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