From c80b25b0e0f95e5758f9d31cec8a742a90e90a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Thu, 6 Nov 2025 15:21:56 +0100 Subject: [PATCH] Fix ReactRuntime --- packages/effect-fc/src/ReactRuntime.ts | 30 +++++++++++++++----------- 1 file changed, 17 insertions(+), 13 deletions(-) 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) +}