Compare commits
3 Commits
ai-doc
...
66b8fd2c2e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66b8fd2c2e | ||
|
|
bc81c443ab | ||
|
|
ee5dbe3766 |
20
packages/extension-query/src/FailureHandler.ts
Normal file
20
packages/extension-query/src/FailureHandler.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { type Context, Effect, Layer, Queue, Stream } from "effect"
|
||||||
|
|
||||||
|
|
||||||
|
export interface FailureHandler<E> {
|
||||||
|
readonly failures: Stream.Stream<E>
|
||||||
|
readonly queue: Queue.Queue<E>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Tag = <const Id extends string>(id: Id) => <
|
||||||
|
Self, E = never,
|
||||||
|
>() => Effect.Tag(id)<Self, FailureHandler<E>>()
|
||||||
|
|
||||||
|
export const layer = <Self, Id extends string, E>(
|
||||||
|
tag: Context.TagClass<Self, Id, FailureHandler<E>>
|
||||||
|
): Layer.Layer<Self> => Layer.effect(tag, Queue.unbounded<E>().pipe(
|
||||||
|
Effect.map(queue => ({
|
||||||
|
failures: Stream.fromQueue(queue),
|
||||||
|
queue,
|
||||||
|
}))
|
||||||
|
))
|
||||||
@@ -6,6 +6,13 @@ import * as QueryRunner from "./QueryRunner.js"
|
|||||||
import type * as QueryService from "./QueryService.js"
|
import type * as QueryService from "./QueryService.js"
|
||||||
|
|
||||||
|
|
||||||
|
export interface QueryExtension<HandlerE> {
|
||||||
|
useQuery<K extends readonly unknown[], A, E, R>(
|
||||||
|
this: ReffuseHelpers.ReffuseHelpers<R>,
|
||||||
|
props: UseQueryProps<K, A, E, R>,
|
||||||
|
): UseQueryResult<K, A, Exclude<E, HandlerE>>
|
||||||
|
}
|
||||||
|
|
||||||
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>
|
readonly query: (key: K) => Effect.Effect<A, E, R>
|
||||||
|
|||||||
@@ -54,22 +54,23 @@ export const make = <K extends readonly unknown[], A, E, R>(
|
|||||||
}))
|
}))
|
||||||
)
|
)
|
||||||
|
|
||||||
const forkFetch = interrupt.pipe(
|
const run = latestKeyRef.pipe(
|
||||||
Effect.andThen(
|
Effect.flatMap(identity),
|
||||||
Ref.set(stateRef, AsyncData.loading()).pipe(
|
Effect.flatMap(key => query(key).pipe(
|
||||||
Effect.andThen(latestKeyRef),
|
Effect.matchCauseEffect({
|
||||||
Effect.flatMap(identity),
|
onSuccess: v => Ref.set(stateRef, AsyncData.success(v)),
|
||||||
Effect.flatMap(key => query(key).pipe(
|
onFailure: c => Ref.set(stateRef, AsyncData.failure(c)),
|
||||||
Effect.matchCauseEffect({
|
})
|
||||||
onSuccess: v => Ref.set(stateRef, AsyncData.success(v)),
|
)),
|
||||||
onFailure: c => Ref.set(stateRef, AsyncData.failure(c)),
|
|
||||||
})
|
|
||||||
)),
|
|
||||||
|
|
||||||
Effect.provide(context),
|
Effect.provide(context),
|
||||||
Effect.fork,
|
)
|
||||||
)
|
|
||||||
),
|
const forkFetch = interrupt.pipe(
|
||||||
|
Effect.andThen(Ref.set(stateRef, AsyncData.loading()).pipe(
|
||||||
|
Effect.andThen(run),
|
||||||
|
Effect.fork,
|
||||||
|
)),
|
||||||
|
|
||||||
Effect.flatMap(fiber =>
|
Effect.flatMap(fiber =>
|
||||||
Ref.set(fiberRef, Option.some(fiber)).pipe(
|
Ref.set(fiberRef, Option.some(fiber)).pipe(
|
||||||
@@ -82,27 +83,16 @@ export const make = <K extends readonly unknown[], A, E, R>(
|
|||||||
)
|
)
|
||||||
|
|
||||||
const forkRefresh = interrupt.pipe(
|
const forkRefresh = interrupt.pipe(
|
||||||
Effect.andThen(
|
Effect.andThen(Ref.update(stateRef, previous => {
|
||||||
Ref.update(stateRef, previous => {
|
if (AsyncData.isSuccess(previous) || AsyncData.isFailure(previous))
|
||||||
if (AsyncData.isSuccess(previous) || AsyncData.isFailure(previous))
|
return AsyncData.refreshing(previous)
|
||||||
return AsyncData.refreshing(previous)
|
if (AsyncData.isRefreshing(previous))
|
||||||
if (AsyncData.isRefreshing(previous))
|
return AsyncData.refreshing(previous.previous)
|
||||||
return AsyncData.refreshing(previous.previous)
|
return AsyncData.loading()
|
||||||
return AsyncData.loading()
|
}).pipe(
|
||||||
}).pipe(
|
Effect.andThen(run),
|
||||||
Effect.andThen(latestKeyRef),
|
Effect.fork,
|
||||||
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)),
|
|
||||||
})
|
|
||||||
)),
|
|
||||||
|
|
||||||
Effect.provide(context),
|
|
||||||
Effect.fork,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
|
|
||||||
Effect.flatMap(fiber =>
|
Effect.flatMap(fiber =>
|
||||||
Ref.set(fiberRef, Option.some(fiber)).pipe(
|
Ref.set(fiberRef, Option.some(fiber)).pipe(
|
||||||
|
|||||||
Reference in New Issue
Block a user