diff --git a/libs/horizon/src/renderer/components/BaseClassComponent.ts b/libs/horizon/src/renderer/components/BaseClassComponent.ts index 12dab04e..ec141650 100644 --- a/libs/horizon/src/renderer/components/BaseClassComponent.ts +++ b/libs/horizon/src/renderer/components/BaseClassComponent.ts @@ -1,11 +1,12 @@ /** * Component的api setState和forceUpdate在实例生成阶段实现 */ -class Component { - props; - context; +class Component { + props: P; + context: C; + state: S; - constructor(props, context) { + constructor(props: P, context: C) { this.props = props; this.context = context; } @@ -16,8 +17,8 @@ Component.prototype.isReactComponent = true; /** * 支持PureComponent */ -class PureComponent extends Component { - constructor(props, context) { +class PureComponent extends Component { + constructor(props: P, context: C) { super(props, context); } } diff --git a/libs/horizon/src/renderer/components/Lazy.ts b/libs/horizon/src/renderer/components/Lazy.ts index 55a276c5..4b37091e 100644 --- a/libs/horizon/src/renderer/components/Lazy.ts +++ b/libs/horizon/src/renderer/components/Lazy.ts @@ -15,7 +15,7 @@ type LazyContent = { }; export type LazyComponent = { - vtype: Symbol | number, + vtype: number, _content: P, _load: (content: P) => T, }; diff --git a/libs/horizon/src/renderer/hooks/BaseHook.ts b/libs/horizon/src/renderer/hooks/BaseHook.ts index ffbd3e6a..18e59db1 100644 --- a/libs/horizon/src/renderer/hooks/BaseHook.ts +++ b/libs/horizon/src/renderer/hooks/BaseHook.ts @@ -3,7 +3,8 @@ import type {Hook} from './HookType'; let processingVNode: VNode = null; -let activatedHook: Hook | null = null; +// lastTimeHook是上一次执行func时产生的hooks中,与currentHook对应的hook +let lastTimeHook: Hook | null = null; // 当前hook函数对应的hook对象 let currentHook: Hook | null = null; @@ -16,12 +17,12 @@ export function setProcessingVNode(vNode: VNode) { processingVNode = vNode; } -export function getActivatedHook() { - return activatedHook; +export function getLastTimeHook() { + return lastTimeHook; } -export function setActivatedHook(hook: Hook) { - activatedHook = hook; +export function setLastTimeHook(hook: Hook) { + lastTimeHook = hook; } export function setCurrentHook(hook: Hook) { @@ -47,24 +48,32 @@ export function createHook(state: any = null): Hook { return currentHook; } -export function getNextHook(hook: Hook, vNode: VNode) { - return vNode.hooks[hook.hIndex + 1] || null; +export function getNextHook(hook: Hook, hooks: Array>) { + return hooks[hook.hIndex + 1] || null; } // 获取当前hook函数对应的hook对象。 -// processing中的hook和activated中的hook,需要同时往前走, -// 原因:1.比对hook的数量有没有变化(非必要);2.从activated中的hook获取removeEffect +// processing中的hook和上一次执行中的hook,需要同时往前走, +// 原因:1.比对hook的数量有没有变化(非必要);2.从上一次执行中的hook获取removeEffect export function getCurrentHook(): Hook { - currentHook = currentHook !== null ? getNextHook(currentHook, processingVNode) : (processingVNode.hooks[0] || null); - const activated = processingVNode.twins; - activatedHook = activatedHook !== null ? getNextHook(activatedHook, activated) : ((activated && activated.hooks[0]) || null); + currentHook = currentHook !== null ? getNextHook(currentHook, processingVNode.hooks) : (processingVNode.hooks[0] || null); + + if (lastTimeHook !== null) { + lastTimeHook = getNextHook(lastTimeHook, processingVNode.oldHooks); + } else { + if (processingVNode.oldHooks && processingVNode.oldHooks.length) { + lastTimeHook = processingVNode.oldHooks[0]; + } else { + lastTimeHook = null; + } + } if (currentHook === null) { - if (activatedHook === null) { + if (lastTimeHook === null) { throw Error('Hooks are more than expected, please check whether the hook is written in the condition.'); } - createHook(activatedHook.state); + createHook(lastTimeHook.state); } return currentHook; diff --git a/libs/horizon/src/renderer/hooks/HookMain.ts b/libs/horizon/src/renderer/hooks/HookMain.ts index 713696d3..e8992e5a 100644 --- a/libs/horizon/src/renderer/hooks/HookMain.ts +++ b/libs/horizon/src/renderer/hooks/HookMain.ts @@ -9,9 +9,9 @@ const { import {getNewContext} from '../components/context/Context'; import { - getActivatedHook, + getLastTimeHook, getProcessingVNode, - setActivatedHook, + setLastTimeHook, setProcessingVNode, setCurrentHook, getNextHook } from './BaseHook'; @@ -24,7 +24,6 @@ export function exeFunctionHook( funcComp: (props: Object, arg: Object) => any, props: Object, arg: Object, - activated: VNode | null, processing: VNode, ): any { // 重置全局变量 @@ -35,11 +34,12 @@ export function exeFunctionHook( setProcessingVNode(processing); + processing.oldHooks = processing.hooks; processing.hooks = []; processing.effectList = []; // 设置hook阶段 - if (activated === null || !activated.hooks.length) { + if (processing.isCreated || !processing.oldHooks.length) { setHookStage(HookStage.Init); } else { setHookStage(HookStage.Update); @@ -51,9 +51,9 @@ export function exeFunctionHook( setHookStage(null); // 判断hook是否写在了if条件中,如果在if中会出现数量不对等的情况 - const activatedHook = getActivatedHook(); - if (activatedHook !== null) { - if (getNextHook(getActivatedHook(), activated) !== null) { + const lastTimeHook = getLastTimeHook(); + if (lastTimeHook !== null) { + if (getNextHook(getLastTimeHook(), processing.oldHooks) !== null) { throw Error('Hooks are less than expected, please check whether the hook is written in the condition.'); } } @@ -67,7 +67,7 @@ export function exeFunctionHook( function resetGlobalVariable() { setHookStage(null); setProcessingVNode(null); - setActivatedHook(null); + setLastTimeHook(null); setCurrentHook(null); } diff --git a/libs/horizon/src/renderer/hooks/HookType.ts b/libs/horizon/src/renderer/hooks/HookType.ts index 984b5ecd..e965fdf1 100644 --- a/libs/horizon/src/renderer/hooks/HookType.ts +++ b/libs/horizon/src/renderer/hooks/HookType.ts @@ -1,5 +1,4 @@ import {EffectConstant} from './EffectConstant'; -import {VNode} from '../Types'; export interface Hook { state: Reducer | Effect | Memo | CallBack | Ref; diff --git a/libs/horizon/src/renderer/hooks/UseEffectHook.ts b/libs/horizon/src/renderer/hooks/UseEffectHook.ts index c777f860..ccd4e7d4 100644 --- a/libs/horizon/src/renderer/hooks/UseEffectHook.ts +++ b/libs/horizon/src/renderer/hooks/UseEffectHook.ts @@ -1,7 +1,7 @@ import { createHook, getCurrentHook, - getActivatedHook, + getLastTimeHook, getProcessingVNode, throwNotInFuncError } from './BaseHook'; import {FlagUtils} from '../vnode/VNodeFlags'; @@ -38,10 +38,10 @@ function useEffect( } export function useEffectForInit(effectFunc, deps, effectType): void { - const hook = createHook(); - const nextDeps = deps !== undefined ? deps : null; FlagUtils.markUpdate(getProcessingVNode()); + const hook = createHook(); + const nextDeps = deps !== undefined ? deps : null; // 初始阶段,设置DepsChange标记位; 构造EffectList数组,并赋值给state hook.state = createEffect(effectFunc, undefined, nextDeps, EffectConstant.DepsChange | effectType); } @@ -51,13 +51,13 @@ export function useEffectForUpdate(effectFunc, deps, effectType): void { const nextDeps = deps !== undefined ? deps : null; let removeFunc; - if (getActivatedHook() !== null) { - const effect = getActivatedHook().state as Effect; - // removeEffect是通过执行effect返回的,所以需要在activated中获取 + if (getLastTimeHook() !== null) { + const effect = getLastTimeHook().state as Effect; + // removeEffect是通过执行effect返回的,所以需要在上一次hook中获取 removeFunc = effect.removeEffect; const lastDeps = effect.dependencies; - // 判断dependencies是否相同,不同就不设置DepsChange标记位 + // 判断dependencies是否相同,同相同不需要设置DepsChange标记位 if (nextDeps !== null && isArrayEqual(nextDeps, lastDeps)) { hook.state = createEffect(effectFunc, removeFunc, nextDeps, effectType); return; diff --git a/libs/horizon/src/renderer/hooks/UseReducerHook.ts b/libs/horizon/src/renderer/hooks/UseReducerHook.ts index 844afb0a..4c241152 100644 --- a/libs/horizon/src/renderer/hooks/UseReducerHook.ts +++ b/libs/horizon/src/renderer/hooks/UseReducerHook.ts @@ -54,10 +54,9 @@ function insertUpdate(action: A, hook: Hook): Update { // setState, setReducer触发函数 export function TriggerAction(vNode: VNode, hook: Hook, action: A) { const newUpdate = insertUpdate(action, hook); - const twins = vNode.twins; // 判断是否需要刷新 - if (!vNode.shouldUpdate && (twins === null || !twins.shouldUpdate)) { + if (!vNode.shouldUpdate) { const reducerObj = hook.state as Reducer; const { stateValue, reducer } = reducerObj; diff --git a/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts b/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts index 49e0c98d..f5a8b986 100644 --- a/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts +++ b/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts @@ -80,7 +80,7 @@ function callTasks(initialTime) { task.expirationTime > currentTime && isOverTime() ) { - // 没到deadline + // 任务的过期时间大于当前时间(没达到此任务的过期时间)且超过了deadline break; } diff --git a/path.json b/path.json deleted file mode 100644 index 8540fb5e..00000000 --- a/path.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "horizon-test-path": "../horizon-test" -}