diff --git a/packages/effect-fc/src/Component.ts b/packages/effect-fc/src/Component.ts index 2bd8c6c..236e895 100644 --- a/packages/effect-fc/src/Component.ts +++ b/packages/effect-fc/src/Component.ts @@ -7,7 +7,8 @@ import * as Memoized from "./Memoized.js" export const TypeId: unique symbol = Symbol.for("effect-fc/Component") export type TypeId = typeof TypeId -export interface Component extends Effect.Effect, never, R>, Component.Options { +export interface Component +extends Effect.Effect, never, Exclude>, Component.Options { new(_: never): {} readonly [TypeId]: TypeId /** @internal */ @@ -365,46 +366,6 @@ export const withOptions: { Object.getPrototypeOf(self), )) - -export const useFC: { - ( - self: Component - ): Effect.Effect, never, Exclude> -} = Effect.fn("useFC")(function* ( - self: Component -) { - const runtimeRef = React.useRef>>(null!) - runtimeRef.current = yield* Effect.runtime>() - - return React.useCallback(function ScopeProvider(props: P) { - const scope = Runtime.runSync(runtimeRef.current)(Hook.useScope( - Array.from( - Context.omit(...nonReactiveTags)(runtimeRef.current.context).unsafeMap.values() - ), - self, - )) - - const FC = React.useMemo(() => { - const f = self.makeFunctionComponent(runtimeRef, scope) - f.displayName = self.displayName ?? "Anonymous" - return Memoized.isMemoized(self) - ? React.memo(f, self.propsAreEqual) - : f - }, [scope]) - - return React.createElement(FC, props) - }, []) -}) - -export const use: { - ( - self: Component, - fn: (Component: React.FC

) => React.ReactNode, - ): Effect.Effect> -} = Effect.fn("use")(function*(self, fn) { - return fn(yield* useFC(self)) -}) - export const withRuntime: { ( context: React.Context>, @@ -418,5 +379,5 @@ export const withRuntime: { context: React.Context>, ): React.FC

=> function WithRuntime(props) { const runtime = React.useContext(context) - return React.createElement(Runtime.runSync(runtime)(useFC(self)), props) + return React.createElement(Runtime.runSync(runtime)(self), props) }) diff --git a/packages/example/src/routes/dev/async-rendering.tsx b/packages/example/src/routes/dev/async-rendering.tsx index aff109e..52b9fb1 100644 --- a/packages/example/src/routes/dev/async-rendering.tsx +++ b/packages/example/src/routes/dev/async-rendering.tsx @@ -9,8 +9,8 @@ import * as React from "react" // Generator version const RouteComponent = Component.make(function* AsyncRendering() { - const VMemoizedAsyncComponent = yield* MemoizedAsyncComponent - const VAsyncComponent = yield* AsyncComponent + const MemoizedAsyncComponentFC = yield* MemoizedAsyncComponent + const AsyncComponentFC = yield* AsyncComponent const [input, setInput] = React.useState("") return ( @@ -20,8 +20,8 @@ const RouteComponent = Component.make(function* AsyncRendering() { onChange={e => setInput(e.target.value)} /> -

Loading memoized...

, [])} /> - +

Loading memoized...

, [])} /> + ) }).pipe( @@ -51,13 +51,15 @@ const RouteComponent = Component.make(function* AsyncRendering() { class AsyncComponent extends Component.make(function* AsyncComponent() { - const VSubComponent = yield* Component.useFC(SubComponent) - yield* Effect.sleep("500 millis") + const SubComponentFC = yield* SubComponent + + yield* Effect.sleep("500 millis") // Async operation + // Cannot use React hooks after the async operation return ( Rendered! - + ) }).pipe( diff --git a/packages/example/src/routes/dev/memo.tsx b/packages/example/src/routes/dev/memo.tsx index c325bc8..42f2f41 100644 --- a/packages/example/src/routes/dev/memo.tsx +++ b/packages/example/src/routes/dev/memo.tsx @@ -8,9 +8,6 @@ import * as React from "react" const RouteComponent = Component.make(function* RouteComponent() { - const VSubComponent = yield* Component.useFC(SubComponent) - const VMemoizedSubComponent = yield* Component.useFC(MemoizedSubComponent) - const [value, setValue] = React.useState("") return ( @@ -20,8 +17,8 @@ const RouteComponent = Component.make(function* RouteComponent() { onChange={e => setValue(e.target.value)} /> - - + {yield* Effect.map(SubComponent, FC => )} + {yield* Effect.map(MemoizedSubComponent, FC => )} ) }).pipe( diff --git a/packages/example/src/routes/index.tsx b/packages/example/src/routes/index.tsx index 93e02d1..8c82d1b 100644 --- a/packages/example/src/routes/index.tsx +++ b/packages/example/src/routes/index.tsx @@ -10,16 +10,11 @@ const TodosStateLive = TodosState.Default("todos") export const Route = createFileRoute("/")({ component: Component.make(function* Index() { - const context = yield* Hook.useContext(TodosStateLive, { finalizerExecutionMode: "fork" }) - return yield* Effect.provide(Component.use(Todos, Todos => ), context) + return yield* Todos.pipe( + Effect.map(FC => ), + Effect.provide(yield* Hook.useContext(TodosStateLive, { finalizerExecutionMode: "fork" })), + ) }).pipe( Component.withRuntime(runtime.context) ) - - // component: Component.make("Index")( - // () => Hook.useContext(TodosStateLive, { finalizerExecutionMode: "fork" }), - // Effect.andThen(context => Effect.provide(Component.use(Todos, Todos => ), context)), - // ).pipe( - // Component.withRuntime(runtime.context) - // ) }) diff --git a/packages/example/src/todo/Todos.tsx b/packages/example/src/todo/Todos.tsx index 7529127..857f303 100644 --- a/packages/example/src/todo/Todos.tsx +++ b/packages/example/src/todo/Todos.tsx @@ -14,17 +14,17 @@ export class Todos extends Component.make(function* Todos() { Effect.addFinalizer(() => Console.log("Todos unmounted")), )) - const VTodo = yield* Component.useFC(Todo) + const TodoFC = yield* Todo return ( Todos - + {Chunk.map(todos, (v, k) => - + )}