24 lines
738 B
TypeScript
24 lines
738 B
TypeScript
import { type Context, Effect, type Layer, Scope } from "effect"
|
|
import type { ScopeOptions } from "./ScopeOptions.js"
|
|
import { useMemo } from "./useMemo.js"
|
|
import { useScope } from "./useScope.js"
|
|
|
|
|
|
export const useContext: {
|
|
<ROut, E, RIn>(
|
|
layer: Layer.Layer<ROut, E, RIn>,
|
|
options?: ScopeOptions,
|
|
): Effect.Effect<Context.Context<ROut>, E, Exclude<RIn, Scope.Scope>>
|
|
} = Effect.fn("useContext")(function* <ROut, E, RIn>(
|
|
layer: Layer.Layer<ROut, E, RIn>,
|
|
options?: ScopeOptions,
|
|
) {
|
|
const scope = yield* useScope([layer], options)
|
|
|
|
return yield* useMemo(() => Effect.provideService(
|
|
Effect.provide(Effect.context<ROut>(), layer),
|
|
Scope.Scope,
|
|
scope,
|
|
), [scope])
|
|
})
|