0.1.11 #14
@@ -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,
|
||||||
|
}))
|
||||||
|
))
|
||||||
|
|||||||
@@ -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(
|
||||||
|
|||||||
Reference in New Issue
Block a user