From 8c5613aa62ff8bb5c0b03d9ae756864574dc39ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Wed, 25 Jun 2025 04:07:22 +0200 Subject: [PATCH] Tests --- .../src/routes/effect-component-tests.tsx | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/example/src/routes/effect-component-tests.tsx b/packages/example/src/routes/effect-component-tests.tsx index c564f03..7cfb033 100644 --- a/packages/example/src/routes/effect-component-tests.tsx +++ b/packages/example/src/routes/effect-component-tests.tsx @@ -1,6 +1,6 @@ import { Box, Text, TextField } from "@radix-ui/themes" import { createFileRoute } from "@tanstack/react-router" -import { Console, Effect, Runtime } from "effect" +import { Console, Effect, Layer, ManagedRuntime, Runtime } from "effect" import * as React from "react" @@ -9,15 +9,31 @@ export const Route = createFileRoute("/effect-component-tests")({ }) function RouteComponent() { - return Effect.runSync(MyTestComponent) + const runtime = React.useMemo(() => ManagedRuntime.make(Layer.empty), []) + + return runtime.runSync(Effect.gen(function* RouteComponent() { + const MyTest = yield* useFunctionComponent(MyTestComponent) + + return <> + + + })) } -const MyTestComponent = Effect.gen(function*() { +const useFunctionComponent = ( + self: (props: P) => Effect.Effect +): Effect.Effect, never, R> => Effect.gen(function* useFunctionComponent() { + const runtime = yield* Effect.runtime() + return React.useCallback((props: P) => Runtime.runSync(runtime)(self(props)), [runtime]) +}) + + +const MyTestComponent = Effect.fn(function*(props?: { readonly value?: string }) { const [state, setState] = React.useState("value") const effectValue = yield* Effect.succeed(`state: ${ state }`) - yield* useEffect(() => Console.log("ouient"), [state]) + yield* useEffect(() => Console.log("ouient"), []) return <> {effectValue}