since import { Alt1 } from 'fp-ts/lib/Alt' ; import { Alternative1 } from 'fp-ts/lib/Alternative' ; import { Applicative1 } from 'fp-ts/lib/Applicative' ; import { Apply1 } from 'fp-ts/lib/Apply' ; import { Compactable1 , Separated } from 'fp-ts/lib/Compactable' ; import * as E from 'fp-ts/lib/Either' ; import { Filterable1 } from 'fp-ts/lib/Filterable' ; import { flow , identity , Predicate , Refinement } from 'fp-ts/lib/function' ; import { Functor1 } from 'fp-ts/lib/Functor' ; import { Monad1 } from 'fp-ts/lib/Monad' ; import { MonadIO1 } from 'fp-ts/lib/MonadIO' ; import { MonadTask1 } from 'fp-ts/lib/MonadTask' ; import { Monoid } from 'fp-ts/lib/Monoid' ; import * as O from 'fp-ts/lib/Option' ; import { pipe } from 'fp-ts/lib/pipeable' ; import { Task } from 'fp-ts/lib/Task' ; import { combineLatest , defer , EMPTY , merge , Observable , of as rxOf } from 'rxjs' ; import { map as rxMap , mergeMap , startWith } from 'rxjs/operators' ; import { MonadObservable1 } from './MonadObservable' ; category since export const fromOption = < A > ( o ) : Observable < A > => ( O . isNone ( o ) ? EMPTY : of ( o . value ) ) ; category since export const fromIO = ma => defer ( ( ) => rxOf ( ma ( ) ) ) ; category since export const fromTask = defer ; category since export const map = f => fa => fa . pipe ( rxMap ( f ) ) ; category since export const ap = fa => fab => combineLatest ( [ fab , fa ] ) . pipe ( rxMap ( ( [ f , a ] ) => f ( a ) ) ) ; category since export const apFirst = fb => flow ( map ( a => ( ) => a ) , ap ( fb ) ) ; category since export const apSecond = < B > ( fb ) : ( < A > ( fa : Observable < A > ) => Observable < B > ) => flow ( map ( ( ) => ( b ) => b ) , ap ( fb ) ) ; category since export const of = rxOf ; category since export const chain = f => ma => ma . pipe ( mergeMap ( f ) ) ; category since export const flatten = chain ( identity ) ; category since export const chainFirst = f => chain ( a => pipe ( f ( a ) , map ( ( ) => a ) ) ) ; category since export const alt = that => fa => merge ( fa , that ( ) ) ; category since export const filterMap = < A , B > ( f ) => ( fa ) : Observable < B > => fa . pipe ( mergeMap ( a => pipe ( f ( a ) , O . fold < B , Observable < B > > ( ( ) => EMPTY , of ) ) ) ) ; category since export const compact = filterMap ( identity ) ; category since export const partitionMap = f => fa => ( { left : pipe ( fa , filterMap ( a => O . fromEither ( E . swap ( f ( a ) ) ) ) ) , right : pipe ( fa , filterMap ( a => O . fromEither ( f ( a ) ) ) ) } ) ; category since export const separate = partitionMap ( identity ) ; category since export const filter = < A > ( p ) => ( fa ) => pipe ( fa , filterMap ( O . fromPredicate ( p ) ) ) ; category since export const partition = < A > ( p ) => ( fa ) => pipe ( fa , partitionMap ( E . fromPredicate ( p , identity ) ) ) ; category since export const zero = ( ) => EMPTY ; const map_ = ( fa , f ) => pipe ( fa , map ( f ) ) ; const ap_ = ( fab , fa ) => pipe ( fab , ap ( fa ) ) ; const chain_ = ( fa , f ) => pipe ( fa , chain ( f ) ) ; const alt_ = ( me , that ) => pipe ( me , alt ( that ) ) ; const filter_ = < A > ( fa , p ) => pipe ( fa , filter ( p ) ) ; const filterMap_ = < A , B > ( fa , f ) => pipe ( fa , filterMap ( f ) ) ; const partition_ = < A > ( fa , p ) => pipe ( fa , partition ( p ) ) ; const partitionMap_ = ( fa , f ) => pipe ( fa , partitionMap ( f ) ) ; category since export const URI = 'Observable' ; category since export type URI = typeof URI ; declare module 'fp-ts/lib/HKT' { interface URItoKind < A > { readonly [ URI ] : Observable < A > ; } } category since export const getMonoid = < A = never > ( ) : Monoid < Observable < A > > => ( { concat : ( x , y ) => merge ( x , y ) , empty : EMPTY } ) ; category since export const Functor = { URI , map : map_ } ; category since export const Apply = { URI , map : map_ , ap : ap_ } ; category since export const Applicative = { URI , map : map_ , ap : ap_ , of } ; category since export const Monad = { URI , map : map_ , ap : ap_ , of , chain : chain_ } ; category since export const Alt = { URI , map : map_ , alt : alt_ } ; category since export const Alternative = { URI , map : map_ , ap : ap_ , of , alt : alt_ , zero } ; category since export const Compactable = { URI , compact , separate } ; category since export const Filterable = { URI , compact , separate , map : map_ , filter : filter_ , filterMap : filterMap_ , partition : partition_ , partitionMap : partitionMap_ } ; category since export const MonadIO = { URI , map : map_ , ap : ap_ , of , chain : chain_ , fromIO } ; category since export const MonadTask = { URI , map : map_ , ap : ap_ , of , chain : chain_ , fromIO , fromTask } ; category since export const MonadObservable = { URI , map : map_ , ap : ap_ , of , chain : chain_ , fromIO , fromTask , fromObservable : identity } ; category since deprecated export const observable = { URI , map : map_ , of , ap : ap_ , chain : chain_ , zero , alt : alt_ , compact , separate , filter : filter_ , filterMap : filterMap_ , partition : partition_ , partitionMap : partitionMap_ , fromIO , fromTask , fromObservable : identity } ; since export const Do = of ( { } ) ; since export const bindTo = < K extends string , A > ( name ) : ( ( fa : Observable < A > ) => Observable < { [ P in K ] : A ; } > ) => map ( a => ( { [ name ] : a } as { [ P in K ] : A ; } ) ) ; since export const bind = < K extends string , A , B > ( name , f ) : ( ( fa : Observable < A > ) => Observable < { [ P in keyof A | K ] : P extends keyof A ? A [ P ] : B ; } > ) => chain ( a => pipe ( f ( a ) , map ( b => ( { ... a , [ name ] : b } as any ) ) ) ) ; since export const toTask = < A > ( o ) : Task < A > => ( ) => new Promise < A > ( ( resolve , reject ) => { let hasResult = false ; let result ; o . subscribe ( { next : value => { result = value ; hasResult = true ; } , error : reject , complete : ( ) => { if ( hasResult ) { resolve ( result ) ; } } } ) ; } ) ; since export const toTaskOption = < A > ( o ) : Task < O . Option < A > > => ( ) => pipe ( o , map ( O . some ) , startWith ( O . none ) ) . toPromise ( ) ;