/** * RefDataEither is a collection of helpers for working with Refs that manage DataEither. * @since 0.12.1 */ import { flow, pipe } from 'fp-ts/function' import * as DE from './DataEither' import * as E from './Env' import * as EE from './EnvEither' import * as O from './Option' import { Progress } from './Progress' import * as Ref from './Ref' /** * @since 0.12.1 * @category Model */ export interface RefDataEither extends Ref.Ref> {} /** * @since 0.12.1 * @category Combinator */ export function toNoData(rd: RefDataEither) { return rd.update(() => E.of(DE.noData)) } /** * @since 0.12.1 * @category Combinator */ export function toLoading(rd: RefDataEither) { return rd.update(flow(DE.toLoading, E.of)) } /** * @since 0.12.1 * @category Combinator */ export function toRefresh(value: A, progress?: O.Option) { return (rd: RefDataEither) => rd.update(() => E.of(DE.refresh(value, progress))) } /** * @since 0.12.1 * @category Combinator */ export function toReplete(value: A) { return (rd: RefDataEither) => rd.update(() => E.of(DE.replete(value))) } /** * @since 0.12.1 * @category Combinator */ export function loadEnv(env: E.Env) { return (rd: RefDataEither) => pipe( rd, toLoading, E.chainW(() => rd.update(() => pipe(env, E.map(DE.replete)))), ) } /** * @since 0.12.1 * @category Combinator */ export function loadEnvEither(env: EE.EnvEither) { return (rd: RefDataEither) => pipe( rd, toLoading, E.chainW(() => rd.update(() => pipe(env, E.map(DE.fromEither)))), ) } /** * @since 0.12.1 * @category Combinator */ export const map = (f: (value: A) => B) => (ref: RefDataEither): Ref.Ref, DE.DataEither> => pipe(ref, Ref.map(DE.map(f)))