Compare commits
4 Commits
16a7dec3fd
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e089bf9fee | ||
|
|
30b72b5b52 | ||
|
|
6dc0a548cd | ||
|
|
2a29f19ece |
@@ -1,16 +1,16 @@
|
|||||||
import { AlertDialog, Button, Flex, Text } from "@radix-ui/themes"
|
import { AlertDialog, Button, Flex, Text } from "@radix-ui/themes"
|
||||||
import { Cause, Console, Effect, Either, flow, Match, Option, Stream } from "effect"
|
import { Cause, Console, Effect, Either, flow, Match, Option, Stream } from "effect"
|
||||||
import { useState } from "react"
|
import { useState } from "react"
|
||||||
import { AppQueryClient } from "./query"
|
|
||||||
import { R } from "./reffuse"
|
import { R } from "./reffuse"
|
||||||
|
import { AppQueryErrorHandler } from "./services"
|
||||||
|
|
||||||
|
|
||||||
export function VQueryErrorHandler() {
|
export function VQueryErrorHandler() {
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
|
|
||||||
const error = R.useSubscribeStream(
|
const error = R.useSubscribeStream(
|
||||||
R.useMemo(() => AppQueryClient.pipe(
|
R.useMemo(() => AppQueryErrorHandler.AppQueryErrorHandler.pipe(
|
||||||
Effect.map(client => client.errorHandler.errors.pipe(
|
Effect.map(handler => handler.errors.pipe(
|
||||||
Stream.changes,
|
Stream.changes,
|
||||||
Stream.tap(Console.error),
|
Stream.tap(Console.error),
|
||||||
Stream.tap(() => Effect.sync(() => setOpen(true))),
|
Stream.tap(() => Effect.sync(() => setOpen(true))),
|
||||||
|
|||||||
@@ -5,14 +5,14 @@ import { Layer } from "effect"
|
|||||||
import { StrictMode } from "react"
|
import { StrictMode } from "react"
|
||||||
import { createRoot } from "react-dom/client"
|
import { createRoot } from "react-dom/client"
|
||||||
import { ReffuseRuntime } from "reffuse"
|
import { ReffuseRuntime } from "reffuse"
|
||||||
import { AppQueryClient, AppQueryErrorHandler } from "./query"
|
|
||||||
import { RootContext } from "./reffuse"
|
import { RootContext } from "./reffuse"
|
||||||
import { routeTree } from "./routeTree.gen"
|
import { routeTree } from "./routeTree.gen"
|
||||||
|
import { AppQueryClient, AppQueryErrorHandler } from "./services"
|
||||||
|
|
||||||
|
|
||||||
const layer = Layer.empty.pipe(
|
const layer = Layer.empty.pipe(
|
||||||
Layer.provideMerge(AppQueryClient.Live),
|
Layer.provideMerge(AppQueryClient.AppQueryClient.Default),
|
||||||
Layer.provideMerge(AppQueryErrorHandler.Live),
|
Layer.provideMerge(AppQueryErrorHandler.AppQueryErrorHandler.Default),
|
||||||
Layer.provideMerge(Clipboard.layer),
|
Layer.provideMerge(Clipboard.layer),
|
||||||
Layer.provideMerge(Geolocation.layer),
|
Layer.provideMerge(Geolocation.layer),
|
||||||
Layer.provideMerge(Permissions.layer),
|
Layer.provideMerge(Permissions.layer),
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
import { HttpClientError } from "@effect/platform"
|
|
||||||
import { QueryClient, QueryErrorHandler } from "@reffuse/extension-query"
|
|
||||||
import { Effect } from "effect"
|
|
||||||
|
|
||||||
|
|
||||||
export class AppQueryErrorHandler extends QueryErrorHandler.Service<AppQueryErrorHandler,
|
|
||||||
HttpClientError.HttpClientError
|
|
||||||
>()(
|
|
||||||
"AppQueryErrorHandler",
|
|
||||||
|
|
||||||
(self, failure, defect) => self.pipe(
|
|
||||||
Effect.catchTag("RequestError", "ResponseError", failure),
|
|
||||||
Effect.catchAllDefect(defect),
|
|
||||||
),
|
|
||||||
) {}
|
|
||||||
|
|
||||||
export class AppQueryClient extends QueryClient.Service<AppQueryClient>()({ ErrorHandler: AppQueryErrorHandler }) {}
|
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
import { QueryService } from "@reffuse/extension-query"
|
import { QueryRunner } from "@reffuse/extension-query"
|
||||||
import { ParseResult, Schema } from "effect"
|
import { ParseResult, Schema } from "effect"
|
||||||
|
|
||||||
|
|
||||||
export const Result = Schema.Array(Schema.String)
|
export const Result = Schema.Array(Schema.String)
|
||||||
|
|
||||||
export class Uuid4Query extends QueryService.Tag("Uuid4Query")<Uuid4Query,
|
export class Uuid4Query extends QueryRunner.Tag("Uuid4Query")<Uuid4Query,
|
||||||
readonly ["uuid4", number],
|
readonly ["uuid4", number],
|
||||||
typeof Result.Type,
|
typeof Result.Type,
|
||||||
ParseResult.ParseError
|
ParseResult.ParseError
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ export function Uuid4QueryService() {
|
|||||||
const runFork = R.useRunFork()
|
const runFork = R.useRunFork()
|
||||||
|
|
||||||
const query = R.useMemo(() => Uuid4Query.Uuid4Query, [])
|
const query = R.useMemo(() => Uuid4Query.Uuid4Query, [])
|
||||||
const [state] = R.useRefState(query.state)
|
const [state] = R.useSubscribeRefs(query.stateRef)
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -3,11 +3,12 @@ import { Clipboard, Geolocation, Permissions } from "@effect/platform-browser"
|
|||||||
import { LazyRefExtension } from "@reffuse/extension-lazyref"
|
import { LazyRefExtension } from "@reffuse/extension-lazyref"
|
||||||
import { QueryExtension } from "@reffuse/extension-query"
|
import { QueryExtension } from "@reffuse/extension-query"
|
||||||
import { Reffuse, ReffuseContext } from "reffuse"
|
import { Reffuse, ReffuseContext } from "reffuse"
|
||||||
import { AppQueryClient } from "./query"
|
import { AppQueryClient, AppQueryErrorHandler } from "./services"
|
||||||
|
|
||||||
|
|
||||||
export const RootContext = ReffuseContext.make<
|
export const RootContext = ReffuseContext.make<
|
||||||
| AppQueryClient
|
| AppQueryClient.AppQueryClient
|
||||||
|
| AppQueryErrorHandler.AppQueryErrorHandler
|
||||||
| Clipboard.Clipboard
|
| Clipboard.Clipboard
|
||||||
| Geolocation.Geolocation
|
| Geolocation.Geolocation
|
||||||
| Permissions.Permissions
|
| Permissions.Permissions
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Uuid4QueryService } from "@/query/views/Uuid4QueryService"
|
|||||||
import { R } from "@/reffuse"
|
import { R } from "@/reffuse"
|
||||||
import { HttpClient } from "@effect/platform"
|
import { HttpClient } from "@effect/platform"
|
||||||
import { createFileRoute } from "@tanstack/react-router"
|
import { createFileRoute } from "@tanstack/react-router"
|
||||||
import { Console, Effect, Schema } from "effect"
|
import { Console, Effect, Layer, Schema } from "effect"
|
||||||
import { useMemo } from "react"
|
import { useMemo } from "react"
|
||||||
|
|
||||||
|
|
||||||
@@ -17,15 +17,18 @@ function RouteComponent() {
|
|||||||
key: R.useStreamFromReactiveValues(["uuid4", 10 as number]),
|
key: R.useStreamFromReactiveValues(["uuid4", 10 as number]),
|
||||||
query: ([, count]) => Console.log(`Querying ${ count } IDs...`).pipe(
|
query: ([, count]) => Console.log(`Querying ${ count } IDs...`).pipe(
|
||||||
Effect.andThen(Effect.sleep("500 millis")),
|
Effect.andThen(Effect.sleep("500 millis")),
|
||||||
Effect.andThen(HttpClient.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)),
|
Effect.andThen(Effect.map(
|
||||||
|
HttpClient.HttpClient,
|
||||||
HttpClient.withTracerPropagation(false),
|
HttpClient.withTracerPropagation(false),
|
||||||
|
)),
|
||||||
|
Effect.flatMap(client => client.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)),
|
||||||
Effect.flatMap(res => res.json),
|
Effect.flatMap(res => res.json),
|
||||||
Effect.flatMap(Schema.decodeUnknown(Uuid4Query.Result)),
|
Effect.flatMap(Schema.decodeUnknown(Uuid4Query.Result)),
|
||||||
Effect.scoped,
|
Effect.scoped,
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
|
|
||||||
const layer = useMemo(() => query.layer(Uuid4Query.Uuid4Query), [query])
|
const layer = useMemo(() => Layer.succeed(Uuid4Query.Uuid4Query, query), [query])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<QueryContext.Provider layer={layer}>
|
<QueryContext.Provider layer={layer}>
|
||||||
|
|||||||
@@ -29,15 +29,18 @@ function RouteComponent() {
|
|||||||
Effect.tap(() => QueryProgress.QueryProgress.update(() =>
|
Effect.tap(() => QueryProgress.QueryProgress.update(() =>
|
||||||
AsyncData.Progress.make({ loaded: 50, total: Option.some(100) })
|
AsyncData.Progress.make({ loaded: 50, total: Option.some(100) })
|
||||||
)),
|
)),
|
||||||
Effect.andThen(HttpClient.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)),
|
Effect.andThen(Effect.map(
|
||||||
|
HttpClient.HttpClient,
|
||||||
HttpClient.withTracerPropagation(false),
|
HttpClient.withTracerPropagation(false),
|
||||||
|
)),
|
||||||
|
Effect.flatMap(client => client.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)),
|
||||||
Effect.flatMap(res => res.json),
|
Effect.flatMap(res => res.json),
|
||||||
Effect.flatMap(Schema.decodeUnknown(Result)),
|
Effect.flatMap(Schema.decodeUnknown(Result)),
|
||||||
Effect.scoped,
|
Effect.scoped,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
const [state] = R.useSubscribeRefs(mutation.state)
|
const [state] = R.useSubscribeRefs(mutation.stateRef)
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -23,15 +23,18 @@ function RouteComponent() {
|
|||||||
key: R.useStreamFromReactiveValues(["uuid4", count]),
|
key: R.useStreamFromReactiveValues(["uuid4", count]),
|
||||||
query: ([, count]) => Console.log(`Querying ${ count } IDs...`).pipe(
|
query: ([, count]) => Console.log(`Querying ${ count } IDs...`).pipe(
|
||||||
Effect.andThen(Effect.sleep("500 millis")),
|
Effect.andThen(Effect.sleep("500 millis")),
|
||||||
Effect.andThen(HttpClient.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)),
|
Effect.andThen(Effect.map(
|
||||||
|
HttpClient.HttpClient,
|
||||||
HttpClient.withTracerPropagation(false),
|
HttpClient.withTracerPropagation(false),
|
||||||
|
)),
|
||||||
|
Effect.flatMap(client => client.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)),
|
||||||
Effect.flatMap(res => res.json),
|
Effect.flatMap(res => res.json),
|
||||||
Effect.flatMap(Schema.decodeUnknown(Result)),
|
Effect.flatMap(Schema.decodeUnknown(Result)),
|
||||||
Effect.scoped,
|
Effect.scoped,
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
|
|
||||||
const [state] = R.useSubscribeRefs(query.state)
|
const [state] = R.useSubscribeRefs(query.stateRef)
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -6,6 +6,16 @@ import { Console, Effect, Option } from "effect"
|
|||||||
import { useEffect, useState } from "react"
|
import { useEffect, useState } from "react"
|
||||||
|
|
||||||
|
|
||||||
|
interface Node {
|
||||||
|
value: string
|
||||||
|
left?: Leaf
|
||||||
|
right?: Leaf
|
||||||
|
}
|
||||||
|
interface Leaf {
|
||||||
|
node: Node
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const makeUuid = Effect.provide(makeUuid4, GetRandomValues.CryptoRandom)
|
const makeUuid = Effect.provide(makeUuid4, GetRandomValues.CryptoRandom)
|
||||||
|
|
||||||
|
|
||||||
@@ -32,6 +42,11 @@ function RouteComponent() {
|
|||||||
runSync,
|
runSync,
|
||||||
), [scopeLayer, runSync])
|
), [scopeLayer, runSync])
|
||||||
|
|
||||||
|
|
||||||
|
const nodeRef = R.useRef(() => Effect.succeed<Node>({ value: "prout" }))
|
||||||
|
const nodeValueRef = R.useSubRefFromPath(nodeRef, ["value"])
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Flex direction="column" justify="center" align="center" gap="2">
|
<Flex direction="column" justify="center" align="center" gap="2">
|
||||||
<Text>{uuid}</Text>
|
<Text>{uuid}</Text>
|
||||||
|
|||||||
7
packages/example/src/services/AppQueryClient.ts
Normal file
7
packages/example/src/services/AppQueryClient.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { QueryClient } from "@reffuse/extension-query"
|
||||||
|
import * as AppQueryErrorHandler from "./AppQueryErrorHandler"
|
||||||
|
|
||||||
|
|
||||||
|
export class AppQueryClient extends QueryClient.Service<AppQueryClient>()({
|
||||||
|
errorHandler: AppQueryErrorHandler.AppQueryErrorHandler
|
||||||
|
}) {}
|
||||||
13
packages/example/src/services/AppQueryErrorHandler.ts
Normal file
13
packages/example/src/services/AppQueryErrorHandler.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { HttpClientError } from "@effect/platform"
|
||||||
|
import { QueryErrorHandler } from "@reffuse/extension-query"
|
||||||
|
import { Effect } from "effect"
|
||||||
|
|
||||||
|
|
||||||
|
export class AppQueryErrorHandler extends Effect.Service<AppQueryErrorHandler>()("AppQueryErrorHandler", {
|
||||||
|
effect: QueryErrorHandler.make<HttpClientError.HttpClientError>()(
|
||||||
|
(self, failure, defect) => self.pipe(
|
||||||
|
Effect.catchTag("RequestError", "ResponseError", failure),
|
||||||
|
Effect.catchAllDefect(defect),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}) {}
|
||||||
@@ -1 +1,2 @@
|
|||||||
export {}
|
export * as AppQueryClient from "./AppQueryClient"
|
||||||
|
export * as AppQueryErrorHandler from "./AppQueryErrorHandler"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@reffuse/extension-query",
|
"name": "@reffuse/extension-query",
|
||||||
"version": "0.1.3",
|
"version": "0.1.5",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"files": [
|
"files": [
|
||||||
"./README.md",
|
"./README.md",
|
||||||
@@ -39,6 +39,6 @@
|
|||||||
"@types/react": "^19.0.0",
|
"@types/react": "^19.0.0",
|
||||||
"effect": "^3.15.0",
|
"effect": "^3.15.0",
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
"reffuse": "^0.1.6"
|
"reffuse": "^0.1.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
import * as AsyncData from "@typed/async-data"
|
import * as AsyncData from "@typed/async-data"
|
||||||
import { type Context, Effect, type Fiber, Queue, Ref, Stream, SubscriptionRef } from "effect"
|
import { Effect, type Fiber, Queue, Ref, 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 MutationRunner<K extends readonly unknown[], A, E, R> {
|
export interface MutationRunner<K extends readonly unknown[], A, E> {
|
||||||
readonly context: Context.Context<R>
|
|
||||||
readonly stateRef: SubscriptionRef.SubscriptionRef<AsyncData.AsyncData<A, E>>
|
readonly stateRef: SubscriptionRef.SubscriptionRef<AsyncData.AsyncData<A, E>>
|
||||||
|
|
||||||
readonly mutate: (...key: K) => Effect.Effect<AsyncData.Success<A> | AsyncData.Failure<E>>
|
readonly mutate: (...key: K) => Effect.Effect<AsyncData.Success<A> | AsyncData.Failure<E>>
|
||||||
@@ -17,6 +16,11 @@ export interface MutationRunner<K extends readonly unknown[], A, E, R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const Tag = <const Id extends string>(id: Id) => <
|
||||||
|
Self, K extends readonly unknown[], A, E = never,
|
||||||
|
>() => Effect.Tag(id)<Self, MutationRunner<K, A, E>>()
|
||||||
|
|
||||||
|
|
||||||
export interface MakeProps<K extends readonly unknown[], A, FallbackA, E, HandledE, R> {
|
export interface MakeProps<K extends readonly unknown[], A, FallbackA, E, HandledE, R> {
|
||||||
readonly QueryClient: QueryClient.GenericTagClass<FallbackA, HandledE>
|
readonly QueryClient: QueryClient.GenericTagClass<FallbackA, HandledE>
|
||||||
readonly mutation: (key: K) => Effect.Effect<A, E, R | QueryProgress.QueryProgress>
|
readonly mutation: (key: K) => Effect.Effect<A, E, R | QueryProgress.QueryProgress>
|
||||||
@@ -28,7 +32,7 @@ export const make = <K extends readonly unknown[], A, FallbackA, E, HandledE, R>
|
|||||||
mutation,
|
mutation,
|
||||||
}: MakeProps<K, A, FallbackA, E, HandledE, R>
|
}: MakeProps<K, A, FallbackA, E, HandledE, R>
|
||||||
): Effect.Effect<
|
): Effect.Effect<
|
||||||
MutationRunner<K, A | FallbackA, Exclude<E, HandledE>, R>,
|
MutationRunner<K, A | FallbackA, Exclude<E, HandledE>>,
|
||||||
never,
|
never,
|
||||||
R | QueryClient.TagClassShape<FallbackA, HandledE>
|
R | QueryClient.TagClassShape<FallbackA, HandledE>
|
||||||
> => Effect.gen(function*() {
|
> => Effect.gen(function*() {
|
||||||
@@ -37,25 +41,18 @@ export const make = <K extends readonly unknown[], A, FallbackA, E, HandledE, R>
|
|||||||
|
|
||||||
const queryStateTag = QueryState.makeTag<A | FallbackA, Exclude<E, HandledE>>()
|
const queryStateTag = QueryState.makeTag<A | FallbackA, Exclude<E, HandledE>>()
|
||||||
|
|
||||||
const run = (key: K) => Effect.Do.pipe(
|
const run = (key: K) => Effect.all([QueryClient, queryStateTag]).pipe(
|
||||||
Effect.bind("state", () => queryStateTag),
|
Effect.flatMap(([client, state]) => state.set(AsyncData.loading()).pipe(
|
||||||
Effect.bind("client", () => QueryClient),
|
|
||||||
|
|
||||||
Effect.flatMap(({ state, client }) => state.set(AsyncData.loading()).pipe(
|
|
||||||
Effect.andThen(mutation(key)),
|
Effect.andThen(mutation(key)),
|
||||||
client.errorHandler.handle,
|
client.errorHandler.handle,
|
||||||
Effect.matchCauseEffect({
|
Effect.matchCauseEffect({
|
||||||
onSuccess: v => Effect.succeed(AsyncData.success(v)).pipe(
|
onSuccess: v => Effect.tap(Effect.succeed(AsyncData.success(v)), state.set),
|
||||||
Effect.tap(state.set)
|
onFailure: c => Effect.tap(Effect.succeed(AsyncData.failure(c)), state.set),
|
||||||
),
|
|
||||||
onFailure: c => Effect.succeed(AsyncData.failure(c)).pipe(
|
|
||||||
Effect.tap(state.set)
|
|
||||||
),
|
|
||||||
}),
|
}),
|
||||||
)),
|
)),
|
||||||
|
|
||||||
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(
|
||||||
@@ -64,11 +61,11 @@ export const make = <K extends readonly unknown[], A, FallbackA, E, HandledE, R>
|
|||||||
value => Ref.set(globalStateRef, value),
|
value => Ref.set(globalStateRef, value),
|
||||||
))
|
))
|
||||||
|
|
||||||
const forkMutate = (...key: K) => Effect.Do.pipe(
|
const forkMutate = (...key: K) => Effect.all([
|
||||||
Effect.bind("stateRef", () => Ref.make(AsyncData.noData<A | FallbackA, Exclude<E, HandledE>>())),
|
Ref.make(AsyncData.noData<A | FallbackA, Exclude<E, HandledE>>()),
|
||||||
Effect.bind("stateQueue", () => Queue.unbounded<AsyncData.AsyncData<A | FallbackA, Exclude<E, HandledE>>>()),
|
Queue.unbounded<AsyncData.AsyncData<A | FallbackA, Exclude<E, HandledE>>>(),
|
||||||
|
]).pipe(
|
||||||
Effect.flatMap(({ stateRef, stateQueue }) =>
|
Effect.flatMap(([stateRef, stateQueue]) =>
|
||||||
Effect.addFinalizer(() => Queue.shutdown(stateQueue)).pipe(
|
Effect.addFinalizer(() => Queue.shutdown(stateQueue)).pipe(
|
||||||
Effect.andThen(run(key)),
|
Effect.andThen(run(key)),
|
||||||
Effect.scoped,
|
Effect.scoped,
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
import type * as AsyncData from "@typed/async-data"
|
|
||||||
import { Effect, type Fiber, type Stream, type SubscriptionRef } from "effect"
|
|
||||||
|
|
||||||
|
|
||||||
export interface MutationService<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>>,
|
|
||||||
]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export const Tag = <const Id extends string>(id: Id) => <
|
|
||||||
Self, K extends readonly unknown[], A, E = never,
|
|
||||||
>() => Effect.Tag(id)<Self, MutationService<K, A, E>>()
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Context, Effect, Layer } from "effect"
|
import { Context, Effect, identity, Layer } from "effect"
|
||||||
import type { Mutable } from "effect/Types"
|
import type { Mutable } from "effect/Types"
|
||||||
import * as QueryErrorHandler from "./QueryErrorHandler.js"
|
import * as QueryErrorHandler from "./QueryErrorHandler.js"
|
||||||
|
|
||||||
@@ -8,6 +8,17 @@ export interface QueryClient<FallbackA, HandledE> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export interface MakeProps<FallbackA, HandledE> {
|
||||||
|
readonly errorHandler: QueryErrorHandler.QueryErrorHandler<FallbackA, HandledE>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const make = <FallbackA, HandledE>(
|
||||||
|
{ errorHandler }: MakeProps<FallbackA, HandledE>
|
||||||
|
): Effect.Effect<QueryClient<FallbackA, HandledE>> => Effect.Do.pipe(
|
||||||
|
Effect.let("errorHandler", () => errorHandler)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
const id = "@reffuse/extension-query/QueryClient"
|
const id = "@reffuse/extension-query/QueryClient"
|
||||||
|
|
||||||
export type TagClassShape<FallbackA, HandledE> = Context.TagClassShape<typeof id, QueryClient<FallbackA, HandledE>>
|
export type TagClassShape<FallbackA, HandledE> = Context.TagClassShape<typeof id, QueryClient<FallbackA, HandledE>>
|
||||||
@@ -19,46 +30,28 @@ export type GenericTagClass<FallbackA, HandledE> = Context.TagClass<
|
|||||||
export const makeGenericTagClass = <FallbackA = never, HandledE = never>(): GenericTagClass<FallbackA, HandledE> => Context.Tag(id)()
|
export const makeGenericTagClass = <FallbackA = never, HandledE = never>(): GenericTagClass<FallbackA, HandledE> => Context.Tag(id)()
|
||||||
|
|
||||||
|
|
||||||
export interface ServiceProps<EH, FallbackA, HandledE> {
|
export interface ServiceProps<FallbackA = never, HandledE = never, E = never, R = never> {
|
||||||
readonly ErrorHandler?: Context.Tag<EH, QueryErrorHandler.QueryErrorHandler<FallbackA, HandledE>>
|
readonly errorHandler?: Effect.Effect<QueryErrorHandler.QueryErrorHandler<FallbackA, HandledE>, E, R>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ServiceResult<Self, EH, FallbackA, HandledE> extends Context.TagClass<
|
export interface ServiceResult<Self, FallbackA, HandledE, E, R> extends Context.TagClass<
|
||||||
Self,
|
Self,
|
||||||
typeof id,
|
typeof id,
|
||||||
QueryClient<FallbackA, HandledE>
|
QueryClient<FallbackA, HandledE>
|
||||||
> {
|
> {
|
||||||
readonly Live: Layer.Layer<
|
readonly Default: Layer.Layer<Self, E, R>
|
||||||
Self | (EH extends QueryErrorHandler.DefaultQueryErrorHandler ? EH : never),
|
|
||||||
never,
|
|
||||||
EH extends QueryErrorHandler.DefaultQueryErrorHandler ? never : EH
|
|
||||||
>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Service = <Self>() => (
|
export const Service = <Self>() => (
|
||||||
<
|
<FallbackA = never, HandledE = never, E = never, R = never>(
|
||||||
EH = QueryErrorHandler.DefaultQueryErrorHandler,
|
props?: ServiceProps<FallbackA, HandledE, E, R>
|
||||||
FallbackA = QueryErrorHandler.Fallback<Context.Tag.Service<QueryErrorHandler.DefaultQueryErrorHandler>>,
|
): ServiceResult<Self, FallbackA, HandledE, E, R> => {
|
||||||
HandledE = QueryErrorHandler.Error<Context.Tag.Service<QueryErrorHandler.DefaultQueryErrorHandler>>,
|
const TagClass = Context.Tag(id)() as ServiceResult<Self, FallbackA, HandledE, E, R>
|
||||||
>(
|
|
||||||
props?: ServiceProps<EH, FallbackA, HandledE>
|
|
||||||
): ServiceResult<Self, EH, FallbackA, HandledE> => {
|
|
||||||
const TagClass = Context.Tag(id)() as ServiceResult<Self, EH, FallbackA, HandledE>
|
|
||||||
|
|
||||||
(TagClass as Mutable<typeof TagClass>).Live = Layer.effect(TagClass, Effect.Do.pipe(
|
(TagClass as Mutable<typeof TagClass>).Default = Layer.effect(TagClass, Effect.flatMap(
|
||||||
Effect.bind("errorHandler", () =>
|
props?.errorHandler ?? QueryErrorHandler.make<never>()(identity),
|
||||||
(props?.ErrorHandler ?? QueryErrorHandler.DefaultQueryErrorHandler) as Effect.Effect<
|
errorHandler => make({ errorHandler }),
|
||||||
QueryErrorHandler.QueryErrorHandler<FallbackA, HandledE>,
|
))
|
||||||
never,
|
|
||||||
EH extends QueryErrorHandler.DefaultQueryErrorHandler ? never : EH
|
|
||||||
>
|
|
||||||
)
|
|
||||||
)).pipe(
|
|
||||||
Layer.provideMerge((props?.ErrorHandler
|
|
||||||
? Layer.empty
|
|
||||||
: QueryErrorHandler.DefaultQueryErrorHandler.Live
|
|
||||||
) as Layer.Layer<EH>)
|
|
||||||
)
|
|
||||||
|
|
||||||
return TagClass
|
return TagClass
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Cause, Context, Effect, identity, Layer, PubSub, Stream } from "effect"
|
import { Cause, Effect, PubSub, Stream } from "effect"
|
||||||
import type { Mutable } from "effect/Types"
|
|
||||||
|
|
||||||
|
|
||||||
export interface QueryErrorHandler<FallbackA, HandledE> {
|
export interface QueryErrorHandler<FallbackA, HandledE> {
|
||||||
@@ -11,31 +10,14 @@ export type Fallback<T> = T extends QueryErrorHandler<infer A, any> ? A : never
|
|||||||
export type Error<T> = T extends QueryErrorHandler<any, infer E> ? E : never
|
export type Error<T> = T extends QueryErrorHandler<any, infer E> ? E : never
|
||||||
|
|
||||||
|
|
||||||
export interface ServiceResult<
|
export const make = <HandledE = never>() => (
|
||||||
Self,
|
<FallbackA>(
|
||||||
Id extends string,
|
|
||||||
FallbackA,
|
|
||||||
HandledE,
|
|
||||||
> extends Context.TagClass<
|
|
||||||
Self,
|
|
||||||
Id,
|
|
||||||
QueryErrorHandler<FallbackA, HandledE>
|
|
||||||
> {
|
|
||||||
readonly Live: Layer.Layer<Self>
|
|
||||||
}
|
|
||||||
|
|
||||||
export const Service = <Self, HandledE = never>() => (
|
|
||||||
<const Id extends string, FallbackA>(
|
|
||||||
id: Id,
|
|
||||||
f: (
|
f: (
|
||||||
self: Effect.Effect<never, HandledE>,
|
self: Effect.Effect<never, HandledE>,
|
||||||
failure: (failure: HandledE) => Effect.Effect<never>,
|
failure: (failure: HandledE) => Effect.Effect<never>,
|
||||||
defect: (defect: unknown) => Effect.Effect<never>,
|
defect: (defect: unknown) => Effect.Effect<never>,
|
||||||
) => Effect.Effect<FallbackA>,
|
) => Effect.Effect<FallbackA>
|
||||||
): ServiceResult<Self, Id, FallbackA, HandledE> => {
|
): Effect.Effect<QueryErrorHandler<FallbackA, HandledE>> => Effect.gen(function*() {
|
||||||
const TagClass = Context.Tag(id)() as ServiceResult<Self, Id, FallbackA, HandledE>
|
|
||||||
|
|
||||||
(TagClass as Mutable<typeof TagClass>).Live = Layer.effect(TagClass, Effect.gen(function*() {
|
|
||||||
const pubsub = yield* PubSub.unbounded<Cause.Cause<HandledE>>()
|
const pubsub = yield* PubSub.unbounded<Cause.Cause<HandledE>>()
|
||||||
const errors = Stream.fromPubSub(pubsub)
|
const errors = Stream.fromPubSub(pubsub)
|
||||||
|
|
||||||
@@ -43,23 +25,16 @@ export const Service = <Self, HandledE = never>() => (
|
|||||||
self: Effect.Effect<A, E, R>
|
self: Effect.Effect<A, E, R>
|
||||||
): Effect.Effect<A | FallbackA, Exclude<E, HandledE>, R> => f(
|
): Effect.Effect<A | FallbackA, Exclude<E, HandledE>, R> => f(
|
||||||
self as unknown as Effect.Effect<never, HandledE, never>,
|
self as unknown as Effect.Effect<never, HandledE, never>,
|
||||||
(failure: HandledE) => PubSub.publish(pubsub, Cause.fail(failure)).pipe(
|
(failure: HandledE) => Effect.andThen(
|
||||||
Effect.andThen(Effect.failCause(Cause.empty))
|
PubSub.publish(pubsub, Cause.fail(failure)),
|
||||||
|
Effect.failCause(Cause.empty),
|
||||||
),
|
),
|
||||||
(defect: unknown) => PubSub.publish(pubsub, Cause.die(defect)).pipe(
|
(defect: unknown) => Effect.andThen(
|
||||||
Effect.andThen(Effect.failCause(Cause.empty))
|
PubSub.publish(pubsub, Cause.die(defect)),
|
||||||
|
Effect.failCause(Cause.empty),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
return { errors, handle }
|
return { errors, handle }
|
||||||
}))
|
})
|
||||||
|
|
||||||
return TagClass
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
export class DefaultQueryErrorHandler extends Service<DefaultQueryErrorHandler>()(
|
|
||||||
"@reffuse/extension-query/DefaultQueryErrorHandler",
|
|
||||||
identity,
|
|
||||||
) {}
|
|
||||||
|
|||||||
@@ -1,53 +1,21 @@
|
|||||||
import type * as AsyncData from "@typed/async-data"
|
import type { Effect, Stream } from "effect"
|
||||||
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 ReffuseNamespace } from "reffuse"
|
import { ReffuseExtension, type ReffuseNamespace } from "reffuse"
|
||||||
import type * as MutationService from "./MutationService.js"
|
import * as MutationRunner from "./MutationRunner.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>
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export interface UseMutationProps<K extends readonly unknown[], A, E, R> {
|
export interface UseMutationProps<K extends readonly unknown[], A, E, R> {
|
||||||
readonly mutation: (key: K) => Effect.Effect<A, E, R | QueryProgress.QueryProgress>
|
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(() => ({
|
export const QueryExtension = ReffuseExtension.make(() => ({
|
||||||
useQuery<
|
useQuery<
|
||||||
@@ -61,32 +29,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>> {
|
||||||
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<
|
||||||
@@ -100,23 +52,10 @@ export const QueryExtension = ReffuseExtension.make(() => ({
|
|||||||
>(
|
>(
|
||||||
this: ReffuseNamespace.ReffuseNamespace<R | QueryClient.TagClassShape<FallbackA, HandledE>>,
|
this: ReffuseNamespace.ReffuseNamespace<R | QueryClient.TagClassShape<FallbackA, HandledE>>,
|
||||||
props: UseMutationProps<QK, QA, QE, QR>,
|
props: UseMutationProps<QK, QA, QE, QR>,
|
||||||
): UseMutationResult<QK, QA | FallbackA, Exclude<QE, HandledE>> {
|
): MutationRunner.MutationRunner<QK, QA | FallbackA, Exclude<QE, HandledE>> {
|
||||||
const runner = this.useMemo(() => MutationRunner.make({
|
return this.useMemo(() => MutationRunner.make({
|
||||||
QueryClient: QueryClient.makeGenericTagClass<FallbackA, HandledE>(),
|
QueryClient: QueryClient.makeGenericTagClass<FallbackA, HandledE>(),
|
||||||
mutation: props.mutation,
|
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])
|
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
193
packages/extension-query/src/QueryRunner.ts
Normal file
193
packages/extension-query/src/QueryRunner.ts
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
import { BrowserStream } from "@effect/platform-browser"
|
||||||
|
import * as AsyncData from "@typed/async-data"
|
||||||
|
import { type Cause, 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 { QueryState } from "./internal/index.js"
|
||||||
|
|
||||||
|
|
||||||
|
export interface QueryRunner<K extends readonly unknown[], A, E> {
|
||||||
|
readonly queryKey: Stream.Stream<K>
|
||||||
|
readonly latestKeyValueRef: SubscriptionRef.SubscriptionRef<Option.Option<K>>
|
||||||
|
readonly stateRef: SubscriptionRef.SubscriptionRef<AsyncData.AsyncData<A, E>>
|
||||||
|
readonly fiberRef: SubscriptionRef.SubscriptionRef<Option.Option<Fiber.RuntimeFiber<
|
||||||
|
AsyncData.Success<A> | AsyncData.Failure<E>,
|
||||||
|
Cause.NoSuchElementException
|
||||||
|
>>>
|
||||||
|
|
||||||
|
readonly interrupt: Effect.Effect<void>
|
||||||
|
readonly forkInterrupt: Effect.Effect<Fiber.RuntimeFiber<void>>
|
||||||
|
readonly forkFetch: (keyValue: K) => Effect.Effect<readonly [
|
||||||
|
fiber: Fiber.RuntimeFiber<AsyncData.Success<A> | AsyncData.Failure<E>>,
|
||||||
|
state: Stream.Stream<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>>,
|
||||||
|
]>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const Tag = <const Id extends string>(id: Id) => <
|
||||||
|
Self, K extends readonly unknown[], A, E = never
|
||||||
|
>() => Effect.Tag(id)<Self, QueryRunner<K, A, E>>()
|
||||||
|
|
||||||
|
|
||||||
|
export interface MakeProps<K extends readonly unknown[], A, FallbackA, E, HandledE, R> {
|
||||||
|
readonly QueryClient: QueryClient.GenericTagClass<FallbackA, HandledE>
|
||||||
|
readonly key: Stream.Stream<K>
|
||||||
|
readonly query: (key: K) => Effect.Effect<A, E, R | QueryProgress.QueryProgress>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const make = <K extends readonly unknown[], A, FallbackA, E, HandledE, R>(
|
||||||
|
{
|
||||||
|
QueryClient,
|
||||||
|
key,
|
||||||
|
query,
|
||||||
|
}: MakeProps<K, A, FallbackA, E, HandledE, R>
|
||||||
|
): Effect.Effect<
|
||||||
|
QueryRunner<K, A | FallbackA, Exclude<E, HandledE>>,
|
||||||
|
never,
|
||||||
|
R | QueryClient.TagClassShape<FallbackA, HandledE>
|
||||||
|
> => Effect.gen(function*() {
|
||||||
|
const context = yield* Effect.context<R | QueryClient.TagClassShape<FallbackA, HandledE>>()
|
||||||
|
|
||||||
|
const latestKeyValueRef = yield* SubscriptionRef.make(Option.none<K>())
|
||||||
|
const stateRef = yield* SubscriptionRef.make(AsyncData.noData<A | FallbackA, Exclude<E, HandledE>>())
|
||||||
|
const fiberRef = yield* SubscriptionRef.make(Option.none<Fiber.RuntimeFiber<
|
||||||
|
AsyncData.Success<A | FallbackA> | AsyncData.Failure<Exclude<E, HandledE>>,
|
||||||
|
Cause.NoSuchElementException
|
||||||
|
>>())
|
||||||
|
|
||||||
|
const queryStateTag = QueryState.makeTag<A | FallbackA, Exclude<E, HandledE>>()
|
||||||
|
|
||||||
|
const interrupt = Effect.flatMap(fiberRef, Option.match({
|
||||||
|
onSome: fiber => Ref.set(fiberRef, Option.none()).pipe(
|
||||||
|
Effect.andThen(Fiber.interrupt(fiber))
|
||||||
|
),
|
||||||
|
onNone: () => Effect.void,
|
||||||
|
}))
|
||||||
|
|
||||||
|
const forkInterrupt = Effect.flatMap(fiberRef, 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 run = (keyValue: K) => Effect.all([QueryClient, queryStateTag]).pipe(
|
||||||
|
Effect.flatMap(([client, state]) => Ref.set(latestKeyValueRef, Option.some(keyValue)).pipe(
|
||||||
|
Effect.andThen(query(keyValue)),
|
||||||
|
client.errorHandler.handle,
|
||||||
|
Effect.matchCauseEffect({
|
||||||
|
onSuccess: v => Effect.tap(Effect.succeed(AsyncData.success(v)), state.set),
|
||||||
|
onFailure: c => Effect.tap(Effect.succeed(AsyncData.failure(c)), state.set),
|
||||||
|
}),
|
||||||
|
)),
|
||||||
|
|
||||||
|
Effect.provide(context),
|
||||||
|
Effect.provide(QueryProgress.QueryProgress.Default),
|
||||||
|
)
|
||||||
|
|
||||||
|
const forkFetch = (keyValue: K) => Queue.unbounded<AsyncData.AsyncData<A | FallbackA, Exclude<E, HandledE>>>().pipe(
|
||||||
|
Effect.flatMap(stateQueue => queryStateTag.pipe(
|
||||||
|
Effect.flatMap(state => interrupt.pipe(
|
||||||
|
Effect.andThen(
|
||||||
|
Effect.addFinalizer(() => Effect.andThen(
|
||||||
|
Ref.set(fiberRef, Option.none()),
|
||||||
|
Queue.shutdown(stateQueue),
|
||||||
|
)).pipe(
|
||||||
|
Effect.andThen(state.set(AsyncData.loading())),
|
||||||
|
Effect.andThen(run(keyValue)),
|
||||||
|
Effect.scoped,
|
||||||
|
Effect.forkDaemon,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
|
||||||
|
Effect.tap(fiber => Ref.set(fiberRef, Option.some(fiber))),
|
||||||
|
Effect.map(fiber => [fiber, Stream.fromQueue(stateQueue)] as const),
|
||||||
|
)),
|
||||||
|
|
||||||
|
Effect.provide(QueryState.layer(
|
||||||
|
queryStateTag,
|
||||||
|
stateRef,
|
||||||
|
value => Effect.andThen(
|
||||||
|
Queue.offer(stateQueue, value),
|
||||||
|
Ref.set(stateRef, value),
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
))
|
||||||
|
)
|
||||||
|
|
||||||
|
const setInitialRefreshState = Effect.flatMap(queryStateTag, state => state.update(previous => {
|
||||||
|
if (AsyncData.isSuccess(previous) || AsyncData.isFailure(previous))
|
||||||
|
return AsyncData.refreshing(previous)
|
||||||
|
if (AsyncData.isRefreshing(previous))
|
||||||
|
return AsyncData.refreshing(previous.previous)
|
||||||
|
return AsyncData.loading()
|
||||||
|
}))
|
||||||
|
|
||||||
|
const forkRefresh = Queue.unbounded<AsyncData.AsyncData<A | FallbackA, Exclude<E, HandledE>>>().pipe(
|
||||||
|
Effect.flatMap(stateQueue => interrupt.pipe(
|
||||||
|
Effect.andThen(
|
||||||
|
Effect.addFinalizer(() => Effect.andThen(
|
||||||
|
Ref.set(fiberRef, Option.none()),
|
||||||
|
Queue.shutdown(stateQueue),
|
||||||
|
)).pipe(
|
||||||
|
Effect.andThen(setInitialRefreshState),
|
||||||
|
Effect.andThen(latestKeyValueRef.pipe(
|
||||||
|
Effect.flatMap(identity),
|
||||||
|
Effect.flatMap(run),
|
||||||
|
)),
|
||||||
|
Effect.scoped,
|
||||||
|
Effect.forkDaemon,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
|
||||||
|
Effect.tap(fiber => Ref.set(fiberRef, Option.some(fiber))),
|
||||||
|
Effect.map(fiber => [fiber, Stream.fromQueue(stateQueue)] as const),
|
||||||
|
|
||||||
|
Effect.provide(QueryState.layer(
|
||||||
|
queryStateTag,
|
||||||
|
stateRef,
|
||||||
|
value => Effect.andThen(
|
||||||
|
Queue.offer(stateQueue, value),
|
||||||
|
Ref.set(stateRef, value),
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
))
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
queryKey: key,
|
||||||
|
latestKeyValueRef,
|
||||||
|
stateRef,
|
||||||
|
fiberRef,
|
||||||
|
|
||||||
|
interrupt,
|
||||||
|
forkInterrupt,
|
||||||
|
forkFetch,
|
||||||
|
forkRefresh,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
export interface RunOptions {
|
||||||
|
readonly refreshOnWindowFocus?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export const run = <K extends readonly unknown[], A, E>(
|
||||||
|
self: QueryRunner<K, A, E>,
|
||||||
|
options?: RunOptions,
|
||||||
|
): Effect.Effect<void, never, Scope.Scope> => Effect.gen(function*() {
|
||||||
|
if (typeof window !== "undefined" && (options?.refreshOnWindowFocus ?? true))
|
||||||
|
yield* Effect.forkScoped(
|
||||||
|
Stream.runForEach(BrowserStream.fromEventListenerWindow("focus"), () => self.forkRefresh)
|
||||||
|
)
|
||||||
|
|
||||||
|
yield* Effect.addFinalizer(() => self.interrupt)
|
||||||
|
yield* Stream.runForEach(Stream.changes(self.queryKey), latestKey => self.forkFetch(latestKey))
|
||||||
|
})
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
import type * as AsyncData from "@typed/async-data"
|
|
||||||
import { type Cause, Effect, type Fiber, type Option, type Stream, type SubscriptionRef } from "effect"
|
|
||||||
|
|
||||||
|
|
||||||
export interface QueryService<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>>,
|
|
||||||
]>
|
|
||||||
}
|
|
||||||
|
|
||||||
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>>()
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
export * as MutationService from "./MutationService.js"
|
export * as MutationRunner from "./MutationRunner.js"
|
||||||
export * as QueryClient from "./QueryClient.js"
|
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 QueryService from "./QueryService.js"
|
export * as QueryRunner from "./QueryRunner.js"
|
||||||
|
|||||||
@@ -1,191 +0,0 @@
|
|||||||
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"
|
|
||||||
|
|
||||||
|
|
||||||
export interface QueryRunner<K extends readonly unknown[], A, E, R> {
|
|
||||||
readonly context: Context.Context<R>
|
|
||||||
|
|
||||||
readonly latestKeyRef: SubscriptionRef.SubscriptionRef<Option.Option<K>>
|
|
||||||
readonly stateRef: SubscriptionRef.SubscriptionRef<AsyncData.AsyncData<A, E>>
|
|
||||||
readonly fiberRef: SubscriptionRef.SubscriptionRef<Option.Option<Fiber.RuntimeFiber<
|
|
||||||
AsyncData.Success<A> | AsyncData.Failure<E>,
|
|
||||||
Cause.NoSuchElementException
|
|
||||||
>>>
|
|
||||||
|
|
||||||
readonly forkInterrupt: Effect.Effect<Fiber.RuntimeFiber<void, Cause.NoSuchElementException>>
|
|
||||||
readonly forkFetch: Effect.Effect<readonly [
|
|
||||||
fiber: Fiber.RuntimeFiber<AsyncData.Success<A> | AsyncData.Failure<E>, Cause.NoSuchElementException>,
|
|
||||||
state: Stream.Stream<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 fetchOnKeyChange: Effect.Effect<void, Cause.NoSuchElementException, Scope.Scope>
|
|
||||||
readonly refreshOnWindowFocus: Effect.Effect<void>
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export interface MakeProps<K extends readonly unknown[], A, FallbackA, E, HandledE, R> {
|
|
||||||
readonly QueryClient: QueryClient.GenericTagClass<FallbackA, HandledE>
|
|
||||||
readonly key: Stream.Stream<K>
|
|
||||||
readonly query: (key: K) => Effect.Effect<A, E, R | QueryProgress.QueryProgress>
|
|
||||||
}
|
|
||||||
|
|
||||||
export const make = <K extends readonly unknown[], A, FallbackA, E, HandledE, R>(
|
|
||||||
{
|
|
||||||
QueryClient,
|
|
||||||
key,
|
|
||||||
query,
|
|
||||||
}: MakeProps<K, A, FallbackA, E, HandledE, R>
|
|
||||||
): Effect.Effect<
|
|
||||||
QueryRunner<K, A | FallbackA, Exclude<E, HandledE>, R>,
|
|
||||||
never,
|
|
||||||
R | QueryClient.TagClassShape<FallbackA, HandledE>
|
|
||||||
> => Effect.gen(function*() {
|
|
||||||
const context = yield* Effect.context<R | QueryClient.TagClassShape<FallbackA, HandledE>>()
|
|
||||||
|
|
||||||
const latestKeyRef = yield* SubscriptionRef.make(Option.none<K>())
|
|
||||||
const stateRef = yield* SubscriptionRef.make(AsyncData.noData<A | FallbackA, Exclude<E, HandledE>>())
|
|
||||||
const fiberRef = yield* SubscriptionRef.make(Option.none<Fiber.RuntimeFiber<
|
|
||||||
AsyncData.Success<A | FallbackA> | AsyncData.Failure<Exclude<E, HandledE>>,
|
|
||||||
Cause.NoSuchElementException
|
|
||||||
>>())
|
|
||||||
|
|
||||||
const queryStateTag = QueryState.makeTag<A | FallbackA, Exclude<E, HandledE>>()
|
|
||||||
|
|
||||||
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 run = Effect.Do.pipe(
|
|
||||||
Effect.bind("state", () => queryStateTag),
|
|
||||||
Effect.bind("client", () => QueryClient),
|
|
||||||
Effect.bind("latestKey", () => latestKeyRef.pipe(Effect.flatMap(identity))),
|
|
||||||
|
|
||||||
Effect.flatMap(({ state, client, latestKey }) => query(latestKey).pipe(
|
|
||||||
client.errorHandler.handle,
|
|
||||||
Effect.matchCauseEffect({
|
|
||||||
onSuccess: v => Effect.succeed(AsyncData.success(v)).pipe(
|
|
||||||
Effect.tap(state.set)
|
|
||||||
),
|
|
||||||
onFailure: c => Effect.succeed(AsyncData.failure(c)).pipe(
|
|
||||||
Effect.tap(state.set)
|
|
||||||
),
|
|
||||||
}),
|
|
||||||
)),
|
|
||||||
|
|
||||||
Effect.provide(context),
|
|
||||||
Effect.provide(QueryProgress.QueryProgress.Live),
|
|
||||||
)
|
|
||||||
|
|
||||||
const forkFetch = Queue.unbounded<AsyncData.AsyncData<A | FallbackA, Exclude<E, HandledE>>>().pipe(
|
|
||||||
Effect.flatMap(stateQueue => queryStateTag.pipe(
|
|
||||||
Effect.flatMap(state => interrupt.pipe(
|
|
||||||
Effect.andThen(Effect.addFinalizer(() => Ref.set(fiberRef, Option.none()).pipe(
|
|
||||||
Effect.andThen(Queue.shutdown(stateQueue))
|
|
||||||
)).pipe(
|
|
||||||
Effect.andThen(state.set(AsyncData.loading())),
|
|
||||||
Effect.andThen(run),
|
|
||||||
Effect.scoped,
|
|
||||||
Effect.forkDaemon,
|
|
||||||
)),
|
|
||||||
|
|
||||||
Effect.tap(fiber => Ref.set(fiberRef, Option.some(fiber))),
|
|
||||||
Effect.map(fiber => [fiber, Stream.fromQueue(stateQueue)] as const),
|
|
||||||
)),
|
|
||||||
|
|
||||||
Effect.provide(QueryState.layer(
|
|
||||||
queryStateTag,
|
|
||||||
stateRef,
|
|
||||||
value => Queue.offer(stateQueue, value).pipe(
|
|
||||||
Effect.andThen(Ref.set(stateRef, value))
|
|
||||||
),
|
|
||||||
)),
|
|
||||||
))
|
|
||||||
)
|
|
||||||
|
|
||||||
const setInitialRefreshState = queryStateTag.pipe(
|
|
||||||
Effect.flatMap(state => state.update(previous => {
|
|
||||||
if (AsyncData.isSuccess(previous) || AsyncData.isFailure(previous))
|
|
||||||
return AsyncData.refreshing(previous)
|
|
||||||
if (AsyncData.isRefreshing(previous))
|
|
||||||
return AsyncData.refreshing(previous.previous)
|
|
||||||
return AsyncData.loading()
|
|
||||||
}))
|
|
||||||
)
|
|
||||||
|
|
||||||
const forkRefresh = Queue.unbounded<AsyncData.AsyncData<A | FallbackA, Exclude<E, HandledE>>>().pipe(
|
|
||||||
Effect.flatMap(stateQueue => interrupt.pipe(
|
|
||||||
Effect.andThen(Effect.addFinalizer(() => Ref.set(fiberRef, Option.none()).pipe(
|
|
||||||
Effect.andThen(Queue.shutdown(stateQueue))
|
|
||||||
)).pipe(
|
|
||||||
Effect.andThen(setInitialRefreshState),
|
|
||||||
Effect.andThen(run),
|
|
||||||
Effect.scoped,
|
|
||||||
Effect.forkDaemon,
|
|
||||||
)),
|
|
||||||
|
|
||||||
Effect.tap(fiber => Ref.set(fiberRef, Option.some(fiber))),
|
|
||||||
Effect.map(fiber => [fiber, Stream.fromQueue(stateQueue)] as const),
|
|
||||||
|
|
||||||
Effect.provide(QueryState.layer(
|
|
||||||
queryStateTag,
|
|
||||||
stateRef,
|
|
||||||
value => Queue.offer(stateQueue, value).pipe(
|
|
||||||
Effect.andThen(Ref.set(stateRef, value))
|
|
||||||
),
|
|
||||||
)),
|
|
||||||
))
|
|
||||||
)
|
|
||||||
|
|
||||||
const fetchOnKeyChange = Effect.addFinalizer(() => interrupt).pipe(
|
|
||||||
Effect.andThen(Stream.runForEach(Stream.changes(key), latestKey =>
|
|
||||||
Ref.set(latestKeyRef, Option.some(latestKey)).pipe(
|
|
||||||
Effect.andThen(forkFetch)
|
|
||||||
)
|
|
||||||
))
|
|
||||||
)
|
|
||||||
|
|
||||||
const refreshOnWindowFocus = Stream.runForEach(
|
|
||||||
BrowserStream.fromEventListenerWindow("focus"),
|
|
||||||
() => forkRefresh,
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
context,
|
|
||||||
|
|
||||||
latestKeyRef,
|
|
||||||
stateRef,
|
|
||||||
fiberRef,
|
|
||||||
|
|
||||||
forkInterrupt,
|
|
||||||
forkFetch,
|
|
||||||
forkRefresh,
|
|
||||||
|
|
||||||
fetchOnKeyChange,
|
|
||||||
refreshOnWindowFocus,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
@@ -1,3 +1 @@
|
|||||||
export * as MutationRunner from "./MutationRunner.js"
|
|
||||||
export * as QueryRunner from "./QueryRunner.js"
|
|
||||||
export * as QueryState from "./QueryState.js"
|
export * as QueryState from "./QueryState.js"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "reffuse",
|
"name": "reffuse",
|
||||||
"version": "0.1.11",
|
"version": "0.1.13",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"files": [
|
"files": [
|
||||||
"./README.md",
|
"./README.md",
|
||||||
|
|||||||
@@ -1,24 +1,29 @@
|
|||||||
import { Array, Function, Option, Predicate } from "effect"
|
import { Array, Function, Option, Predicate } from "effect"
|
||||||
|
|
||||||
|
|
||||||
export type Paths<T> = [] | (
|
type Prev = readonly [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
T extends readonly any[] ? ArrayPaths<T> :
|
|
||||||
T extends object ? ObjectPaths<T> :
|
export type Paths<T, D extends number = 5, Seen = never> = [] | (
|
||||||
|
D extends never ? [] :
|
||||||
|
T extends Seen ? [] :
|
||||||
|
T extends readonly any[] ? ArrayPaths<T, D, Seen | T> :
|
||||||
|
T extends object ? ObjectPaths<T, D, Seen | T> :
|
||||||
never
|
never
|
||||||
)
|
)
|
||||||
|
|
||||||
export type ArrayPaths<T extends readonly any[]> = {
|
export type ArrayPaths<T extends readonly any[], D extends number, Seen> = {
|
||||||
[K in keyof T as K extends number ? K : never]:
|
[K in keyof T as K extends number ? K : never]:
|
||||||
| [K]
|
| [K]
|
||||||
| [K, ...Paths<T[K]>]
|
| [K, ...Paths<T[K], Prev[D], Seen>]
|
||||||
} extends infer O
|
} extends infer O
|
||||||
? O[keyof O]
|
? O[keyof O]
|
||||||
: never
|
: never
|
||||||
|
|
||||||
export type ObjectPaths<T extends object> = {
|
export type ObjectPaths<T extends object, D extends number, Seen> = {
|
||||||
[K in keyof T as K extends string | number | symbol ? K : never]:
|
[K in keyof T as K extends string | number | symbol ? K : never]-?:
|
||||||
| [K]
|
NonNullable<T[K]> extends infer V
|
||||||
| [K, ...Paths<T[K]>]
|
? [K] | [K, ...Paths<V, Prev[D], Seen>]
|
||||||
|
: never
|
||||||
} extends infer O
|
} extends infer O
|
||||||
? O[keyof O]
|
? O[keyof O]
|
||||||
: never
|
: never
|
||||||
|
|||||||
Reference in New Issue
Block a user