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