Created
November 4, 2019 11:59
-
-
Save effe-megna/e8d5d0a984934a0e242a7ecf14741037 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Reader } from 'fp-ts/lib/Reader' | |
| import { Monad3 } from 'fp-ts/lib/Monad' | |
| import * as RD from "@devexperts/remote-data-ts" | |
| import * as T from "fp-ts/lib/Task" | |
| import { monadTaskRemoteData, TaskRemoteData, TRD } from "./TaskRemoteData" | |
| import { pipe, pipeable } from "fp-ts/lib/pipeable" | |
| import { sequenceT } from 'fp-ts/lib/Apply' | |
| declare module 'fp-ts/lib/HKT' { | |
| interface URItoKind3<R, E, A> { | |
| ReaderTaskRemoteData: ReaderTaskRemoteData<R, E, A> | |
| } | |
| } | |
| export interface ReaderTaskRemoteData<R, E, A> extends Reader<R, TaskRemoteData<E, A>> { } | |
| export const monadReaderTaskRemoteData: Monad3<'ReaderTaskRemoteData'> = { | |
| URI: 'ReaderTaskRemoteData', | |
| of: (a) => { | |
| return (r) => { | |
| return monadTaskRemoteData.of(a) | |
| } | |
| }, | |
| map: <R, E, A, B>(fa: ReaderTaskRemoteData<R, E, A>, f: (a: A) => B) => { | |
| return (r: R) => { | |
| return pipe( | |
| fa(r), | |
| T.map(a => pipe( | |
| a, | |
| RD.fold( | |
| () => RD.initial, | |
| () => RD.pending, | |
| (e) => RD.failure(e), | |
| (v) => RD.remoteData.of(f(v)) | |
| ) | |
| )) | |
| ) | |
| } | |
| }, | |
| chain: <R, E, A, B>(fa: ReaderTaskRemoteData<R, E, A>, f: (a: A) => ReaderTaskRemoteData<R, E, B>) => { | |
| return (r: R) => pipe( | |
| fa(r), | |
| TRD.chain(a => f(a)(r)), | |
| TRD.chain(a => monadTaskRemoteData.of(a)) | |
| ) | |
| }, | |
| ap: (fab, fa) => { | |
| return (r) => pipe( | |
| sequenceT(monadTaskRemoteData)( | |
| fab(r), | |
| fa(r) | |
| ), | |
| TRD.map(([toB, a]) => toB(a)) | |
| ) | |
| } | |
| } | |
| export const RTRD = pipeable(monadReaderTaskRemoteData) | |
| const fetchFirstName = (): ReaderTaskRemoteData<{ baseUrl: string }, Error, string> => { | |
| return ({ baseUrl }) => monadTaskRemoteData.of(baseUrl) | |
| } | |
| const fetchLastName = (): ReaderTaskRemoteData<{ baseUrl: string }, Error, string> => { | |
| return ({ baseUrl }) => monadTaskRemoteData.of("123") | |
| } | |
| const a = pipe( | |
| sequenceT(monadReaderTaskRemoteData)( | |
| fetchFirstName(), | |
| fetchLastName() | |
| ) | |
| ) | |
| a({ baseUrl: "123" })().then(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment