This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { type Context, Effect, type Layer, Scope } from "effect"
|
import { type Context, Effect, Layer, ManagedRuntime, Scope } from "effect"
|
||||||
import type { ScopeOptions } from "./ScopeOptions.js"
|
import type { ScopeOptions } from "./ScopeOptions.js"
|
||||||
import { useMemo } from "./useMemo.js"
|
import { useMemo } from "./useMemo.js"
|
||||||
import { useScope } from "./useScope.js"
|
import { useScope } from "./useScope.js"
|
||||||
@@ -8,16 +8,18 @@ export const useContext: {
|
|||||||
<ROut, E, RIn>(
|
<ROut, E, RIn>(
|
||||||
layer: Layer.Layer<ROut, E, RIn>,
|
layer: Layer.Layer<ROut, E, RIn>,
|
||||||
options?: ScopeOptions,
|
options?: ScopeOptions,
|
||||||
): Effect.Effect<Context.Context<ROut>, E, Exclude<RIn, Scope.Scope>>
|
): Effect.Effect<Context.Context<ROut>, E, RIn>
|
||||||
} = Effect.fnUntraced(function* <ROut, E, RIn>(
|
} = Effect.fnUntraced(function* <ROut, E, RIn>(
|
||||||
layer: Layer.Layer<ROut, E, RIn>,
|
layer: Layer.Layer<ROut, E, RIn>,
|
||||||
options?: ScopeOptions,
|
options?: ScopeOptions,
|
||||||
) {
|
) {
|
||||||
const scope = yield* useScope([layer], options)
|
const scope = yield* useScope([layer], options)
|
||||||
|
|
||||||
return yield* useMemo(() => Effect.provideService(
|
return yield* useMemo(() => Effect.context<RIn>().pipe(
|
||||||
Effect.provide(Effect.context<ROut>(), layer),
|
Effect.map(context => ManagedRuntime.make(Layer.provide(layer, Layer.succeedContext(context)))),
|
||||||
Scope.Scope,
|
Effect.tap(runtime => Effect.addFinalizer(() => runtime.disposeEffect)),
|
||||||
scope,
|
Effect.andThen(runtime => runtime.runtimeEffect),
|
||||||
|
Effect.andThen(runtime => runtime.context),
|
||||||
|
Effect.provideService(Scope.Scope, scope),
|
||||||
), [scope])
|
), [scope])
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { SubscriptionSubRef } from "effect-fc/types"
|
|||||||
|
|
||||||
|
|
||||||
export class TodosState extends Effect.Service<TodosState>()("TodosState", {
|
export class TodosState extends Effect.Service<TodosState>()("TodosState", {
|
||||||
effect: Effect.fn("TodosState")(function*(key: string) {
|
scoped: Effect.fnUntraced(function*(key: string) {
|
||||||
const kv = yield* KeyValueStore.KeyValueStore
|
const kv = yield* KeyValueStore.KeyValueStore
|
||||||
|
|
||||||
const readFromLocalStorage = Console.log("Reading todos from local storage...").pipe(
|
const readFromLocalStorage = Console.log("Reading todos from local storage...").pipe(
|
||||||
|
|||||||
Reference in New Issue
Block a user