Created
April 14, 2020 17:30
-
-
Save Goobles/45f0b3027d31884dba0b164ee2cc50ea to your computer and use it in GitHub Desktop.
combineLatestProperties
This file contains hidden or 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 { Observable, ObservableInput, combineLatest } from 'rxjs'; | |
| import { map } from 'rxjs/operators'; | |
| export type ObservableReturnType<T> = T extends Observable<infer R> ? R : any; | |
| export type ObjectWithObservableReturnTypes<T> = { | |
| [P in keyof T]: ObservableReturnType<T[P]> | |
| }; | |
| export function combineLatestProperties<TObj extends { [key: string]: ObservableInput<any> }>(observableObject: TObj): Observable<ObjectWithObservableReturnTypes<TObj>> { | |
| const keys = Object.keys(observableObject); | |
| return combineLatest(keys.map(key => observableObject[key])).pipe(map(resultArray => { | |
| const returnObject = {}; | |
| keys.forEach((key, index) => { | |
| returnObject[key] = resultArray[index]; | |
| }); | |
| return returnObject; | |
| })) as Observable<ObjectWithObservableReturnTypes<TObj>>; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment