useMemoLayer
All checks were successful
Lint / lint (push) Successful in 15s

This commit is contained in:
Julien Valverdé
2025-07-01 00:23:45 +02:00
parent 8be1295e2f
commit 7cc0a68170
2 changed files with 15 additions and 8 deletions

View File

@@ -1,4 +1,4 @@
import { Effect, ExecutionStrategy, Exit, pipe, Runtime, Scope, Stream, SubscriptionRef } from "effect"
import { type Context, Effect, ExecutionStrategy, Exit, type Layer, pipe, Runtime, Scope, Stream, SubscriptionRef } from "effect"
import * as React from "react"
@@ -29,6 +29,19 @@ export const useOnce: {
return yield* useMemo(factory, [])
})
export const useMemoLayer: {
<ROut, E, RIn>(
layer: Layer.Layer<ROut, E, RIn>
): Effect.Effect<Context.Context<ROut>, never, RIn>
} = Effect.fnUntraced(function* <ROut, E, RIn>(
layer: Layer.Layer<ROut, E, RIn>
) {
const runtime = yield* Effect.runtime<RIn>()
return React.useMemo(() => Runtime.runSync(runtime)(
Effect.provide(Effect.context<ROut>(), layer)
), [layer])
})
export const useEffect: {
<E, R>(
effect: () => Effect.Effect<void, E, R>,