import { CacheValue } from './CacheValue' ; import * as QR from './QueryResult' ; import { Observable } from 'rxjs/internal/Observable' ; import { observable } from './Observable' ; import { distinctUntilChanged , map } from 'rxjs/operators' ; import { ObservableQuery } from './Query' ; import * as R from 'fp-ts/lib/Record' ; import * as Eq from 'fp-ts/lib/Eq' ; import * as S from './Strategy' ; import * as CV from './CacheValue' ; import { pipe } from 'fp-ts/lib/pipeable' ; export function cacheValueToQueryResult < L , P > ( cacheValue ) { return pipe ( cacheValue , CV . fold ( ( ) => QR . queryResultLoading , ( ) => QR . queryResultLoading , value => QR . queryResultFailure < L , P > ( value , false ) , value => QR . queryResultSuccess < L , P > ( value , false ) ) ) ; } const sequenceRecordObservable = R . sequence ( observable ) ; const sequenceRecordQueryResult = R . sequence ( QR . queryResult ) ; function _observe < A , L , P > ( query , input ) { switch ( query . type ) { case 'composition' : const masterObservable = _observe ( query . master , input ) ; return observable . chain ( masterObservable , QR . fold ( ( ) => observable . of ( QR . queryResultLoading ) , error => observable . of ( QR . queryResultFailure ( error , false ) ) , value => _observe ( query . slave , value ) ) ) ; case 'product' : return pipe ( query . queries , R . mapWithIndex ( ( k , query ) => _observe ( query , ( ( input || { } ) as any ) [ k ] ) ) , sequenceRecordObservable ) . pipe ( map ( sequenceRecordQueryResult ) ) as any ; case 'cached' : return query . cache . observe ( input ) . pipe ( map ( cacheValueToQueryResult ) ) ; } } param query param input param resultEq export function observe < A , L , P > ( query , input , resultEq ) { return _observe ( query , input ) . pipe ( distinctUntilChanged ( resultEq . equals ) ) ; } param query param input export function observeStrict < A , L , P > ( query , input ) { return _observe ( query , input ) . pipe ( distinctUntilChanged ( QR . getEq < L , P > ( Eq . eqStrict , Eq . eqStrict ) . equals ) ) ; } param query param input export function observeShallow < A , L , P > ( query , input ) { return _observe ( query , input ) . pipe ( distinctUntilChanged ( QR . getEq < L , P > ( S . eqShallow , S . eqShallow ) . equals ) ) ; }