Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions example/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Data, Effect } from "effect"
import { reactCache } from "../src/ReactCache.js"

class CustomError extends Data.TaggedError("CustomError") {}

// Example of a function that yields an error string
function cachableFunctionWithError(id: string) {
return Effect.gen(function*() {
yield* Effect.log(`Attempting to fetch user ${id}`)
yield* Effect.sleep(1000)
// Simulate an error
return yield* new CustomError()
})
}

export const cachedFunctionWithError = reactCache(cachableFunctionWithError)

const result = await Effect.runPromise(
Effect.gen(function*() {
const result = yield* cachedFunctionWithError("x").pipe(
Effect.catchTag("CustomError", () => Effect.succeed("error"))
)
return result
})
)

console.log(result)
54 changes: 45 additions & 9 deletions src/ReactCache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { Effect } from "effect"
import { Cause, Effect, Exit } from "effect"
import type * as Context from "effect/Context"
import type * as Scope from "effect/Scope"
import { cache } from "react"

type CauseResult<E> = {
error: E | undefined
defect: unknown
}

type PromiseResult<A, E> = {
success: A | undefined
error: E | undefined
defect: unknown
}

/**
* @since 1.0.0
* @category type ids
Expand All @@ -23,19 +34,41 @@ const runEffectFn = <A, E, R, Args extends Array<unknown>>(
effect: (...args: Args) => Effect.Effect<A, E, NoScope<R>>,
context: Context.Context<NoScope<R>>,
...args: Args
) => {
): Promise<PromiseResult<A, E>> => {
const effectResult = effect(...args)
const effectWithContext = Effect.provide(effectResult, context)

return Effect.runPromise(effectWithContext)
return Effect.runPromiseExit(effectWithContext).then((exit) => {
if (Exit.isSuccess(exit)) {
return { success: exit.value, error: undefined, defect: undefined }
}
if (Exit.isFailure(exit)) {
const cause = Cause.match(exit.cause, {
onEmpty: { error: undefined, defect: undefined } as CauseResult<E>,
onFail: (error) => ({ error, defect: undefined }),
onDie: (defect) => ({ error: undefined, defect }),
onInterrupt: () => {
throw new Error("Interrupt cause not supported")
},
onSequential: () => {
throw new Error("Sequential cause not supported")
},
onParallel: () => {
throw new Error("Parallel cause not supported")
}
})
return { success: undefined, error: cause.error, defect: cause.defect }
}
return { success: undefined, error: undefined, defect: undefined }
})
}

const runEffectCachedFn = cache(
<A, E, R, Args extends Array<unknown>>(
effect: (...args: Args) => Effect.Effect<A, E, NoScope<R>>,
...args: Args
) => {
let promise: Promise<A> | undefined
let promise: Promise<PromiseResult<A, E>>
return (context: Context.Context<NoScope<R>>) => {
if (!promise) {
promise = runEffectFn<A, E, R, Args>(effect, context, ...args)
Expand Down Expand Up @@ -64,10 +97,13 @@ export const reactCache = <A, E, R, Args extends Array<unknown>>(
): Effect.Effect<A, E, NoScope<R>> =>
Effect.gen(function*() {
const context = yield* Effect.context<NoScope<R>>()
const value: A = yield* Effect.tryPromise({
try: () => runEffectCachedFn(effect, ...args)(context),
catch: (e) => e as E
})
return value
const result = yield* Effect.promise(() => runEffectCachedFn(effect, ...args)(context))
if (result.success) {
return yield* Effect.succeed(result.success)
}
if (result.error) {
return yield* Effect.fail(result.error)
}
return yield* Effect.die(result.defect)
})
}