Query refactoring
Some checks failed
Lint / lint (push) Failing after 12s

This commit is contained in:
Julien Valverdé
2025-05-22 19:49:40 +02:00
parent 619dbe32ae
commit 810e4bb9fd
7 changed files with 17 additions and 46 deletions

View File

@@ -1,32 +1,18 @@
import type * as AsyncData from "@typed/async-data" 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 { type Context, Effect, type Fiber, Layer, type Stream, type SubscriptionRef } from "effect"
import * as React from "react" import * as React from "react"
import { ReffuseExtension, type ReffuseNamespace } from "reffuse" import { ReffuseExtension, type ReffuseNamespace } from "reffuse"
import { MutationRunner } from "./internal/index.js"
import type * as MutationService from "./MutationService.js" import type * as MutationService from "./MutationService.js"
import * as QueryClient from "./QueryClient.js" import * as QueryClient from "./QueryClient.js"
import type * as QueryProgress from "./QueryProgress.js" import type * as QueryProgress from "./QueryProgress.js"
import type * as QueryService from "./QueryService.js" import * as QueryRunner from "./QueryRunner.js"
import { MutationRunner, QueryRunner } from "./internal/index.js"
export interface UseQueryProps<K extends readonly unknown[], A, E, R> { export interface UseQueryProps<K extends readonly unknown[], A, E, R> {
readonly key: Stream.Stream<K> readonly key: Stream.Stream<K>
readonly query: (key: K) => Effect.Effect<A, E, R | QueryProgress.QueryProgress> readonly query: (key: K) => Effect.Effect<A, E, R | QueryProgress.QueryProgress>
readonly refreshOnWindowFocus?: boolean readonly options?: QueryRunner.RunOptions
}
export interface UseQueryResult<K extends readonly unknown[], A, E> {
readonly latestKey: SubscriptionRef.SubscriptionRef<Option.Option<K>>
readonly state: SubscriptionRef.SubscriptionRef<AsyncData.AsyncData<A, E>>
readonly forkRefresh: Effect.Effect<readonly [
fiber: Fiber.RuntimeFiber<AsyncData.Success<A> | AsyncData.Failure<E>, Cause.NoSuchElementException>,
state: Stream.Stream<AsyncData.AsyncData<A, E>>,
]>
readonly layer: <Self, Id extends string>(
tag: Context.TagClass<Self, Id, QueryService.QueryService<K, A, E>>
) => Layer.Layer<Self>
} }
@@ -61,32 +47,16 @@ export const QueryExtension = ReffuseExtension.make(() => ({
>( >(
this: ReffuseNamespace.ReffuseNamespace<R | QueryClient.TagClassShape<FallbackA, HandledE>>, this: ReffuseNamespace.ReffuseNamespace<R | QueryClient.TagClassShape<FallbackA, HandledE>>,
props: UseQueryProps<QK, QA, QE, QR>, props: UseQueryProps<QK, QA, QE, QR>,
): UseQueryResult<QK, QA | FallbackA, Exclude<QE, HandledE>> { ): QueryRunner.QueryRunner<QK, QA | FallbackA, Exclude<QE, HandledE>, QR> {
const runner = this.useMemo(() => QueryRunner.make({ const runner = this.useMemo(() => QueryRunner.make({
QueryClient: QueryClient.makeGenericTagClass<FallbackA, HandledE>(), QueryClient: QueryClient.makeGenericTagClass<FallbackA, HandledE>(),
key: props.key, key: props.key,
query: props.query, query: props.query,
}), [props.key]) }), [props.key])
this.useFork(() => runner.fetchOnKeyChange, [runner]) this.useFork(() => QueryRunner.run(runner, props.options), [runner])
this.useFork(() => (props.refreshOnWindowFocus ?? true) return runner
? runner.refreshOnWindowFocus
: Effect.void,
[props.refreshOnWindowFocus, runner])
return React.useMemo(() => ({
latestKey: runner.latestKeyRef,
state: runner.stateRef,
forkRefresh: runner.forkRefresh,
layer: tag => Layer.succeed(tag, {
latestKey: runner.latestKeyRef,
state: runner.stateRef,
forkRefresh: runner.forkRefresh,
}),
}), [runner])
}, },
useMutation< useMutation<

View File

@@ -10,7 +10,7 @@ export class QueryProgress extends Effect.Tag("@reffuse/extension-query/QueryPro
f: (previous: Option.Option<AsyncData.Progress>) => AsyncData.Progress f: (previous: Option.Option<AsyncData.Progress>) => AsyncData.Progress
) => Effect.Effect<void> ) => Effect.Effect<void>
}>() { }>() {
static readonly Live: Layer.Layer< static readonly Default: Layer.Layer<
QueryProgress, QueryProgress,
never, never,
QueryState.QueryState<any, any> QueryState.QueryState<any, any>

View File

@@ -1,9 +1,9 @@
import { BrowserStream } from "@effect/platform-browser" import { BrowserStream } from "@effect/platform-browser"
import * as AsyncData from "@typed/async-data" import * as AsyncData from "@typed/async-data"
import { type Cause, type Context, Effect, Fiber, identity, Option, Queue, Ref, type Scope, Stream, SubscriptionRef } from "effect" import { type Cause, type Context, Effect, Fiber, identity, Option, Queue, Ref, type Scope, Stream, SubscriptionRef } from "effect"
import type * as QueryClient from "../QueryClient.js" import type * as QueryClient from "./QueryClient.js"
import * as QueryProgress from "../QueryProgress.js" import * as QueryProgress from "./QueryProgress.js"
import * as QueryState from "./QueryState.js" import { QueryState } from "./internal/index.js"
export interface QueryRunner<K extends readonly unknown[], A, E, R> { export interface QueryRunner<K extends readonly unknown[], A, E, R> {
@@ -97,7 +97,7 @@ export const make = <K extends readonly unknown[], A, FallbackA, E, HandledE, R>
)), )),
Effect.provide(context), Effect.provide(context),
Effect.provide(QueryProgress.QueryProgress.Live), Effect.provide(QueryProgress.QueryProgress.Default),
) )
const forkFetch = Queue.unbounded<AsyncData.AsyncData<A | FallbackA, Exclude<E, HandledE>>>().pipe( const forkFetch = Queue.unbounded<AsyncData.AsyncData<A | FallbackA, Exclude<E, HandledE>>>().pipe(

View File

@@ -1,5 +1,6 @@
import type * as AsyncData from "@typed/async-data" import type * as AsyncData from "@typed/async-data"
import { type Cause, Effect, type Fiber, type Option, type Stream, type SubscriptionRef } from "effect" import { type Cause, Effect, type Fiber, type Option, type Stream, type SubscriptionRef } from "effect"
import * as QueryRunner from "./QueryRunner.js"
export interface QueryService<K extends readonly unknown[], A, E> { export interface QueryService<K extends readonly unknown[], A, E> {
@@ -12,5 +13,5 @@ export interface QueryService<K extends readonly unknown[], A, E> {
} }
export const Tag = <const Id extends string>(id: Id) => < export const Tag = <const Id extends string>(id: Id) => <
Self, K extends readonly unknown[], A, E = never, Self, K extends readonly unknown[], A, E = never, R = never,
>() => Effect.Tag(id)<Self, QueryService<K, A, E>>() >() => Effect.Tag(id)<Self, QueryRunner.QueryRunner<K, A, E, R>>()

View File

@@ -3,4 +3,5 @@ export * as QueryClient from "./QueryClient.js"
export * as QueryErrorHandler from "./QueryErrorHandler.js" export * as QueryErrorHandler from "./QueryErrorHandler.js"
export * from "./QueryExtension.js" export * from "./QueryExtension.js"
export * as QueryProgress from "./QueryProgress.js" export * as QueryProgress from "./QueryProgress.js"
export * as QueryRunner from "./QueryRunner.js"
export * as QueryService from "./QueryService.js" export * as QueryService from "./QueryService.js"

View File

@@ -55,7 +55,7 @@ export const make = <K extends readonly unknown[], A, FallbackA, E, HandledE, R>
)), )),
Effect.provide(context), Effect.provide(context),
Effect.provide(QueryProgress.QueryProgress.Live), Effect.provide(QueryProgress.QueryProgress.Default),
) )
const mutate = (...key: K) => Effect.provide(run(key), QueryState.layer( const mutate = (...key: K) => Effect.provide(run(key), QueryState.layer(

View File

@@ -1,3 +1,2 @@
export * as MutationRunner from "./MutationRunner.js" export * as MutationRunner from "./MutationRunner.js"
export * as QueryRunner from "./QueryRunner.js"
export * as QueryState from "./QueryState.js" export * as QueryState from "./QueryState.js"