126 lines
4.0 KiB
TypeScript
126 lines
4.0 KiB
TypeScript
import { BrowserStream } from "@effect/platform-browser"
|
|
import * as AsyncData from "@typed/async-data"
|
|
import { Effect, Fiber, identity, Option, Ref, Stream, SubscriptionRef } from "effect"
|
|
|
|
|
|
export interface QueryRunner<A, E, R> {
|
|
readonly queryRef: SubscriptionRef.SubscriptionRef<Effect.Effect<A, E, R>>
|
|
readonly stateRef: SubscriptionRef.SubscriptionRef<AsyncData.AsyncData<A, E>>
|
|
readonly fiberRef: SubscriptionRef.SubscriptionRef<Option.Option<Fiber.RuntimeFiber<void>>>
|
|
|
|
readonly forkInterrupt: Effect.Effect<Fiber.RuntimeFiber<void>>
|
|
readonly forkFetch: Effect.Effect<Fiber.RuntimeFiber<void>>
|
|
readonly forkRefresh: Effect.Effect<Fiber.RuntimeFiber<void>>
|
|
|
|
readonly refreshOnWindowFocus: Effect.Effect<void>
|
|
}
|
|
|
|
|
|
export interface MakeProps<A, E, R> {
|
|
readonly query: Effect.Effect<A, E, R>
|
|
}
|
|
|
|
export const make = <A, E, R>(
|
|
props: MakeProps<A, E, R>
|
|
): Effect.Effect<QueryRunner<A, E, R>, never, R> => Effect.gen(function*() {
|
|
const context = yield* Effect.context<R>()
|
|
|
|
const queryRef = yield* SubscriptionRef.make(props.query)
|
|
const stateRef = yield* SubscriptionRef.make(AsyncData.noData<A, E>())
|
|
const fiberRef = yield* SubscriptionRef.make(Option.none<Fiber.RuntimeFiber<void>>())
|
|
|
|
const interrupt = fiberRef.pipe(
|
|
Effect.flatMap(Option.match({
|
|
onSome: fiber => Ref.set(fiberRef, Option.none()).pipe(
|
|
Effect.andThen(Fiber.interrupt(fiber))
|
|
),
|
|
onNone: () => Effect.void,
|
|
}))
|
|
)
|
|
|
|
const forkInterrupt = fiberRef.pipe(
|
|
Effect.flatMap(Option.match({
|
|
onSome: fiber => Ref.set(fiberRef, Option.none()).pipe(
|
|
Effect.andThen(Fiber.interrupt(fiber).pipe(
|
|
Effect.asVoid,
|
|
Effect.forkDaemon,
|
|
))
|
|
),
|
|
onNone: () => Effect.forkDaemon(Effect.void),
|
|
}))
|
|
)
|
|
|
|
const forkFetch = interrupt.pipe(
|
|
Effect.andThen(
|
|
Ref.set(stateRef, AsyncData.loading()).pipe(
|
|
Effect.andThen(queryRef),
|
|
Effect.flatMap(identity),
|
|
Effect.matchCauseEffect({
|
|
onSuccess: v => Ref.set(stateRef, AsyncData.success(v)),
|
|
onFailure: c => Ref.set(stateRef, AsyncData.failure(c)),
|
|
}),
|
|
|
|
Effect.provide(context),
|
|
Effect.fork,
|
|
)
|
|
),
|
|
|
|
Effect.flatMap(fiber =>
|
|
Ref.set(fiberRef, Option.some(fiber)).pipe(
|
|
Effect.andThen(Fiber.join(fiber)),
|
|
Effect.andThen(Ref.set(fiberRef, Option.none())),
|
|
)
|
|
),
|
|
|
|
Effect.forkDaemon,
|
|
)
|
|
|
|
const forkRefresh = interrupt.pipe(
|
|
Effect.andThen(
|
|
Ref.update(stateRef, previous => {
|
|
if (AsyncData.isSuccess(previous) || AsyncData.isFailure(previous))
|
|
return AsyncData.refreshing(previous)
|
|
if (AsyncData.isRefreshing(previous))
|
|
return AsyncData.refreshing(previous.previous)
|
|
return AsyncData.loading()
|
|
}).pipe(
|
|
Effect.andThen(queryRef),
|
|
Effect.flatMap(identity),
|
|
Effect.matchCauseEffect({
|
|
onSuccess: v => Ref.set(stateRef, AsyncData.success(v)),
|
|
onFailure: c => Ref.set(stateRef, AsyncData.failure(c)),
|
|
}),
|
|
|
|
Effect.provide(context),
|
|
Effect.fork,
|
|
)
|
|
),
|
|
|
|
Effect.flatMap(fiber =>
|
|
Ref.set(fiberRef, Option.some(fiber)).pipe(
|
|
Effect.andThen(Fiber.join(fiber)),
|
|
Effect.andThen(Ref.set(fiberRef, Option.none())),
|
|
)
|
|
),
|
|
|
|
Effect.forkDaemon,
|
|
)
|
|
|
|
const refreshOnWindowFocus = Stream.runForEach(
|
|
BrowserStream.fromEventListenerWindow("focus"),
|
|
() => forkRefresh,
|
|
)
|
|
|
|
return {
|
|
queryRef,
|
|
stateRef,
|
|
fiberRef,
|
|
|
|
forkInterrupt,
|
|
forkFetch,
|
|
forkRefresh,
|
|
|
|
refreshOnWindowFocus,
|
|
}
|
|
})
|