@reffuse/extension-query 0.1.4 #15
@@ -3,7 +3,7 @@ import { type Cause, type Context, Effect, Layer, Queue, Stream } from "effect"
|
||||
|
||||
export interface ErrorHandler<E> {
|
||||
readonly errors: Stream.Stream<Cause.Cause<E>>
|
||||
readonly handle: <A, SelfE, R>(self: Effect.Effect<A, SelfE | E, R>) => Effect.Effect<A, SelfE, R>
|
||||
readonly handle: <A, SelfE, R>(self: Effect.Effect<A, SelfE, R>) => Effect.Effect<A, Exclude<SelfE, E>, R>
|
||||
}
|
||||
|
||||
export const Tag = <const Id extends string>(id: Id) => <
|
||||
@@ -17,10 +17,10 @@ export const layer = <Self, Id extends string, E>(
|
||||
const errors = Stream.fromQueue(queue)
|
||||
|
||||
const handle = <A, SelfE, R>(
|
||||
self: Effect.Effect<A, SelfE | E, R>
|
||||
self: Effect.Effect<A, SelfE, R>
|
||||
) => Effect.tapErrorCause(self, cause =>
|
||||
Queue.offer(queue, cause as Cause.Cause<E>)
|
||||
) as Effect.Effect<A, SelfE, R>
|
||||
) as Effect.Effect<A, Exclude<SelfE, E>, R>
|
||||
|
||||
return { errors, handle }
|
||||
}))
|
||||
|
||||
@@ -6,13 +6,9 @@ export interface QueryClient<EH, HandledE> {
|
||||
readonly ErrorHandler: Context.Tag<EH, ErrorHandler.ErrorHandler<HandledE>>
|
||||
}
|
||||
|
||||
export const makeTag = <
|
||||
EH = never,
|
||||
HandledE = never,
|
||||
>(): Context.Tag<
|
||||
QueryClient<EH, HandledE>,
|
||||
QueryClient<EH, HandledE>
|
||||
> => Context.GenericTag("@reffuse/extension-query/QueryClient")
|
||||
|
||||
export type Tag<EH, HandledE> = Context.Tag<QueryClient<EH, HandledE>, QueryClient<EH, HandledE>>
|
||||
export const makeTag = <EH = never, HandledE = never>(): Tag<EH, HandledE> => Context.GenericTag("@reffuse/extension-query/QueryClient")
|
||||
|
||||
|
||||
export interface LayerProps<EH, HandledE> {
|
||||
|
||||
@@ -2,7 +2,7 @@ import type * as AsyncData from "@typed/async-data"
|
||||
import { type Cause, type Context, Effect, type Fiber, Layer, type Option, type Stream, type SubscriptionRef } from "effect"
|
||||
import * as React from "react"
|
||||
import { ReffuseExtension, type ReffuseHelpers } from "reffuse"
|
||||
import type * as QueryClient from "./QueryClient.js"
|
||||
import * as QueryClient from "./QueryClient.js"
|
||||
import * as QueryRunner from "./QueryRunner.js"
|
||||
import type * as QueryService from "./QueryService.js"
|
||||
|
||||
@@ -26,10 +26,11 @@ export interface UseQueryResult<K extends readonly unknown[], A, E> {
|
||||
|
||||
export const QueryExtension = ReffuseExtension.make(() => ({
|
||||
useQuery<EH, K extends readonly unknown[], A, E, HandledE, R>(
|
||||
this: ReffuseHelpers.ReffuseHelpers<R | QueryClient.QueryClient<EH, HandledE>>,
|
||||
this: ReffuseHelpers.ReffuseHelpers<R | QueryClient.QueryClient<EH, HandledE> | EH>,
|
||||
props: UseQueryProps<K, A, E, R>,
|
||||
): UseQueryResult<K, A, Exclude<E, HandledE>> {
|
||||
const runner = this.useMemo(() => QueryRunner.make({
|
||||
QueryClient: QueryClient.makeTag<EH, HandledE>(),
|
||||
key: props.key,
|
||||
query: props.query,
|
||||
}), [props.key])
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { BrowserStream } from "@effect/platform-browser"
|
||||
import * as AsyncData from "@typed/async-data"
|
||||
import { type Cause, Effect, Fiber, identity, Option, Ref, type Scope, Stream, SubscriptionRef } from "effect"
|
||||
import { type Cause, type Context, Effect, Fiber, identity, Option, Ref, type Scope, Stream, SubscriptionRef } from "effect"
|
||||
import type * as QueryClient from "./QueryClient.js"
|
||||
|
||||
|
||||
export interface QueryRunner<K extends readonly unknown[], A, E, R> {
|
||||
readonly query: (key: K) => Effect.Effect<A, E, R>
|
||||
readonly context: Context.Context<R>
|
||||
|
||||
readonly latestKeyRef: SubscriptionRef.SubscriptionRef<Option.Option<K>>
|
||||
readonly stateRef: SubscriptionRef.SubscriptionRef<AsyncData.AsyncData<A, E>>
|
||||
@@ -19,18 +20,27 @@ export interface QueryRunner<K extends readonly unknown[], A, E, R> {
|
||||
}
|
||||
|
||||
|
||||
export interface MakeProps<K extends readonly unknown[], A, E, R> {
|
||||
export interface MakeProps<EH, K extends readonly unknown[], A, E, HandledE, R> {
|
||||
readonly QueryClient: QueryClient.Tag<EH, HandledE>
|
||||
readonly key: Stream.Stream<K>
|
||||
readonly query: (key: K) => Effect.Effect<A, E, R>
|
||||
}
|
||||
|
||||
export const make = <K extends readonly unknown[], A, E, R>(
|
||||
{ key, query }: MakeProps<K, A, E, R>
|
||||
): Effect.Effect<QueryRunner<K, A, E, R>, never, R> => Effect.gen(function*() {
|
||||
const context = yield* Effect.context<R>()
|
||||
export const make = <EH, K extends readonly unknown[], A, E, HandledE, R>(
|
||||
{
|
||||
QueryClient,
|
||||
key,
|
||||
query,
|
||||
}: MakeProps<EH, K, A, E, HandledE, R>
|
||||
): Effect.Effect<
|
||||
QueryRunner<K, A, Exclude<E, HandledE>, R>,
|
||||
never,
|
||||
R | QueryClient.QueryClient<EH, HandledE> | EH
|
||||
> => Effect.gen(function*() {
|
||||
const context = yield* Effect.context<R | QueryClient.QueryClient<EH, HandledE> | EH>()
|
||||
|
||||
const latestKeyRef = yield* SubscriptionRef.make(Option.none<K>())
|
||||
const stateRef = yield* SubscriptionRef.make(AsyncData.noData<A, E>())
|
||||
const stateRef = yield* SubscriptionRef.make(AsyncData.noData<A, Exclude<E, HandledE>>())
|
||||
const fiberRef = yield* SubscriptionRef.make(Option.none<Fiber.RuntimeFiber<void, Cause.NoSuchElementException>>())
|
||||
|
||||
const interrupt = fiberRef.pipe(
|
||||
@@ -54,13 +64,17 @@ export const make = <K extends readonly unknown[], A, E, R>(
|
||||
}))
|
||||
)
|
||||
|
||||
const run = latestKeyRef.pipe(
|
||||
Effect.flatMap(identity),
|
||||
Effect.flatMap(key => query(key).pipe(
|
||||
Effect.matchCauseEffect({
|
||||
onSuccess: v => Ref.set(stateRef, AsyncData.success(v)),
|
||||
onFailure: c => Ref.set(stateRef, AsyncData.failure(c)),
|
||||
})
|
||||
const run = QueryClient.pipe(
|
||||
Effect.flatMap(client => client.ErrorHandler),
|
||||
Effect.flatMap(errorHandler => latestKeyRef.pipe(
|
||||
Effect.flatMap(identity),
|
||||
Effect.flatMap(key => query(key).pipe(
|
||||
errorHandler.handle,
|
||||
Effect.matchCauseEffect({
|
||||
onSuccess: v => Ref.set(stateRef, AsyncData.success(v)),
|
||||
onFailure: c => Ref.set(stateRef, AsyncData.failure(c)),
|
||||
}),
|
||||
)),
|
||||
)),
|
||||
|
||||
Effect.provide(context),
|
||||
@@ -118,7 +132,7 @@ export const make = <K extends readonly unknown[], A, E, R>(
|
||||
)
|
||||
|
||||
return {
|
||||
query,
|
||||
context,
|
||||
|
||||
latestKeyRef,
|
||||
stateRef,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
export * as ErrorHandler from "./ErrorHandler.js"
|
||||
export * as QueryClient from "./QueryClient.js"
|
||||
export * from "./QueryExtension.js"
|
||||
export * as QueryRunner from "./QueryRunner.js"
|
||||
export * as QueryService from "./QueryService.js"
|
||||
|
||||
Reference in New Issue
Block a user