0.1.4 #5
@@ -1,7 +1,7 @@
|
|||||||
import { Context, Effect, Effectable, ExecutionStrategy, Function, Predicate, Runtime, Scope, Tracer, type Types, type Utils } from "effect"
|
import { Context, Effect, Effectable, ExecutionStrategy, Function, Predicate, Runtime, Scope, Tracer, type Types, type Utils } from "effect"
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import { Hooks } from "./hooks/index.js"
|
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")
|
export const TypeId: unique symbol = Symbol.for("effect-fc/Component")
|
||||||
@@ -67,7 +67,7 @@ const ComponentProto = Object.freeze({
|
|||||||
const FC = React.useMemo(() => {
|
const FC = React.useMemo(() => {
|
||||||
const f: React.FC<P> = self.makeFunctionComponent(runtimeRef, scope)
|
const f: React.FC<P> = self.makeFunctionComponent(runtimeRef, scope)
|
||||||
f.displayName = self.displayName ?? "Anonymous"
|
f.displayName = self.displayName ?? "Anonymous"
|
||||||
return Memo.isMemo(self)
|
return Memoized.isMemoized(self)
|
||||||
? React.memo(f, self.propsAreEqual)
|
? React.memo(f, self.propsAreEqual)
|
||||||
: f
|
: f
|
||||||
}, [scope])
|
}, [scope])
|
||||||
|
|||||||
@@ -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<P> extends Memo.Options<P> {
|
|
||||||
readonly [TypeId]: TypeId
|
|
||||||
}
|
|
||||||
|
|
||||||
export namespace Memo {
|
|
||||||
export interface Options<P> {
|
|
||||||
readonly propsAreEqual?: Equivalence.Equivalence<P>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
const MemoProto = Object.freeze({
|
|
||||||
[TypeId]: TypeId
|
|
||||||
} as const)
|
|
||||||
|
|
||||||
|
|
||||||
export const isMemo = (u: unknown): u is Memo<unknown> => Predicate.hasProperty(u, TypeId)
|
|
||||||
|
|
||||||
export const memo = <T extends Component.Component<any, any, any, any>>(
|
|
||||||
self: T
|
|
||||||
): T & Memo<Component.Component.Props<T>> => Object.setPrototypeOf(
|
|
||||||
Object.assign(function() {}, self),
|
|
||||||
Object.freeze(Object.setPrototypeOf(
|
|
||||||
Object.assign({}, MemoProto),
|
|
||||||
Object.getPrototypeOf(self),
|
|
||||||
)),
|
|
||||||
)
|
|
||||||
|
|
||||||
export const withOptions: {
|
|
||||||
<T extends Component.Component<any, any, any, any> & Memo<any>>(
|
|
||||||
options: Partial<Memo.Options<Component.Component.Props<T>>>
|
|
||||||
): (self: T) => T
|
|
||||||
<T extends Component.Component<any, any, any, any> & Memo<any>>(
|
|
||||||
self: T,
|
|
||||||
options: Partial<Memo.Options<Component.Component.Props<T>>>,
|
|
||||||
): T
|
|
||||||
} = Function.dual(2, <T extends Component.Component<any, any, any, any> & Memo<any>>(
|
|
||||||
self: T,
|
|
||||||
options: Partial<Memo.Options<Component.Component.Props<T>>>,
|
|
||||||
): T => Object.setPrototypeOf(
|
|
||||||
Object.assign(function() {}, self, options),
|
|
||||||
Object.getPrototypeOf(self),
|
|
||||||
))
|
|
||||||
50
packages/effect-fc/src/Memoized.ts
Normal file
50
packages/effect-fc/src/Memoized.ts
Normal file
@@ -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<P> extends Memoized.Options<P> {
|
||||||
|
readonly [TypeId]: TypeId
|
||||||
|
}
|
||||||
|
|
||||||
|
export namespace Memoized {
|
||||||
|
export interface Options<P> {
|
||||||
|
readonly propsAreEqual?: Equivalence.Equivalence<P>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const MemoizedProto = Object.freeze({
|
||||||
|
[TypeId]: TypeId
|
||||||
|
} as const)
|
||||||
|
|
||||||
|
|
||||||
|
export const isMemoized = (u: unknown): u is Memoized<unknown> => Predicate.hasProperty(u, TypeId)
|
||||||
|
|
||||||
|
export const memoized = <T extends Component.Component<any, any, any, any>>(
|
||||||
|
self: T
|
||||||
|
): T & Memoized<Component.Component.Props<T>> => Object.setPrototypeOf(
|
||||||
|
Object.assign(function() {}, self),
|
||||||
|
Object.freeze(Object.setPrototypeOf(
|
||||||
|
Object.assign({}, MemoizedProto),
|
||||||
|
Object.getPrototypeOf(self),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
|
||||||
|
export const withOptions: {
|
||||||
|
<T extends Component.Component<any, any, any, any> & Memoized<any>>(
|
||||||
|
options: Partial<Memoized.Options<Component.Component.Props<T>>>
|
||||||
|
): (self: T) => T
|
||||||
|
<T extends Component.Component<any, any, any, any> & Memoized<any>>(
|
||||||
|
self: T,
|
||||||
|
options: Partial<Memoized.Options<Component.Component.Props<T>>>,
|
||||||
|
): T
|
||||||
|
} = Function.dual(2, <T extends Component.Component<any, any, any, any> & Memoized<any>>(
|
||||||
|
self: T,
|
||||||
|
options: Partial<Memoized.Options<Component.Component.Props<T>>>,
|
||||||
|
): T => Object.setPrototypeOf(
|
||||||
|
Object.assign(function() {}, self, options),
|
||||||
|
Object.getPrototypeOf(self),
|
||||||
|
))
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
export * as Component from "./Component.js"
|
export * as Component from "./Component.js"
|
||||||
export * as Form from "./Form.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 ReactRuntime from "./ReactRuntime.js"
|
||||||
export * as Suspense from "./Suspense.js"
|
export * as Suspense from "./Suspense.js"
|
||||||
|
|||||||
Reference in New Issue
Block a user