import type { FC } from '../jsx'; import type { AnyFn } from '../util'; import type { ComponentState, Context, ContextState } from './common'; import { CONTEXT, CONTEXT_STATE, NON_ROOT_COMPONENT_NODES, ONMOUNT, REFS, ROOT_NODES, STATE, UNMOUNT_FNS, __ } from './common'; import type { Ref, RefFn } from './ref'; /** * 用于判定是否是 Component 的函数。比 instanceof 要快很多。https://jsperf.app/bufamo */ export declare function isComponent(v: unknown): v is ComponentHost; export declare class ComponentHost { /** * 用于判定是否是 Component 的属性。比 instanceof 要快很多。https://jsperf.app/bufamo */ readonly [__] = true; /** * 将构造函数传递来的 attrs 存下来,以便可以在后期使用,以及在组件销毁时销毁该 attrs。 * 如果传递的 attrs 不是 ViewModel,则说明没有需要监听绑定的 attribute,不保存该 attrs。 */ /** * 组件的上下文对象 */ [CONTEXT]?: Context; [CONTEXT_STATE]: ContextState; /** * 组件的状态 */ [STATE]: ComponentState; /** * NON_ROOT_COMPONENT_NODES means nearest non-root component-nodes in the view-tree. * Node in view-tree have two types, html-node and component-node. * html-node include html dom node and html text node, * component-node is an instance of a Component. * For example, we have rendered a view-tree: * RootApp(Component) * / | \ * h1(Html) h2(Html) A(Component) * | | * C(Component) D(Component) * * The nearest non-root component-nodes of RootApp include C, * but not include A(as it's root) or D(as it's not nearest). * * By the way, the ROOT_NODES of view-tree above is [h1, h2, A] */ [NON_ROOT_COMPONENT_NODES]: ComponentHost[]; [REFS]?: (Ref | RefFn)[]; [ONMOUNT]?: AnyFn[]; /** * store functions will be called before unmount/destroy */ [UNMOUNT_FNS]?: AnyFn[]; /** * ROOT_NODES means root children of this component, * include html-nodes and component-nodes. * We use this infomation to remove DOM after this component is destroied. * We do not maintain the whole parent-child view-tree but only root children, * because when we remove the root children, whole view-tree will be * removed, so we do not need waste memory to maintain whole view-tree. */ [ROOT_NODES]: (ComponentHost | Node)[]; constructor(context?: Context); } /** * Get first rendered DOM Node after Component is rendered. * * 按从左往右从上到下的深度遍历,找到的第一个 DOM 节点。 */ export declare function getFirstDOM(component: ComponentHost): T; /** * Get last rendered DOM Node after Component is rendered. * * 按从右往左,从上到下的深度遍历,找到的第一个 DOM 节点(相对于从左到右的顺序是最后一个 DOM 节点)。 */ export declare function getLastDOM(component: ComponentHost): T; export declare function handleRenderDone(component: ComponentHost): void; export declare function addMountFn(component: ComponentHost, fn: AnyFn): void; export declare function addUnmountFn(component: ComponentHost, fn: AnyFn): void; /** * 销毁组件的内容,但不销毁组件本身。 */ export declare function destroyComponentContent(target: ComponentHost, removeDOM?: boolean): void; /** * 销毁组件 */ export declare function destroyComponent(target: ComponentHost, removeDOM?: boolean): void; /** 重置组件,销毁旧的,重置为全新。 */ export declare function resetComponent(target: ComponentHost, context?: Context): void; export declare function getComponentContext(component: ComponentHost, key: string | symbol): T; export declare function setComponentContext(component: ComponentHost, key: string | symbol, value: unknown): void; export declare function renderFunctionComponent(host: ComponentHost, fc: T, attrs?: any): Node[]; export declare function beforeReplaceRender(host: ComponentHost, context: Context | undefined, placeholder: string): { $parent: Node; $placeholder: Comment; }; export declare function afterReplaceRender(host: ComponentHost, nodes: Node[], $parent: Node, $placeholder: Node): void; export declare function replaceRenderFunctionComponent(host: ComponentHost, fc: FC | undefined, context: Context | undefined, props: any, placeholder?: string): void; export declare function renderSlotFunction(host: ComponentHost, slotFc: FC | undefined, attrs?: any): Node[]; export declare function replaceRenderSlot(host: ComponentHost, slotFc: FC | undefined, context: Context | undefined, props: any, placeholder?: string): void;