0.1.3 #4

Merged
Thilawyn merged 90 commits from next into master 2025-08-23 03:07:28 +02:00
Showing only changes of commit a95ba9d4b3 - Show all commits

View File

@@ -1,4 +1,4 @@
import { Context, Effect, Effectable, ExecutionStrategy, Function, identity, Predicate, Runtime, Scope, String, Tracer, type Types } from "effect" import { Context, Effect, Effectable, ExecutionStrategy, Function, Predicate, Runtime, Scope, String, Tracer, type Types } 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 Memoized from "./Memoized.js" import * as Memoized from "./Memoized.js"
@@ -7,35 +7,35 @@ import * as Memoized from "./Memoized.js"
export const TypeId: unique symbol = Symbol.for("effect-fc/Component") export const TypeId: unique symbol = Symbol.for("effect-fc/Component")
export type TypeId = typeof TypeId export type TypeId = typeof TypeId
export interface Component<F extends FunctionComponent, E, R> export interface Component<P, A extends React.ReactNode, E, R>
extends extends
Effect.Effect<F, never, Exclude<R, Scope.Scope>>, Effect.Effect<(props: P) => A, never, Exclude<R, Scope.Scope>>,
Component.Options Component.Options
{ {
new(_: never): {} new(_: never): {}
readonly [TypeId]: TypeId readonly [TypeId]: TypeId
readonly ["~FunctionComponent"]: F readonly ["~Props"]: P
readonly ["~Props"]: FunctionComponent.Props<F> readonly ["~Success"]: A
readonly ["~Success"]: FunctionComponent.Success<F>
readonly ["~Error"]: E readonly ["~Error"]: E
readonly ["~Context"]: R readonly ["~Context"]: R
/** @internal */ /** @internal */
readonly body: (props: FunctionComponent.Props<F>) => Effect.Effect<FunctionComponent.Success<F>, E, R> readonly body: (props: P) => Effect.Effect<A, E, R>
/** @internal */ /** @internal */
makeFunctionComponent( makeFunctionComponent(
runtimeRef: React.Ref<Runtime.Runtime<Exclude<R, Scope.Scope>>>, runtimeRef: React.Ref<Runtime.Runtime<Exclude<R, Scope.Scope>>>,
scope: Scope.Scope, scope: Scope.Scope,
): F ): (props: P) => A
} }
export namespace Component { 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 Props<T extends Component<any, any, any, any>> = T extends Component<infer P, infer _A, infer _E, infer _R> ? P : never
export type Error<T extends Component<any, any, any>> = T extends Component<infer _F, infer E, infer _R> ? E : never export type Success<T extends Component<any, any, any, any>> = T extends Component<infer _P, infer A, infer _E, infer _R> ? A : never
export type Context<T extends Component<any, any, any>> = T extends Component<infer _F, infer _E, infer R> ? R : never export type Error<T extends Component<any, any, any, any>> = T extends Component<infer _P, infer _A, infer E, infer _R> ? E : never
export type Context<T extends Component<any, any, any, any>> = T extends Component<infer _P, infer _A, infer _E, infer R> ? R : never
export type AsComponent<T extends Component<any, any, any>> = Component<FunctionComponent<T>, Error<T>, Context<T>> export type AsComponent<T extends Component<any, any, any, any>> = Component<Props<T>, Success<T>, Error<T>, Context<T>>
export interface Options { export interface Options {
readonly displayName?: string readonly displayName?: string
@@ -44,26 +44,19 @@ export namespace Component {
} }
} }
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({ const ComponentProto = Object.freeze({
...Effectable.CommitPrototype, ...Effectable.CommitPrototype,
[TypeId]: TypeId, [TypeId]: TypeId,
commit: Effect.fnUntraced(function* <F extends FunctionComponent, E, R>( commit: Effect.fnUntraced(function* <P, A extends React.ReactNode, E, R>(
this: Component<F, E, R> this: Component<P, A, E, R>
) { ) {
const self = this const self = this
const runtimeRef = React.useRef<Runtime.Runtime<Exclude<R, Scope.Scope>>>(null!) const runtimeRef = React.useRef<Runtime.Runtime<Exclude<R, Scope.Scope>>>(null!)
runtimeRef.current = yield* Effect.runtime<Exclude<R, Scope.Scope>>() runtimeRef.current = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
return React.useCallback(function ScopeProvider(props: FunctionComponent.Props<F>) { return React.useCallback(function ScopeProvider(props: P) {
const scope = Runtime.runSync(runtimeRef.current)(Hooks.useScope( const scope = Runtime.runSync(runtimeRef.current)(Hooks.useScope(
Array.from( Array.from(
Context.omit(...nonReactiveTags)(runtimeRef.current.context).unsafeMap.values() Context.omit(...nonReactiveTags)(runtimeRef.current.context).unsafeMap.values()
@@ -72,23 +65,23 @@ const ComponentProto = Object.freeze({
)) ))
const FC = React.useMemo(() => { const FC = React.useMemo(() => {
const f: React.FC<FunctionComponent.Props<F>> = self.makeFunctionComponent(runtimeRef, scope) const f: React.FC<P> = self.makeFunctionComponent(runtimeRef, scope)
f.displayName = self.displayName ?? "Anonymous" f.displayName = self.displayName ?? "Anonymous"
return Memoized.isMemoized(self) return Memoized.isMemoized(self)
? React.memo(f, self.propsAreEqual) ? React.memo(f, self.propsAreEqual)
: f : f
}, [scope]) }, [scope])
return React.createElement(FC, props) return React.createElement(FC as React.FC<{}>, props as {})
}, []) }, [])
}), }),
makeFunctionComponent<F extends FunctionComponent, E, R>( makeFunctionComponent<P, A extends React.ReactNode, E, R>(
this: Component<F, E, R>, this: Component<P, A, E, R>,
runtimeRef: React.RefObject<Runtime.Runtime<Exclude<R, Scope.Scope>>>, runtimeRef: React.RefObject<Runtime.Runtime<Exclude<R, Scope.Scope>>>,
scope: Scope.Scope, scope: Scope.Scope,
) { ) {
return (props: FunctionComponent.Props<F>) => Runtime.runSync(runtimeRef.current)( return (props: P) => Runtime.runSync(runtimeRef.current)(
Effect.provideService(this.body(props), Scope.Scope, scope) Effect.provideService(this.body(props), Scope.Scope, scope)
) )
}, },
@@ -102,7 +95,7 @@ const defaultOptions = {
const nonReactiveTags = [Tracer.ParentSpan] 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 isComponent = (u: unknown): u is Component<unknown, React.ReactNode, unknown, unknown> => Predicate.hasProperty(u, TypeId)
export const make = <Args extends readonly any[], A extends React.ReactNode, E, R>( export const make = <Args extends readonly any[], A extends React.ReactNode, E, R>(
body: (...args: Args) => Effect.Effect<A, E, R> body: (...args: Args) => Effect.Effect<A, E, R>
@@ -114,21 +107,15 @@ export const make = <Args extends readonly any[], A extends React.ReactNode, E,
ComponentProto, 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: { export const withOptions: {
<T extends Component<any, any, any>>( <T extends Component<any, any, any, any>>(
options: Partial<Component.Options> options: Partial<Component.Options>
): (self: T) => T ): (self: T) => T
<T extends Component<any, any, any>>( <T extends Component<any, any, any, any>>(
self: T, self: T,
options: Partial<Component.Options>, options: Partial<Component.Options>,
): T ): T
} = Function.dual(2, <T extends Component<any, any, any>>( } = Function.dual(2, <T extends Component<any, any, any, any>>(
self: T, self: T,
options: Partial<Component.Options>, options: Partial<Component.Options>,
): T => Object.setPrototypeOf( ): T => Object.setPrototypeOf(
@@ -137,17 +124,17 @@ export const withOptions: {
)) ))
export const withRuntime: { export const withRuntime: {
<F extends FunctionComponent, E, R>( <P, A extends React.ReactNode, E, R>(
context: React.Context<Runtime.Runtime<R>>, context: React.Context<Runtime.Runtime<R>>,
): (self: Component<F, E, Types.NoInfer<R>>) => F ): (self: Component<P, A, E, Types.NoInfer<R>>) => (props: P) => A
<F extends FunctionComponent, E, R>( <P, A extends React.ReactNode, E, R>(
self: Component<F, E, Types.NoInfer<R>>, self: Component<P, A, E, Types.NoInfer<R>>,
context: React.Context<Runtime.Runtime<R>>, context: React.Context<Runtime.Runtime<R>>,
): F ): (props: P) => A
} = Function.dual(2, <F extends FunctionComponent, E, R>( } = Function.dual(2, <P extends {}, A extends React.ReactNode, E, R>(
self: Component<F, E, R>, self: Component<P, A, E, R>,
context: React.Context<Runtime.Runtime<R>>, context: React.Context<Runtime.Runtime<R>>,
) => function WithRuntime(props: FunctionComponent.Props<F>) { ) => function WithRuntime(props: P) {
return React.createElement( return React.createElement(
Runtime.runSync(React.useContext(context))(self), Runtime.runSync(React.useContext(context))(self),
props, props,