Fix
All checks were successful
Lint / lint (push) Successful in 13s

This commit is contained in:
Julien Valverdé
2025-03-12 06:37:39 +01:00
parent bc81c443ab
commit 66b8fd2c2e
2 changed files with 36 additions and 37 deletions

View File

@@ -1,4 +1,4 @@
import { Effect, type Queue, type Stream } from "effect" import { type Context, Effect, Layer, Queue, Stream } from "effect"
export interface FailureHandler<E> { export interface FailureHandler<E> {
@@ -9,3 +9,12 @@ export interface FailureHandler<E> {
export const Tag = <const Id extends string>(id: Id) => < export const Tag = <const Id extends string>(id: Id) => <
Self, E = never, Self, E = never,
>() => Effect.Tag(id)<Self, FailureHandler<E>>() >() => 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,
}))
))

View File

@@ -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(