Files
effect-fc/packages/effect-fc/src/Component.ts
Julien Valverdé 591ec17e9f
All checks were successful
Lint / lint (push) Successful in 12s
Make refactoring
2025-07-16 12:45:57 +02:00

344 lines
15 KiB
TypeScript

import { Context, Effect, type Equivalence, ExecutionStrategy, Function, pipe, Pipeable, Predicate, Runtime, Scope, String, Tracer, type Utils } from "effect"
import * as React from "react"
import * as Hook from "./Hook.js"
import type { ExcludeKeys } from "./utils.js"
export interface Component<E, R, P extends {}> extends Pipeable.Pipeable {
readonly body: (props: P) => Effect.Effect<React.ReactNode, E, R>
readonly displayName?: string
readonly finalizerExecutionMode: "sync" | "fork"
readonly finalizerExecutionStrategy: ExecutionStrategy.ExecutionStrategy
}
export namespace Component {
export type Error<T> = T extends Component<infer E, infer _R, infer _P> ? E : never
export type Context<T> = T extends Component<infer _E, infer R, infer _P> ? R : never
export type Props<T> = T extends Component<infer _E, infer _R, infer P> ? P : never
}
const ComponentProto = Object.seal({
pipe() { return Pipeable.pipeArguments(this, arguments) }
} as const)
const nonReactiveTags = [Tracer.ParentSpan] as const
export namespace make {
export interface Options {
readonly untraced?: boolean
readonly finalizerExecutionMode?: "sync" | "fork"
readonly finalizerExecutionStrategy?: ExecutionStrategy.ExecutionStrategy
}
}
export const make: {
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, P extends {} = {}>(
body: (props: P) => Generator<Eff, React.ReactNode, never>,
): Component<
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never,
P
>
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, AEff, EOut, ROut, P extends {} = {}>(
body: (props: P) => Generator<Eff, AEff, never>,
a: (
_: Effect.Effect<
AEff,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never
>,
props: NoInfer<P>,
) => Effect.Effect<React.ReactNode, EOut, ROut>
): Component<EOut, ROut, P>
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, AEff, A, EOut, ROut, P extends {} = {}>(
body: (props: P) => Generator<Eff, AEff, never>,
a: (
_: Effect.Effect<
AEff,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never
>,
props: NoInfer<P>,
) => A,
b: (_: A, props: NoInfer<P>) => Effect.Effect<React.ReactNode, EOut, ROut>,
): Component<EOut, ROut, P>
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, AEff, A, B, EOut, ROut, P extends {} = {}>(
body: (props: P) => Generator<Eff, AEff, never>,
a: (
_: Effect.Effect<
AEff,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never
>,
props: NoInfer<P>,
) => A,
b: (_: A, props: NoInfer<P>) => B,
c: (_: B, props: NoInfer<P>) => Effect.Effect<React.ReactNode, EOut, ROut>,
): Component<EOut, ROut, P>
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, AEff, A, B, C, EOut, ROut, P extends {} = {}>(
body: (props: P) => Generator<Eff, AEff, never>,
a: (
_: Effect.Effect<
AEff,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never
>,
props: NoInfer<P>,
) => A,
b: (_: A, props: NoInfer<P>) => B,
c: (_: B, props: NoInfer<P>) => C,
d: (_: C, props: NoInfer<P>) => Effect.Effect<React.ReactNode, EOut, ROut>,
): Component<EOut, ROut, P>
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, AEff, A, B, C, D, EOut, ROut, P extends {} = {}>(
body: (props: P) => Generator<Eff, AEff, never>,
a: (
_: Effect.Effect<
AEff,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never
>,
props: NoInfer<P>,
) => A,
b: (_: A, props: NoInfer<P>) => B,
c: (_: B, props: NoInfer<P>) => C,
d: (_: C, props: NoInfer<P>) => D,
e: (_: D, props: NoInfer<P>) => Effect.Effect<React.ReactNode, EOut, ROut>,
): Component<EOut, ROut, P>
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, AEff, A, B, C, D, E, EOut, ROut, P extends {} = {}>(
body: (props: P) => Generator<Eff, AEff, never>,
a: (
_: Effect.Effect<
AEff,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never
>,
props: NoInfer<P>,
) => A,
b: (_: A, props: NoInfer<P>) => B,
c: (_: B, props: NoInfer<P>) => C,
d: (_: C, props: NoInfer<P>) => D,
e: (_: D, props: NoInfer<P>) => E,
f: (_: E, props: NoInfer<P>) => Effect.Effect<React.ReactNode, EOut, ROut>,
): Component<EOut, ROut, P>
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, AEff, A, B, C, D, E, F, EOut, ROut, P extends {} = {}>(
body: (props: P) => Generator<Eff, AEff, never>,
a: (
_: Effect.Effect<
AEff,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never
>,
props: NoInfer<P>,
) => A,
b: (_: A, props: NoInfer<P>) => B,
c: (_: B, props: NoInfer<P>) => C,
d: (_: C, props: NoInfer<P>) => D,
e: (_: D, props: NoInfer<P>) => E,
f: (_: E, props: NoInfer<P>) => F,
g: (_: F, props: NoInfer<P>) => Effect.Effect<React.ReactNode, EOut, ROut>,
): Component<EOut, ROut, P>
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, AEff, A, B, C, D, E, F, G, EOut, ROut, P extends {} = {}>(
body: (props: P) => Generator<Eff, AEff, never>,
a: (
_: Effect.Effect<
AEff,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never
>,
props: NoInfer<P>,
) => A,
b: (_: A, props: NoInfer<P>) => B,
c: (_: B, props: NoInfer<P>) => C,
d: (_: C, props: NoInfer<P>) => D,
e: (_: D, props: NoInfer<P>) => E,
f: (_: E, props: NoInfer<P>) => F,
g: (_: F, props: NoInfer<P>) => G,
h: (_: G, props: NoInfer<P>) => Effect.Effect<React.ReactNode, EOut, ROut>,
): Component<EOut, ROut, P>
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, AEff, A, B, C, D, E, F, G, H, EOut, ROut, P extends {} = {}>(
body: (props: P) => Generator<Eff, AEff, never>,
a: (
_: Effect.Effect<
AEff,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never
>,
props: NoInfer<P>,
) => A,
b: (_: A, props: NoInfer<P>) => B,
c: (_: B, props: NoInfer<P>) => C,
d: (_: C, props: NoInfer<P>) => D,
e: (_: D, props: NoInfer<P>) => E,
f: (_: E, props: NoInfer<P>) => F,
g: (_: F, props: NoInfer<P>) => G,
h: (_: G, props: NoInfer<P>) => H,
i: (_: H, props: NoInfer<P>) => Effect.Effect<React.ReactNode, EOut, ROut>,
): Component<EOut, ROut, P>
} = (...pipeables: readonly [any]) => {
const displayName: string = !String.isEmpty(pipeables[0].name) ? pipeables[0].name : undefined
return Object.setPrototypeOf({
body: displayName
? Effect.fn(displayName)(...pipeables)
: Effect.fn(...pipeables),
displayName,
}, ComponentProto)
}
export const make_old = <
Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>,
P extends {} = {},
>(
body: (props: P) => Generator<Eff, React.ReactNode, never>,
options?: make.Options,
): Component<
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer E, infer _R>>] ? E : never,
[Eff] extends [never] ? never : [Eff] extends [Utils.YieldWrap<Effect.Effect<infer _A, infer _E, infer R>>] ? R : never,
P
> => {
const displayName = !String.isEmpty(body.name) ? body.name : undefined
return Object.setPrototypeOf({
body: options?.untraced
? Effect.fnUntraced(body)
: displayName
? Effect.fn(displayName)(body)
: Effect.fn(body),
displayName,
finalizerExecutionMode: options?.finalizerExecutionMode ?? "sync",
finalizerExecutionStrategy: options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential,
}, ComponentProto)
}
export const withRuntime: {
<E, R, P extends {}>(
context: React.Context<Runtime.Runtime<R>>,
): (self: Component<E, R | Scope.Scope, P>) => React.FC<P>
<E, R, P extends {}>(
self: Component<E, R | Scope.Scope, P>,
context: React.Context<Runtime.Runtime<R>>,
): React.FC<P>
} = Function.dual(2, <E, R, P extends {}>(
self: Component<E, R | Scope.Scope, P>,
context: React.Context<Runtime.Runtime<R>>,
): React.FC<P> => function WithRuntime(props) {
const runtime = React.useContext(context)
return React.createElement(Runtime.runSync(runtime)(useFC(self)), props)
})
export interface Memoized<P> {
readonly memo: true
readonly propsAreEqual?: Equivalence.Equivalence<P>
}
export const memo = <T extends Component<any, any, any>>(
self: ExcludeKeys<T, keyof Memoized<Component.Props<T>>>
): T & Memoized<Component.Props<T>> => Object.setPrototypeOf(
{ ...self, memo: true },
Object.getPrototypeOf(self),
)
export const memoWithEquivalence: {
<T extends Component<any, any, any>>(
propsAreEqual: Equivalence.Equivalence<Component.Props<T>>
): (
self: ExcludeKeys<T, keyof Memoized<Component.Props<T>>>
) => T & Memoized<Component.Props<T>>
<T extends Component<any, any, any>>(
self: ExcludeKeys<T, keyof Memoized<Component.Props<T>>>,
propsAreEqual: Equivalence.Equivalence<Component.Props<T>>,
): T & Memoized<Component.Props<T>>
} = Function.dual(2, <T extends Component<any, any, any>>(
self: ExcludeKeys<T, keyof Memoized<Component.Props<T>>>,
propsAreEqual: Equivalence.Equivalence<Component.Props<T>>,
): T & Memoized<Component.Props<T>> => Object.setPrototypeOf(
{ ...self, memo: true, propsAreEqual },
Object.getPrototypeOf(self),
))
export interface Suspense {
readonly suspense: true
}
export type SuspenseProps = Omit<React.SuspenseProps, "children">
export const suspense = <T extends Component<any, any, P>, P extends {}>(
self: ExcludeKeys<T, keyof Suspense> & Component<any, any, ExcludeKeys<P, keyof SuspenseProps>>
): T & Suspense => Object.setPrototypeOf(
{ ...self, suspense: true },
Object.getPrototypeOf(self),
)
export const useFC: {
<E, R, P extends {}>(
self: Component<E, R, P> & Suspense
): Effect.Effect<React.FC<P & SuspenseProps>, never, Exclude<R, Scope.Scope>>
<E, R, P extends {}>(
self: Component<E, R, P>
): Effect.Effect<React.FC<P>, never, Exclude<R, Scope.Scope>>
} = Effect.fn("useFC")(function* <E, R, P extends {}>(
self: Component<E, R, P> & (Memoized<P> | Suspense | {})
) {
const runtimeRef = React.useRef<Runtime.Runtime<Exclude<R, Scope.Scope>>>(null!)
runtimeRef.current = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
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: React.FC<P> = Predicate.hasProperty(self, "suspense")
? pipe(
function SuspenseInner(props: { readonly promise: Promise<React.ReactNode> }) {
return React.use(props.promise)
},
SuspenseInner => ({ fallback, name, ...props }: P & SuspenseProps) => {
const promise = Runtime.runPromise(runtimeRef.current)(
Effect.provideService(self.body(props as P), Scope.Scope, scope)
)
return React.createElement(
React.Suspense,
{ fallback, name },
React.createElement(SuspenseInner, { promise }),
)
},
)
: (props: P) => Runtime.runSync(runtimeRef.current)(
Effect.provideService(self.body(props), Scope.Scope, scope)
)
f.displayName = self.displayName ?? "Anonymous"
return Predicate.hasProperty(self, "memo")
? React.memo(f, self.propsAreEqual)
: f
}, [scope])
return React.createElement(FC, props)
}, [])
})
export const use: {
<E, R, P extends {}>(
self: Component<E, R, P> & Suspense,
fn: (Component: React.FC<P & SuspenseProps>) => React.ReactNode,
): Effect.Effect<React.ReactNode, never, Exclude<R, Scope.Scope>>
<E, R, P extends {}>(
self: Component<E, R, P>,
fn: (Component: React.FC<P>) => React.ReactNode,
): Effect.Effect<React.ReactNode, never, Exclude<R, Scope.Scope>>
} = Effect.fn("use")(function*(self, fn) {
return fn(yield* useFC(self))
})