diff --git a/packages/effect-fc/src/Component.ts b/packages/effect-fc/src/Component.ts index e1c37e3..a407b54 100644 --- a/packages/effect-fc/src/Component.ts +++ b/packages/effect-fc/src/Component.ts @@ -85,6 +85,33 @@ export const useFC: { )) }) +export const useSuspenseFC: { + ( + self: Component + ): Effect.Effect, never, Exclude> +} = Effect.fn("useSuspenseFC")(function* ( + self: Component +) { + const runtimeRef = React.useRef>>(null!) + runtimeRef.current = yield* Effect.runtime>() + + return React.useMemo(() => function ScopeProvider(props: P) { + const scope = Runtime.runSync(runtimeRef.current)(Hook.useScope([], self.options)) + + const FC = React.useMemo(() => { + const f = (props: P) => Runtime.runSync(runtimeRef.current)( + Effect.provideService(self(props), Scope.Scope, scope) + ) + f.displayName = self.displayName ?? "Anonymous" + return f + }, [scope]) + + return React.createElement(FC, props) + }, Array.from( + Context.omit(...nonReactiveTags)(runtimeRef.current.context).unsafeMap.values() + )) +}) + export const use: { ( self: Component,