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"
|
||||
|
||||
|
||||
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> {
|
||||
readonly key: Stream.Stream<K>
|
||||
readonly query: (key: K) => Effect.Effect<A, E, R>
|
||||
|
||||
@@ -54,10 +54,7 @@ export const make = <K extends readonly unknown[], A, E, R>(
|
||||
}))
|
||||
)
|
||||
|
||||
const forkFetch = interrupt.pipe(
|
||||
Effect.andThen(
|
||||
Ref.set(stateRef, AsyncData.loading()).pipe(
|
||||
Effect.andThen(latestKeyRef),
|
||||
const run = latestKeyRef.pipe(
|
||||
Effect.flatMap(identity),
|
||||
Effect.flatMap(key => query(key).pipe(
|
||||
Effect.matchCauseEffect({
|
||||
@@ -67,9 +64,13 @@ export const make = <K extends readonly unknown[], A, E, R>(
|
||||
)),
|
||||
|
||||
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 =>
|
||||
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(
|
||||
Effect.andThen(
|
||||
Ref.update(stateRef, previous => {
|
||||
Effect.andThen(Ref.update(stateRef, previous => {
|
||||
if (AsyncData.isSuccess(previous) || AsyncData.isFailure(previous))
|
||||
return AsyncData.refreshing(previous)
|
||||
if (AsyncData.isRefreshing(previous))
|
||||
return AsyncData.refreshing(previous.previous)
|
||||
return AsyncData.loading()
|
||||
}).pipe(
|
||||
Effect.andThen(latestKeyRef),
|
||||
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.andThen(run),
|
||||
Effect.fork,
|
||||
)
|
||||
),
|
||||
)),
|
||||
|
||||
Effect.flatMap(fiber =>
|
||||
Ref.set(fiberRef, Option.some(fiber)).pipe(
|
||||
|
||||
Reference in New Issue
Block a user