156 lines
5.7 KiB
TypeScript
156 lines
5.7 KiB
TypeScript
import { Context, Effect, Effectable, ExecutionStrategy, Function, identity, Predicate, Runtime, Scope, String, Tracer, type Types } from "effect"
|
|
import * as React from "react"
|
|
import { Hooks } from "./hooks/index.js"
|
|
import * as Memoized from "./Memoized.js"
|
|
|
|
|
|
export const TypeId: unique symbol = Symbol.for("effect-fc/Component")
|
|
export type TypeId = typeof TypeId
|
|
|
|
export interface Component<F extends FunctionComponent, E, R>
|
|
extends
|
|
Effect.Effect<F, never, Exclude<R, Scope.Scope>>,
|
|
Component.Options
|
|
{
|
|
new(_: never): {}
|
|
readonly [TypeId]: TypeId
|
|
readonly ["~FunctionComponent"]: F
|
|
readonly ["~Props"]: FunctionComponent.Props<F>
|
|
readonly ["~Success"]: FunctionComponent.Success<F>
|
|
readonly ["~Error"]: E
|
|
readonly ["~Context"]: R
|
|
|
|
/** @internal */
|
|
readonly body: (props: FunctionComponent.Props<F>) => Effect.Effect<FunctionComponent.Success<F>, E, R>
|
|
|
|
/** @internal */
|
|
makeFunctionComponent(
|
|
runtimeRef: React.Ref<Runtime.Runtime<Exclude<R, Scope.Scope>>>,
|
|
scope: Scope.Scope,
|
|
): F
|
|
}
|
|
|
|
export namespace Component {
|
|
export type FunctionComponent<T extends Component<any, any, any>> = T extends Component<infer F, infer _E, infer _R> ? F : never
|
|
export type Error<T extends Component<any, any, any>> = T extends Component<infer _F, infer E, infer _R> ? E : never
|
|
export type Context<T extends Component<any, any, any>> = T extends Component<infer _F, infer _E, infer R> ? R : never
|
|
|
|
export type AsComponent<T extends Component<any, any, any>> = Component<FunctionComponent<T>, Error<T>, Context<T>>
|
|
|
|
export interface Options {
|
|
readonly displayName?: string
|
|
readonly finalizerExecutionMode: "sync" | "fork"
|
|
readonly finalizerExecutionStrategy: ExecutionStrategy.ExecutionStrategy
|
|
}
|
|
}
|
|
|
|
export type FunctionComponent = (...args: readonly any[]) => React.ReactNode
|
|
|
|
export namespace FunctionComponent {
|
|
export type Props<T extends FunctionComponent> = T extends (...args: readonly [infer P, ...any[]]) => infer _A ? P : never
|
|
export type Success<T extends FunctionComponent> = T extends (...args: infer _Args) => infer A ? A : never
|
|
}
|
|
|
|
|
|
const ComponentProto = Object.freeze({
|
|
...Effectable.CommitPrototype,
|
|
[TypeId]: TypeId,
|
|
|
|
commit: Effect.fnUntraced(function* <F extends FunctionComponent, E, R>(
|
|
this: Component<F, E, R>
|
|
) {
|
|
const self = this
|
|
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: FunctionComponent.Props<F>) {
|
|
const scope = Runtime.runSync(runtimeRef.current)(Hooks.useScope(
|
|
Array.from(
|
|
Context.omit(...nonReactiveTags)(runtimeRef.current.context).unsafeMap.values()
|
|
),
|
|
self,
|
|
))
|
|
|
|
const FC = React.useMemo(() => {
|
|
const f: React.FC<FunctionComponent.Props<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)
|
|
}, [])
|
|
}),
|
|
|
|
makeFunctionComponent<F extends FunctionComponent, E, R>(
|
|
this: Component<F, E, R>,
|
|
runtimeRef: React.RefObject<Runtime.Runtime<Exclude<R, Scope.Scope>>>,
|
|
scope: Scope.Scope,
|
|
) {
|
|
return (props: FunctionComponent.Props<F>) => Runtime.runSync(runtimeRef.current)(
|
|
Effect.provideService(this.body(props), Scope.Scope, scope)
|
|
)
|
|
},
|
|
} as const)
|
|
|
|
const defaultOptions = {
|
|
finalizerExecutionMode: "sync",
|
|
finalizerExecutionStrategy: ExecutionStrategy.sequential,
|
|
} as const
|
|
|
|
const nonReactiveTags = [Tracer.ParentSpan] as const
|
|
|
|
|
|
export const isComponent = (u: unknown): u is Component<FunctionComponent, unknown, unknown> => Predicate.hasProperty(u, TypeId)
|
|
|
|
export const make = <Args extends readonly any[], A extends React.ReactNode, E, R>(
|
|
body: (...args: Args) => Effect.Effect<A, E, R>
|
|
): Component<(...args: Args) => A, E, R> => Object.setPrototypeOf(
|
|
Object.assign(function() {}, defaultOptions, {
|
|
body,
|
|
displayName: !String.isEmpty(body.name) ? body.name : undefined,
|
|
}),
|
|
ComponentProto,
|
|
)
|
|
|
|
export const withFunctionComponentSignature: {
|
|
<F extends FunctionComponent>(): <T extends Component<any, any, any>>(self: T) =>
|
|
& Omit<T, keyof Component.AsComponent<T>>
|
|
& Component<F, Component.Error<T>, Component.Context<T>>
|
|
} = () => identity
|
|
|
|
export const withOptions: {
|
|
<T extends Component<any, any, any>>(
|
|
options: Partial<Component.Options>
|
|
): (self: T) => T
|
|
<T extends Component<any, any, any>>(
|
|
self: T,
|
|
options: Partial<Component.Options>,
|
|
): T
|
|
} = Function.dual(2, <T extends Component<any, any, any>>(
|
|
self: T,
|
|
options: Partial<Component.Options>,
|
|
): T => Object.setPrototypeOf(
|
|
Object.assign(function() {}, self, options),
|
|
Object.getPrototypeOf(self),
|
|
))
|
|
|
|
export const withRuntime: {
|
|
<F extends FunctionComponent, E, R>(
|
|
context: React.Context<Runtime.Runtime<R>>,
|
|
): (self: Component<F, E, Types.NoInfer<R>>) => F
|
|
<F extends FunctionComponent, E, R>(
|
|
self: Component<F, E, Types.NoInfer<R>>,
|
|
context: React.Context<Runtime.Runtime<R>>,
|
|
): F
|
|
} = Function.dual(2, <F extends FunctionComponent, E, R>(
|
|
self: Component<F, E, R>,
|
|
context: React.Context<Runtime.Runtime<R>>,
|
|
) => function WithRuntime(props: FunctionComponent.Props<F>) {
|
|
return React.createElement(
|
|
Runtime.runSync(React.useContext(context))(self),
|
|
props,
|
|
)
|
|
})
|