| 
									
										
										
										
											2024-01-20 20:46:41 +08:00
										 |  |  | import { EffectScope } from '@vue/reactivity' | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { EMPTY_OBJ } from '@vue/shared' | 
					
						
							| 
									
										
										
										
											2023-12-29 22:05:33 +08:00
										 |  |  | import type { Block } from './render' | 
					
						
							|  |  |  | import type { DirectiveBinding } from './directive' | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   type ComponentPropsOptions, | 
					
						
							|  |  |  |   type NormalizedPropsOptions, | 
					
						
							|  |  |  |   normalizePropsOptions, | 
					
						
							|  |  |  | } from './componentProps' | 
					
						
							| 
									
										
										
										
											2024-02-04 21:18:57 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   type EmitFn, | 
					
						
							|  |  |  |   type EmitsOptions, | 
					
						
							|  |  |  |   type ObjectEmitsOptions, | 
					
						
							|  |  |  |   emit, | 
					
						
							|  |  |  |   normalizeEmitsOptions, | 
					
						
							|  |  |  | } from './componentEmits' | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-07 01:51:57 +08:00
										 |  |  | import type { Data } from '@vue/shared' | 
					
						
							| 
									
										
										
										
											2023-12-25 02:44:01 +08:00
										 |  |  | import { VaporLifecycleHooks } from './enums' | 
					
						
							| 
									
										
										
										
											2023-11-30 02:11:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  | export type Component = FunctionalComponent | ObjectComponent | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 14:59:11 +08:00
										 |  |  | export type SetupFn = (props: any, ctx: any) => Block | Data | 
					
						
							|  |  |  | export type FunctionalComponent = SetupFn & { | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  |   props: ComponentPropsOptions | 
					
						
							| 
									
										
										
										
											2024-02-04 21:18:57 +08:00
										 |  |  |   emits: EmitsOptions | 
					
						
							| 
									
										
										
										
											2023-12-06 14:59:11 +08:00
										 |  |  |   render(ctx: any): Block | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | export interface ObjectComponent { | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  |   props: ComponentPropsOptions | 
					
						
							| 
									
										
										
										
											2024-02-04 21:18:57 +08:00
										 |  |  |   emits: EmitsOptions | 
					
						
							| 
									
										
										
										
											2023-12-13 15:16:33 +08:00
										 |  |  |   setup?: SetupFn | 
					
						
							| 
									
										
										
										
											2023-12-06 14:59:11 +08:00
										 |  |  |   render(ctx: any): Block | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-15 01:47:56 +08:00
										 |  |  | type LifecycleHook<TFn = Function> = TFn[] | null | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-21 02:16:30 +08:00
										 |  |  | export interface ElementMetadata { | 
					
						
							|  |  |  |   props: Data | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 02:11:21 +08:00
										 |  |  | export interface ComponentInternalInstance { | 
					
						
							|  |  |  |   uid: number | 
					
						
							|  |  |  |   container: ParentNode | 
					
						
							|  |  |  |   block: Block | null | 
					
						
							|  |  |  |   scope: EffectScope | 
					
						
							| 
									
										
										
										
											2023-12-06 14:59:11 +08:00
										 |  |  |   component: FunctionalComponent | ObjectComponent | 
					
						
							| 
									
										
										
										
											2024-02-04 21:18:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // TODO: ExtraProps: key, ref, ...
 | 
					
						
							|  |  |  |   rawProps: { [key: string]: any } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // normalized options
 | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  |   propsOptions: NormalizedPropsOptions | 
					
						
							| 
									
										
										
										
											2024-02-04 21:18:57 +08:00
										 |  |  |   emitsOptions: ObjectEmitsOptions | null | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-23 15:17:18 +08:00
										 |  |  |   parent: ComponentInternalInstance | null | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  |   // state
 | 
					
						
							|  |  |  |   props: Data | 
					
						
							| 
									
										
										
										
											2024-02-10 13:07:13 +08:00
										 |  |  |   attrs: Data | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  |   setupState: Data | 
					
						
							| 
									
										
										
										
											2024-02-04 21:18:57 +08:00
										 |  |  |   emit: EmitFn | 
					
						
							|  |  |  |   emitted: Record<string, boolean> | null | 
					
						
							| 
									
										
										
										
											2024-01-21 13:59:56 +08:00
										 |  |  |   refs: Data | 
					
						
							|  |  |  |   metadata: WeakMap<Node, ElementMetadata> | 
					
						
							| 
									
										
										
										
											2023-11-30 02:11:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 22:11:40 +08:00
										 |  |  |   vapor: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-03 18:36:01 +08:00
										 |  |  |   /** directives */ | 
					
						
							|  |  |  |   dirs: Map<Node, DirectiveBinding[]> | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // lifecycle
 | 
					
						
							| 
									
										
										
										
											2024-01-20 20:46:41 +08:00
										 |  |  |   isMounted: boolean | 
					
						
							|  |  |  |   isUnmounted: boolean | 
					
						
							| 
									
										
										
										
											2024-01-13 03:25:57 +08:00
										 |  |  |   isUpdating: boolean | 
					
						
							| 
									
										
										
										
											2023-12-23 15:17:18 +08:00
										 |  |  |   // TODO: registory of provides, lifecycles, ...
 | 
					
						
							| 
									
										
										
										
											2023-12-15 01:47:56 +08:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.BEFORE_CREATE]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.CREATED]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.BEFORE_MOUNT]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.MOUNTED]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.BEFORE_UPDATE]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.UPDATED]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.BEFORE_UNMOUNT]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.UNMOUNTED]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.RENDER_TRACKED]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.RENDER_TRIGGERED]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.ACTIVATED]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.DEACTIVATED]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   [VaporLifecycleHooks.ERROR_CAPTURED]: LifecycleHook | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   // [VaporLifecycleHooks.SERVER_PREFETCH]: LifecycleHook<() => Promise<unknown>>
 | 
					
						
							| 
									
										
										
										
											2023-11-30 02:11:21 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-03 18:36:01 +08:00
										 |  |  | // TODO
 | 
					
						
							|  |  |  | export let currentInstance: ComponentInternalInstance | null = null | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const getCurrentInstance: () => ComponentInternalInstance | null = () => | 
					
						
							|  |  |  |   currentInstance | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const setCurrentInstance = (instance: ComponentInternalInstance) => { | 
					
						
							| 
									
										
										
										
											2024-01-19 16:38:41 +08:00
										 |  |  |   const prev = currentInstance | 
					
						
							| 
									
										
										
										
											2023-12-03 18:36:01 +08:00
										 |  |  |   currentInstance = instance | 
					
						
							| 
									
										
										
										
											2024-01-19 16:38:41 +08:00
										 |  |  |   instance.scope.on() | 
					
						
							|  |  |  |   return () => { | 
					
						
							|  |  |  |     instance.scope.off() | 
					
						
							|  |  |  |     currentInstance = prev | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-12-03 18:36:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const unsetCurrentInstance = () => { | 
					
						
							| 
									
										
										
										
											2024-01-19 16:38:41 +08:00
										 |  |  |   currentInstance?.scope.off() | 
					
						
							| 
									
										
										
										
											2023-12-03 18:36:01 +08:00
										 |  |  |   currentInstance = null | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 02:11:21 +08:00
										 |  |  | let uid = 0 | 
					
						
							|  |  |  | export const createComponentInstance = ( | 
					
						
							| 
									
										
										
										
											2023-12-06 14:59:11 +08:00
										 |  |  |   component: ObjectComponent | FunctionalComponent, | 
					
						
							| 
									
										
										
										
											2024-02-04 21:18:57 +08:00
										 |  |  |   rawProps: Data, | 
					
						
							| 
									
										
										
										
											2023-11-30 02:11:21 +08:00
										 |  |  | ): ComponentInternalInstance => { | 
					
						
							|  |  |  |   const instance: ComponentInternalInstance = { | 
					
						
							|  |  |  |     uid: uid++, | 
					
						
							|  |  |  |     block: null, | 
					
						
							| 
									
										
										
										
											2023-12-15 01:47:56 +08:00
										 |  |  |     container: null!, // set on mountComponent
 | 
					
						
							| 
									
										
										
										
											2023-11-30 02:11:21 +08:00
										 |  |  |     scope: new EffectScope(true /* detached */)!, | 
					
						
							|  |  |  |     component, | 
					
						
							| 
									
										
										
										
											2024-02-04 21:18:57 +08:00
										 |  |  |     rawProps, | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-23 15:17:18 +08:00
										 |  |  |     // TODO: registory of parent
 | 
					
						
							|  |  |  |     parent: null, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  |     // resolved props and emits options
 | 
					
						
							|  |  |  |     propsOptions: normalizePropsOptions(component), | 
					
						
							| 
									
										
										
										
											2024-02-04 21:18:57 +08:00
										 |  |  |     emitsOptions: normalizeEmitsOptions(component), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // emit
 | 
					
						
							|  |  |  |     emit: null!, // to be set immediately
 | 
					
						
							|  |  |  |     emitted: null, | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // state
 | 
					
						
							|  |  |  |     props: EMPTY_OBJ, | 
					
						
							| 
									
										
										
										
											2024-02-10 13:07:13 +08:00
										 |  |  |     attrs: EMPTY_OBJ, | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  |     setupState: EMPTY_OBJ, | 
					
						
							| 
									
										
										
										
											2024-01-21 13:59:56 +08:00
										 |  |  |     refs: EMPTY_OBJ, | 
					
						
							|  |  |  |     metadata: new WeakMap(), | 
					
						
							| 
									
										
										
										
											2024-01-24 22:11:40 +08:00
										 |  |  |     vapor: true, | 
					
						
							| 
									
										
										
										
											2023-12-10 01:33:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     dirs: new Map(), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // lifecycle
 | 
					
						
							| 
									
										
										
										
											2024-01-20 20:46:41 +08:00
										 |  |  |     isMounted: false, | 
					
						
							|  |  |  |     isUnmounted: false, | 
					
						
							| 
									
										
										
										
											2024-01-13 03:25:57 +08:00
										 |  |  |     isUpdating: false, | 
					
						
							| 
									
										
										
										
											2023-11-30 02:11:21 +08:00
										 |  |  |     // TODO: registory of provides, appContext, lifecycles, ...
 | 
					
						
							| 
									
										
										
										
											2023-12-15 01:47:56 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.BEFORE_CREATE]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.CREATED]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.BEFORE_MOUNT]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.MOUNTED]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.BEFORE_UPDATE]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.UPDATED]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.BEFORE_UNMOUNT]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.UNMOUNTED]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.RENDER_TRACKED]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.RENDER_TRIGGERED]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.ACTIVATED]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.DEACTIVATED]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     [VaporLifecycleHooks.ERROR_CAPTURED]: null, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @internal | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     // [VaporLifecycleHooks.SERVER_PREFETCH]: null,
 | 
					
						
							| 
									
										
										
										
											2023-11-30 02:11:21 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-02-04 21:18:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   instance.emit = emit.bind(null, instance) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 02:11:21 +08:00
										 |  |  |   return instance | 
					
						
							|  |  |  | } |