StreamUtils.ts 700 B

123456789101112131415161718192021222324
  1. import { combineLatest, Observable } from "rxjs"
  2. import { map } from "rxjs/operators"
  3. /**
  4. * Constructs a stream of a object from a collection of other observables
  5. *
  6. * @param streamObj The object containing key of observables to assemble from
  7. *
  8. * @returns The constructed object observable
  9. */
  10. export function constructFromStreams<T>(
  11. streamObj: { [key in keyof T]: Observable<T[key]> }
  12. ): Observable<T> {
  13. return combineLatest(Object.values<Observable<T[keyof T]>>(streamObj)).pipe(
  14. map((streams) => {
  15. const keys = Object.keys(streamObj) as (keyof T)[]
  16. return keys.reduce(
  17. (acc, s, i) => Object.assign(acc, { [s]: streams[i] }),
  18. {}
  19. ) as T
  20. })
  21. )
  22. }