0.1.13 #18
@@ -1,32 +1,18 @@
|
||||
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 { ReffuseExtension, type ReffuseNamespace } from "reffuse"
|
||||
import { MutationRunner } from "./internal/index.js"
|
||||
import type * as MutationService from "./MutationService.js"
|
||||
import * as QueryClient from "./QueryClient.js"
|
||||
import type * as QueryProgress from "./QueryProgress.js"
|
||||
import type * as QueryService from "./QueryService.js"
|
||||
import { MutationRunner, QueryRunner } from "./internal/index.js"
|
||||
import * as QueryRunner from "./QueryRunner.js"
|
||||
|
||||
|
||||
export interface UseQueryProps<K extends readonly unknown[], A, E, R> {
|
||||
readonly key: Stream.Stream<K>
|
||||
readonly query: (key: K) => Effect.Effect<A, E, R | QueryProgress.QueryProgress>
|
||||
readonly refreshOnWindowFocus?: boolean
|
||||
}
|
||||
|
||||
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>
|
||||
readonly options?: QueryRunner.RunOptions
|
||||
}
|
||||
|
||||
|
||||
@@ -61,32 +47,16 @@ export const QueryExtension = ReffuseExtension.make(() => ({
|
||||
>(
|
||||
this: ReffuseNamespace.ReffuseNamespace<R | QueryClient.TagClassShape<FallbackA, HandledE>>,
|
||||
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({
|
||||
QueryClient: QueryClient.makeGenericTagClass<FallbackA, HandledE>(),
|
||||
key: props.key,
|
||||
query: props.query,
|
||||
}), [props.key])
|
||||
|
||||
this.useFork(() => runner.fetchOnKeyChange, [runner])
|
||||
this.useFork(() => QueryRunner.run(runner, props.options), [runner])
|
||||
|
||||
this.useFork(() => (props.refreshOnWindowFocus ?? true)
|
||||
? 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])
|
||||
return runner
|
||||
},
|
||||
|
||||
useMutation<
|
||||
|
||||
@@ -10,7 +10,7 @@ export class QueryProgress extends Effect.Tag("@reffuse/extension-query/QueryPro
|
||||
f: (previous: Option.Option<AsyncData.Progress>) => AsyncData.Progress
|
||||
) => Effect.Effect<void>
|
||||
}>() {
|
||||
static readonly Live: Layer.Layer<
|
||||
static readonly Default: Layer.Layer<
|
||||
QueryProgress,
|
||||
never,
|
||||
QueryState.QueryState<any, any>
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { BrowserStream } from "@effect/platform-browser"
|
||||
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 * as QueryClient from "../QueryClient.js"
|
||||
import * as QueryProgress from "../QueryProgress.js"
|
||||
import * as QueryState from "./QueryState.js"
|
||||
import type * as QueryClient from "./QueryClient.js"
|
||||
import * as QueryProgress from "./QueryProgress.js"
|
||||
import { QueryState } from "./internal/index.js"
|
||||
|
||||
|
||||
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(QueryProgress.QueryProgress.Live),
|
||||
Effect.provide(QueryProgress.QueryProgress.Default),
|
||||
)
|
||||
|
||||
const forkFetch = Queue.unbounded<AsyncData.AsyncData<A | FallbackA, Exclude<E, HandledE>>>().pipe(
|
||||
@@ -1,5 +1,6 @@
|
||||
import type * as AsyncData from "@typed/async-data"
|
||||
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> {
|
||||
@@ -12,5 +13,5 @@ export interface QueryService<K extends readonly unknown[], A, E> {
|
||||
}
|
||||
|
||||
export const Tag = <const Id extends string>(id: Id) => <
|
||||
Self, K extends readonly unknown[], A, E = never,
|
||||
>() => Effect.Tag(id)<Self, QueryService<K, A, E>>()
|
||||
Self, K extends readonly unknown[], A, E = never, R = never,
|
||||
>() => Effect.Tag(id)<Self, QueryRunner.QueryRunner<K, A, E, R>>()
|
||||
|
||||
@@ -3,4 +3,5 @@ export * as QueryClient from "./QueryClient.js"
|
||||
export * as QueryErrorHandler from "./QueryErrorHandler.js"
|
||||
export * from "./QueryExtension.js"
|
||||
export * as QueryProgress from "./QueryProgress.js"
|
||||
export * as QueryRunner from "./QueryRunner.js"
|
||||
export * as QueryService from "./QueryService.js"
|
||||
|
||||
@@ -55,7 +55,7 @@ export const make = <K extends readonly unknown[], A, FallbackA, E, HandledE, R>
|
||||
)),
|
||||
|
||||
Effect.provide(context),
|
||||
Effect.provide(QueryProgress.QueryProgress.Live),
|
||||
Effect.provide(QueryProgress.QueryProgress.Default),
|
||||
)
|
||||
|
||||
const mutate = (...key: K) => Effect.provide(run(key), QueryState.layer(
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
export * as MutationRunner from "./MutationRunner.js"
|
||||
export * as QueryRunner from "./QueryRunner.js"
|
||||
export * as QueryState from "./QueryState.js"
|
||||
|
||||
Reference in New Issue
Block a user