0.1.8 #11

Merged
Thilawyn merged 233 commits from next into master 2025-04-21 02:08:14 +02:00
2 changed files with 36 additions and 37 deletions
Showing only changes of commit 66b8fd2c2e - Show all commits

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,10 +54,7 @@ export const make = <K extends readonly unknown[], A, E, R>(
})) }))
) )
const forkFetch = interrupt.pipe( const run = latestKeyRef.pipe(
Effect.andThen(
Ref.set(stateRef, AsyncData.loading()).pipe(
Effect.andThen(latestKeyRef),
Effect.flatMap(identity), Effect.flatMap(identity),
Effect.flatMap(key => query(key).pipe( Effect.flatMap(key => query(key).pipe(
Effect.matchCauseEffect({ Effect.matchCauseEffect({
@@ -67,9 +64,13 @@ export const make = <K extends readonly unknown[], A, E, R>(
)), )),
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(latestKeyRef), Effect.andThen(run),
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.fork,
) )),
),
Effect.flatMap(fiber => Effect.flatMap(fiber =>
Ref.set(fiberRef, Option.some(fiber)).pipe( Ref.set(fiberRef, Option.some(fiber)).pipe(