import * as React from 'react' import {Observable} from 'rxjs' import {distinctUntilChanged, map, switchMap} from 'rxjs/operators' import {streamingComponent} from './streamingComponent' interface Props { observable: Observable children?: (value: T) => React.ReactElement } type ObservableComponent = React.ComponentType> function id(val: T): T { return val } // something is a bit off with the TS typings here function createWithObservable(): ObservableComponent { return streamingComponent>(props$ => props$.pipe( distinctUntilChanged((props, prevProps) => props.observable === prevProps.observable), switchMap(props => props.observable.pipe( map( observableValue => props.children ? props.children(observableValue) : id(observableValue) ) ) ) ) ) } export const WithObservable = createWithObservable()