From 567140ac1d6c3baf2e78c6ab5a6898830a3daf68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sun, 19 Jan 2025 03:09:27 +0100 Subject: [PATCH] useMemo --- packages/reffuse/src/Reffuse.ts | 74 ++++++++++++++++----------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/packages/reffuse/src/Reffuse.ts b/packages/reffuse/src/Reffuse.ts index 0f9acdc..5fe882d 100644 --- a/packages/reffuse/src/Reffuse.ts +++ b/packages/reffuse/src/Reffuse.ts @@ -80,36 +80,36 @@ export class Reffuse { * Changes to the Reffuse runtime or context will recompute the value in addition to the deps. * You can disable this behavior by setting `doNotReExecuteOnRuntimeOrContextChange` to `true` in `options`. */ - // useMemo( - // effect: Effect.Effect, - // deps?: React.DependencyList, - // options?: RenderOptions & ScopeOptions, - // ): A { - // const runSync = this.useRunSync() + useMemo( + effect: Effect.Effect, + deps?: React.DependencyList, + options?: RenderOptions & ScopeOptions, + ): A { + const runSync = this.useRunSync() - // const [value, scope] = React.useMemo(() => Scope.make(options?.finalizerExecutionStrategy).pipe( - // Effect.flatMap(scope => - // Effect.provideService(effect, Scope.Scope, scope).pipe( - // Effect.map(value => [value, scope] as const) - // ) - // ), + const [value, scope] = React.useMemo(() => Scope.make(options?.finalizerExecutionStrategy).pipe( + Effect.flatMap(scope => + Effect.provideService(effect, Scope.Scope, scope).pipe( + Effect.map(value => [value, scope] as const) + ) + ), - // runSync, - // ), [ - // ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync], - // ...(deps ?? []), - // ]) + runSync, + ), [ + ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync], + ...(deps ?? []), + ]) - // React.useEffect(() => { - // // console.log("effect", value, scope) - // return () => { - // // console.log("cleanup", value, scope) - // runSync(Scope.close(scope, Exit.void)) - // } - // }, [scope]) + React.useEffect(() => { + // console.log("effect", value, scope) + return () => { + // console.log("cleanup", value, scope) + runSync(Scope.close(scope, Exit.void)) + } + }, [scope]) - // return value - // } + return value + } /** * Reffuse equivalent to `React.useMemo`. @@ -120,18 +120,18 @@ export class Reffuse { * Changes to the Reffuse runtime or context will recompute the value in addition to the deps. * You can disable this behavior by setting `doNotReExecuteOnRuntimeOrContextChange` to `true` in `options`. */ - useMemo( - effect: Effect.Effect, - deps?: React.DependencyList, - options?: RenderOptions, - ): A { - const runSync = this.useRunSync() + // useMemo( + // effect: Effect.Effect, + // deps?: React.DependencyList, + // options?: RenderOptions, + // ): A { + // const runSync = this.useRunSync() - return React.useMemo(() => runSync(effect), [ - ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync], - ...(deps ?? []), - ]) - } + // return React.useMemo(() => runSync(effect), [ + // ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync], + // ...(deps ?? []), + // ]) + // } /** * Reffuse equivalent to `React.useEffect`.