From 57230116421abc57d8c44da8f4bf55ae2fa5e4dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Wed, 1 Oct 2025 00:49:24 +0200 Subject: [PATCH] Memo -> Memoized --- packages/effect-fc/src/Component.ts | 4 +-- packages/effect-fc/src/Memo.ts | 50 ----------------------------- packages/effect-fc/src/Memoized.ts | 50 +++++++++++++++++++++++++++++ packages/effect-fc/src/index.ts | 2 +- 4 files changed, 53 insertions(+), 53 deletions(-) delete mode 100644 packages/effect-fc/src/Memo.ts create mode 100644 packages/effect-fc/src/Memoized.ts diff --git a/packages/effect-fc/src/Component.ts b/packages/effect-fc/src/Component.ts index c0ddc6f..7383963 100644 --- a/packages/effect-fc/src/Component.ts +++ b/packages/effect-fc/src/Component.ts @@ -1,7 +1,7 @@ import { Context, Effect, Effectable, ExecutionStrategy, Function, Predicate, Runtime, Scope, Tracer, type Types, type Utils } from "effect" import * as React from "react" import { Hooks } from "./hooks/index.js" -import * as Memo from "./Memo.js" +import { Memoized } from "./index.js" export const TypeId: unique symbol = Symbol.for("effect-fc/Component") @@ -67,7 +67,7 @@ const ComponentProto = Object.freeze({ const FC = React.useMemo(() => { const f: React.FC

= self.makeFunctionComponent(runtimeRef, scope) f.displayName = self.displayName ?? "Anonymous" - return Memo.isMemo(self) + return Memoized.isMemoized(self) ? React.memo(f, self.propsAreEqual) : f }, [scope]) diff --git a/packages/effect-fc/src/Memo.ts b/packages/effect-fc/src/Memo.ts deleted file mode 100644 index 7abf319..0000000 --- a/packages/effect-fc/src/Memo.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { type Equivalence, Function, Predicate } from "effect" -import type * as Component from "./Component.js" - - -export const TypeId: unique symbol = Symbol.for("effect-fc/Memo") -export type TypeId = typeof TypeId - -export interface Memo

extends Memo.Options

{ - readonly [TypeId]: TypeId -} - -export namespace Memo { - export interface Options

{ - readonly propsAreEqual?: Equivalence.Equivalence

- } -} - - -const MemoProto = Object.freeze({ - [TypeId]: TypeId -} as const) - - -export const isMemo = (u: unknown): u is Memo => Predicate.hasProperty(u, TypeId) - -export const memo = >( - self: T -): T & Memo> => Object.setPrototypeOf( - Object.assign(function() {}, self), - Object.freeze(Object.setPrototypeOf( - Object.assign({}, MemoProto), - Object.getPrototypeOf(self), - )), -) - -export const withOptions: { - & Memo>( - options: Partial>> - ): (self: T) => T - & Memo>( - self: T, - options: Partial>>, - ): T -} = Function.dual(2, & Memo>( - self: T, - options: Partial>>, -): T => Object.setPrototypeOf( - Object.assign(function() {}, self, options), - Object.getPrototypeOf(self), -)) diff --git a/packages/effect-fc/src/Memoized.ts b/packages/effect-fc/src/Memoized.ts new file mode 100644 index 0000000..c80a3e0 --- /dev/null +++ b/packages/effect-fc/src/Memoized.ts @@ -0,0 +1,50 @@ +import { type Equivalence, Function, Predicate } from "effect" +import type * as Component from "./Component.js" + + +export const TypeId: unique symbol = Symbol.for("effect-fc/Memoized") +export type TypeId = typeof TypeId + +export interface Memoized

extends Memoized.Options

{ + readonly [TypeId]: TypeId +} + +export namespace Memoized { + export interface Options

{ + readonly propsAreEqual?: Equivalence.Equivalence

+ } +} + + +const MemoizedProto = Object.freeze({ + [TypeId]: TypeId +} as const) + + +export const isMemoized = (u: unknown): u is Memoized => Predicate.hasProperty(u, TypeId) + +export const memoized = >( + self: T +): T & Memoized> => Object.setPrototypeOf( + Object.assign(function() {}, self), + Object.freeze(Object.setPrototypeOf( + Object.assign({}, MemoizedProto), + Object.getPrototypeOf(self), + )), +) + +export const withOptions: { + & Memoized>( + options: Partial>> + ): (self: T) => T + & Memoized>( + self: T, + options: Partial>>, + ): T +} = Function.dual(2, & Memoized>( + self: T, + options: Partial>>, +): T => Object.setPrototypeOf( + Object.assign(function() {}, self, options), + Object.getPrototypeOf(self), +)) diff --git a/packages/effect-fc/src/index.ts b/packages/effect-fc/src/index.ts index 1595021..513ddd1 100644 --- a/packages/effect-fc/src/index.ts +++ b/packages/effect-fc/src/index.ts @@ -1,5 +1,5 @@ export * as Component from "./Component.js" export * as Form from "./Form.js" -export * as Memo from "./Memo.js" +export * as Memoized from "./Memoized.js" export * as ReactRuntime from "./ReactRuntime.js" export * as Suspense from "./Suspense.js"