This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
import type * as AsyncData from "@typed/async-data"
|
||||
import { type Context, Effect, type Fiber, Layer, type Stream, type SubscriptionRef } from "effect"
|
||||
import * as React from "react"
|
||||
import { type Effect, type Stream } from "effect"
|
||||
import { ReffuseExtension, type ReffuseNamespace } from "reffuse"
|
||||
import { MutationRunner } from "./internal/index.js"
|
||||
import type * as MutationService from "./MutationService.js"
|
||||
import * as MutationRunner from "./MutationRunner.js"
|
||||
import * as QueryClient from "./QueryClient.js"
|
||||
import type * as QueryProgress from "./QueryProgress.js"
|
||||
import * as QueryRunner from "./QueryRunner.js"
|
||||
@@ -15,25 +12,10 @@ export interface UseQueryProps<K extends readonly unknown[], A, E, R> {
|
||||
readonly options?: QueryRunner.RunOptions
|
||||
}
|
||||
|
||||
|
||||
export interface UseMutationProps<K extends readonly unknown[], A, E, R> {
|
||||
readonly mutation: (key: K) => Effect.Effect<A, E, R | QueryProgress.QueryProgress>
|
||||
}
|
||||
|
||||
export interface UseMutationResult<K extends readonly unknown[], A, E> {
|
||||
readonly state: SubscriptionRef.SubscriptionRef<AsyncData.AsyncData<A, E>>
|
||||
|
||||
readonly mutate: (...key: K) => Effect.Effect<AsyncData.Success<A> | AsyncData.Failure<E>>
|
||||
readonly forkMutate: (...key: K) => Effect.Effect<readonly [
|
||||
fiber: Fiber.RuntimeFiber<AsyncData.Success<A> | AsyncData.Failure<E>>,
|
||||
state: Stream.Stream<AsyncData.AsyncData<A, E>>,
|
||||
]>
|
||||
|
||||
readonly layer: <Self, Id extends string>(
|
||||
tag: Context.TagClass<Self, Id, MutationService.MutationService<K, A, E>>
|
||||
) => Layer.Layer<Self>
|
||||
}
|
||||
|
||||
|
||||
export const QueryExtension = ReffuseExtension.make(() => ({
|
||||
useQuery<
|
||||
@@ -70,23 +52,10 @@ export const QueryExtension = ReffuseExtension.make(() => ({
|
||||
>(
|
||||
this: ReffuseNamespace.ReffuseNamespace<R | QueryClient.TagClassShape<FallbackA, HandledE>>,
|
||||
props: UseMutationProps<QK, QA, QE, QR>,
|
||||
): UseMutationResult<QK, QA | FallbackA, Exclude<QE, HandledE>> {
|
||||
const runner = this.useMemo(() => MutationRunner.make({
|
||||
): MutationRunner.MutationRunner<QK, QA | FallbackA, Exclude<QE, HandledE>> {
|
||||
return this.useMemo(() => MutationRunner.make({
|
||||
QueryClient: QueryClient.makeGenericTagClass<FallbackA, HandledE>(),
|
||||
mutation: props.mutation,
|
||||
}), [])
|
||||
|
||||
return React.useMemo(() => ({
|
||||
state: runner.stateRef,
|
||||
|
||||
mutate: runner.mutate,
|
||||
forkMutate: runner.forkMutate,
|
||||
|
||||
layer: tag => Layer.succeed(tag, {
|
||||
state: runner.stateRef,
|
||||
mutate: runner.mutate,
|
||||
forkMutate: runner.forkMutate,
|
||||
}),
|
||||
}), [runner])
|
||||
},
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user