Created
August 22, 2022 10:40
-
-
Save mattiamanzati/9a6e66a519a50303e746c77c46e742f0 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 * as T from "@effect/core/io/Effect" | |
import type * as EX from "@effect/core/io/Exit" | |
import * as FID from "@effect/core/io/FiberId" | |
import * as HUB from "@effect/core/io/Hub" | |
import * as S from "@effect/core/stream/Stream" | |
import * as C from "@effect-ts/core/Case" | |
import { pipe } from "@tsplus/stdlib/data/Function" | |
import * as O from "@tsplus/stdlib/data/Maybe" | |
import * as ENV from "@tsplus/stdlib/service/Env" | |
import { Tag } from "@tsplus/stdlib/service/Tag" | |
import * as React from "react" | |
export const RetryPolicyContext = React.createContext( | |
<R, E, A>(eff: T.Effect<R, E, A>): T.Effect<R, never, A> => T.orDie(eff) | |
) | |
export function useEffect<R, E, A>(eff: T.Effect<R, E, A>) { | |
const retryPolicy = React.useContext(RetryPolicyContext) | |
return React.useCallback( | |
(args: ENV.Env<R>, cb?: (ex: EX.Exit<E, A>) => void) => { | |
const cancel = T.unsafeRunWith( | |
pipe(retryPolicy(eff), T.provideEnvironment(args)), | |
cb ? cb : () => undefined | |
) | |
return () => cancel(FID.none)(() => undefined) | |
}, | |
[eff, retryPolicy] | |
) | |
} | |
export function useEffectSingleton<R, E, A>(eff: T.Effect<R, E, A>) { | |
const [state, setState] = React.useState(0) | |
const _run = useEffect(eff) | |
const run = React.useCallback( | |
(args: ENV.Env<R>, cb?: (ex: EX.Exit<E, A>) => void) => { | |
setState((_) => _ + 1) | |
return _run(args, (exit) => { | |
if (cb) cb(exit) | |
setState((_) => _ - 1) | |
}) | |
}, | |
[_run] | |
) | |
return [run, state > 0] as const | |
} | |
export function useStreamLatestValue<A>(stream: S.Stream<never, never, A>): O.Maybe<A> { | |
const [state, setState] = React.useState<O.Maybe<A>>(O.none) | |
const eff = React.useMemo( | |
() => | |
pipe( | |
stream, | |
S.changesWith((a, b) => a === b || JSON.stringify(a) === JSON.stringify(b)), | |
S.mapEffect((value) => T.succeedWith(() => setState(O.some(value)))), | |
S.runDrain | |
), | |
[stream, setState] | |
) | |
const runInterrupt = useEffect(eff) | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
// eslint-disable-next-line @typescript-eslint/no-empty-function | |
React.useEffect(() => runInterrupt(ENV.Env.empty, () => {}), [eff, runInterrupt]) | |
return state | |
} | |
export class RefetchAllEvent extends C.Tagged("RefetchAllEvent")<{}> {} | |
export type Event = RefetchAllEvent | |
export interface EventHub extends HUB.Hub<Event> {} | |
export const EventHub = Tag<EventHub>() | |
export const EventHubContext = React.createContext<EventHub>(null as any) | |
export function EventHubProvider(props: React.PropsWithChildren<{}>) { | |
const [hub, setHub] = React.useState<O.Maybe<EventHub>>(O.none) | |
React.useEffect(() => { | |
T.unsafeRunWith( | |
pipe( | |
HUB.unbounded<Event>(), | |
T.map((h) => setHub(() => O.some(h))) | |
), | |
() => undefined | |
) | |
}, []) | |
if (O.isNone(hub)) return null | |
return React.createElement( | |
EventHubContext.Provider, | |
{ value: hub.value }, | |
props.children | |
) | |
} | |
export function invalidate<R, E, A>( | |
eff: T.Effect<R, E, A> | |
): T.Effect<R | EventHub, E, A> { | |
return pipe( | |
eff, | |
T.zipLeft( | |
T.serviceWithEffect(EventHub, (eventHub) => | |
eventHub.publish(new RefetchAllEvent()) | |
) | |
) | |
) | |
} | |
export function useQuery<R, E, A>( | |
fn: () => T.Effect<R, E, A>, | |
deps: any[] | |
): S.Stream<R, E, A> { | |
const eff = React.useMemo(fn, deps) | |
const eventHub = React.useContext(EventHubContext) | |
return React.useMemo( | |
() => | |
pipe( | |
S.succeed(new RefetchAllEvent()), | |
S.concat(S.fromHub(eventHub, 1)), | |
S.flatMapParSwitch(1, () => S.fromEffect(eff)) | |
), | |
[eff, eventHub] | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment