since import { Alt1 } from 'fp-ts/lib/Alt' ; import { Applicative1 } from 'fp-ts/lib/Applicative' ; import { Apply1 } from 'fp-ts/lib/Apply' ; import { flow , identity , Predicate , Refinement } from 'fp-ts/lib/function' ; import { Functor1 } from 'fp-ts/lib/Functor' ; import { IO } from 'fp-ts/lib/IO' ; import { Monad1 } from 'fp-ts/lib/Monad' ; import { MonadIO1 } from 'fp-ts/lib/MonadIO' ; import { MonadTask1 } from 'fp-ts/lib/MonadTask' ; import * as O from 'fp-ts/lib/Option' ; import { pipe } from 'fp-ts/lib/pipeable' ; import { Observable } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; import { MonadObservable1 } from './MonadObservable' ; import * as R from './Observable' ; category since export interface ObservableOption < A > extends Observable < O . Option < A > > { } category since export const none = pipe ( O . none , R . of ) ; category since export const some = flow ( O . some , R . of ) ; category since export const fromObservable = R . map ( O . some ) ; category since export const fromIO = flow ( R . fromIO , fromObservable ) ; category since export const fromTask = flow ( R . fromTask , fromObservable ) ; category since export const tryCatch = flow ( R . map ( O . some ) , catchError ( ( ) => none ) ) ; category since export const fold = flow ( O . fold , R . chain ) ; category since export const getOrElse = < A > ( onNone ) => ( ma ) : Observable < A > => pipe ( ma , R . chain ( O . fold ( onNone , R . of ) ) ) ; category since export const alt = f => R . chain ( O . fold ( f , some ) ) ; category since export const map = f => R . map ( O . map ( f ) ) ; category since export const ap = < A > ( fa ) : ( < B > ( fab : ObservableOption < ( a : A ) => B > ) => ObservableOption < B > ) => flow ( R . map ( gab => ( ga ) => O . ap ( ga ) ( gab ) ) , R . ap ( fa ) ) ; category since export const apFirst = fb => flow ( map ( a => ( ) => a ) , ap ( fb ) ) ; category since export const apSecond = < B > ( fb ) : ( < A > ( fa : ObservableOption < A > ) => ObservableOption < B > ) => flow ( map ( ( ) => ( b ) => b ) , ap ( fb ) ) ; category since export const chain = < A , B > ( f ) => ( ma ) : ObservableOption < B > => pipe ( ma , R . chain ( O . fold ( ( ) => none , f ) ) ) ; category since export const flatten = chain ( identity ) ; category since export const chainFirst = f => chain ( a => pipe ( f ( a ) , map ( ( ) => a ) ) ) ; since export const of = some ; since export const filterOrElse = < A > ( predicate ) : ( ( ma : ObservableOption < A > ) => ObservableOption < A > ) => chain ( a => ( predicate ( a ) ? of ( a ) : none ) ) ; since export const fromOption = < A > ( ma ) : ObservableOption < A > => ( ma . _tag === 'None' ? none : of ( ma . value ) ) ; since export const fromPredicate = < A > ( predicate ) => ( a ) : ObservableOption < A > => ( predicate ( a ) ? of ( a ) : none ) ; const map_ = ( fa , f ) => pipe ( fa , map ( f ) ) ; const ap_ = ( fab , fa ) => pipe ( fab , ap ( fa ) ) ; const chain_ = ( ma , f ) => pipe ( ma , chain ( f ) ) ; const alt_ = ( fx , fy ) => pipe ( fx , alt ( fy ) ) ; category since export const URI = 'ObservableOption' ; category since export type URI = typeof URI ; declare module 'fp-ts/lib/HKT' { interface URItoKind < A > { readonly [ URI ] : ObservableOption < A > ; } } 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 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 } ; since export const Do = of ( { } ) ; since export const bindTo = < K extends string , A > ( name ) : ( ( fa : ObservableOption < A > ) => ObservableOption < { [ 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 : ObservableOption < A > ) => ObservableOption < { [ P in keyof A | K ] : P extends keyof A ? A [ P ] : B ; } > ) => chain ( a => pipe ( f ( a ) , map ( b => ( { ... a , [ name ] : b } as any ) ) ) ) ;