This commit is contained in:
@@ -4,12 +4,15 @@ import * as Hook from "./Hook.js"
|
|||||||
import type { ExcludeKeys } from "./utils.js"
|
import type { ExcludeKeys } from "./utils.js"
|
||||||
|
|
||||||
|
|
||||||
export interface Component<E, R, P extends {}> extends Pipeable.Pipeable {
|
export const TypeId: unique symbol = Symbol.for("effect-fc/Component")
|
||||||
|
export type TypeId = typeof TypeId
|
||||||
|
|
||||||
|
export interface Component<E, R, P extends {}> extends Pipeable.Pipeable, Component.Options {
|
||||||
new(_: never): {}
|
new(_: never): {}
|
||||||
|
|
||||||
|
readonly [TypeId]: TypeId
|
||||||
readonly body: (props: P) => Effect.Effect<React.ReactNode, E, R>
|
readonly body: (props: P) => Effect.Effect<React.ReactNode, E, R>
|
||||||
readonly displayName?: string
|
readonly displayName?: string
|
||||||
readonly options: Component.Options
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace Component {
|
export namespace Component {
|
||||||
@@ -25,17 +28,22 @@ export namespace Component {
|
|||||||
|
|
||||||
|
|
||||||
const ComponentProto = Object.seal({
|
const ComponentProto = Object.seal({
|
||||||
|
[TypeId]: TypeId,
|
||||||
pipe() { return Pipeable.pipeArguments(this, arguments) }
|
pipe() { return Pipeable.pipeArguments(this, arguments) }
|
||||||
} as const)
|
} as const)
|
||||||
|
|
||||||
const defaultOptions: Component.Options = {
|
const makeWithDefaults = (): Component<any, any, any> => Object.assign(
|
||||||
finalizerExecutionMode: "sync",
|
Object.setPrototypeOf(function() {}, ComponentProto), {
|
||||||
finalizerExecutionStrategy: ExecutionStrategy.sequential,
|
finalizerExecutionMode: "sync",
|
||||||
}
|
finalizerExecutionStrategy: ExecutionStrategy.sequential,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const nonReactiveTags = [Tracer.ParentSpan] as const
|
const nonReactiveTags = [Tracer.ParentSpan] as const
|
||||||
|
|
||||||
|
|
||||||
|
export const isComponent = (u: unknown): u is Component<any, any, any> => Predicate.hasProperty(u, TypeId)
|
||||||
|
|
||||||
export namespace make {
|
export namespace make {
|
||||||
export type Gen = {
|
export type Gen = {
|
||||||
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, P extends {} = {}>(
|
<Eff extends Utils.YieldWrap<Effect.Effect<any, any, any>>, P extends {} = {}>(
|
||||||
@@ -268,31 +276,31 @@ export const make: (
|
|||||||
spanName: string,
|
spanName: string,
|
||||||
spanOptions?: Tracer.SpanOptions,
|
spanOptions?: Tracer.SpanOptions,
|
||||||
) => make.Gen & make.NonGen)
|
) => make.Gen & make.NonGen)
|
||||||
) = (spanNameOrBody: Function | string, ...pipeables: any[]) => {
|
) = (spanNameOrBody: Function | string, ...pipeables: any[]): any => {
|
||||||
if (typeof spanNameOrBody !== "string") {
|
if (typeof spanNameOrBody !== "string") {
|
||||||
const displayName = displayNameFromBody(spanNameOrBody)
|
const displayName = displayNameFromBody(spanNameOrBody)
|
||||||
return Object.setPrototypeOf({
|
return Object.assign(makeWithDefaults(), {
|
||||||
body: displayName
|
body: displayName
|
||||||
? Effect.fn(displayName)(spanNameOrBody as any, ...pipeables as [])
|
? Effect.fn(displayName)(spanNameOrBody as any, ...pipeables as [])
|
||||||
: Effect.fn(spanNameOrBody as any, ...pipeables),
|
: Effect.fn(spanNameOrBody as any, ...pipeables),
|
||||||
displayName,
|
displayName,
|
||||||
options: { ...defaultOptions },
|
})
|
||||||
}, ComponentProto)
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const spanOptions = pipeables[0]
|
const spanOptions = pipeables[0]
|
||||||
return (body: any, ...pipeables: any[]) => Object.setPrototypeOf({
|
return (body: any, ...pipeables: any[]) => Object.assign(makeWithDefaults(), {
|
||||||
body: Effect.fn(spanNameOrBody, spanOptions)(body, ...pipeables as []),
|
body: Effect.fn(spanNameOrBody, spanOptions)(body, ...pipeables as []),
|
||||||
displayName: displayNameFromBody(body) ?? spanNameOrBody,
|
displayName: displayNameFromBody(body) ?? spanNameOrBody,
|
||||||
options: { ...defaultOptions },
|
})
|
||||||
}, ComponentProto)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const makeUntraced: make.Gen & make.NonGen = (body: Function, ...pipeables: any[]) => Object.setPrototypeOf({
|
export const makeUntraced: make.Gen & make.NonGen = (
|
||||||
|
body: Function,
|
||||||
|
...pipeables: any[]
|
||||||
|
) => Object.assign(makeWithDefaults(), {
|
||||||
body: Effect.fnUntraced(body as any, ...pipeables as []),
|
body: Effect.fnUntraced(body as any, ...pipeables as []),
|
||||||
displayName: displayNameFromBody(body),
|
displayName: displayNameFromBody(body),
|
||||||
options: { ...defaultOptions },
|
|
||||||
}, ComponentProto)
|
}, ComponentProto)
|
||||||
|
|
||||||
const displayNameFromBody = (body: Function) => !String.isEmpty(body.name) ? body.name : undefined
|
const displayNameFromBody = (body: Function) => !String.isEmpty(body.name) ? body.name : undefined
|
||||||
@@ -326,7 +334,7 @@ export const withOptions: {
|
|||||||
self: T,
|
self: T,
|
||||||
options: Partial<Component.Options>,
|
options: Partial<Component.Options>,
|
||||||
): T => Object.setPrototypeOf(
|
): T => Object.setPrototypeOf(
|
||||||
{ ...self, options: { ...self.options, ...options } },
|
{ ...self, ...options },
|
||||||
Object.getPrototypeOf(self),
|
Object.getPrototypeOf(self),
|
||||||
))
|
))
|
||||||
|
|
||||||
@@ -427,7 +435,7 @@ export const useFC: {
|
|||||||
Array.from(
|
Array.from(
|
||||||
Context.omit(...nonReactiveTags)(runtimeRef.current.context).unsafeMap.values()
|
Context.omit(...nonReactiveTags)(runtimeRef.current.context).unsafeMap.values()
|
||||||
),
|
),
|
||||||
self.options,
|
self,
|
||||||
))
|
))
|
||||||
|
|
||||||
const FC = React.useMemo(() => {
|
const FC = React.useMemo(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user