diff --git a/packages/effect-fc/src/ReactRuntime.ts b/packages/effect-fc/src/ReactRuntime.ts index 9fe4552..c24cd9a 100644 --- a/packages/effect-fc/src/ReactRuntime.ts +++ b/packages/effect-fc/src/ReactRuntime.ts @@ -1,5 +1,5 @@ /** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */ -import { Effect, Layer, ManagedRuntime, Predicate, type Runtime } from "effect" +import { Effect, Layer, ManagedRuntime, Predicate, Runtime, Scope } from "effect" import * as React from "react" import * as Component from "./Component.js" import * as ErrorObserver from "./ErrorObserver.js" @@ -60,16 +60,20 @@ export const Provider = ( ) } -interface ProviderInnerProps { - readonly runtime: ReactRuntime - readonly promise: Promise> - readonly children?: React.ReactNode -} - const ProviderInner = ( - { runtime, promise, children }: ProviderInnerProps -): React.ReactNode => React.createElement( - runtime.context, - { value: React.use(promise) }, - children, -) + { runtime, promise, children }: { + readonly runtime: ReactRuntime + readonly promise: Promise> + readonly children?: React.ReactNode + } +): React.ReactNode => { + const effectRuntime = React.use(promise) + const scope = Runtime.runSync(effectRuntime)(Component.useScope([effectRuntime])) + Runtime.runSync(effectRuntime)(Effect.provideService( + Component.useOnChange(() => Effect.addFinalizer(() => runtime.runtime.disposeEffect), [scope]), + Scope.Scope, + scope, + )) + + return React.createElement(runtime.context, { value: effectRuntime }, children) +}