0.1.3 #4
@@ -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 { useMemo } from "./useMemo.js"
|
||||
import { useScope } from "./useScope.js"
|
||||
@@ -8,16 +8,18 @@ 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.Effect<Context.Context<ROut>, E, RIn>
|
||||
} = Effect.fnUntraced(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,
|
||||
return yield* useMemo(() => Effect.context<RIn>().pipe(
|
||||
Effect.map(context => ManagedRuntime.make(Layer.provide(layer, Layer.succeedContext(context)))),
|
||||
Effect.tap(runtime => Effect.addFinalizer(() => runtime.disposeEffect)),
|
||||
Effect.andThen(runtime => runtime.runtimeEffect),
|
||||
Effect.andThen(runtime => runtime.context),
|
||||
Effect.provideService(Scope.Scope, scope),
|
||||
), [scope])
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ import { SubscriptionSubRef } from "effect-fc/types"
|
||||
|
||||
|
||||
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 readFromLocalStorage = Console.log("Reading todos from local storage...").pipe(
|
||||
|
||||
Reference in New Issue
Block a user