From 6d5f3c6888b3767e442cc748bf4c811b1ac8d4c4 Mon Sep 17 00:00:00 2001 From: * <8> Date: Sat, 25 Dec 2021 15:07:38 +0800 Subject: [PATCH 1/3] Match-id-36a3aeae070795ceee07c5bd46be79ae9649ebc2 --- .../renderer/components/BaseClassComponent.ts | 13 ++++--- libs/horizon/src/renderer/components/Lazy.ts | 2 +- libs/horizon/src/renderer/hooks/BaseHook.ts | 37 ++++++++++++------- libs/horizon/src/renderer/hooks/HookMain.ts | 16 ++++---- libs/horizon/src/renderer/hooks/HookType.ts | 1 - .../src/renderer/hooks/UseEffectHook.ts | 14 +++---- .../src/renderer/hooks/UseReducerHook.ts | 3 +- .../src/renderer/taskExecutor/TaskExecutor.ts | 2 +- path.json | 3 -- 9 files changed, 48 insertions(+), 43 deletions(-) delete mode 100644 path.json 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 {
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"
-}
From d217302bf154a47c4da00f0015179dcf0db99cb4 Mon Sep 17 00:00:00 2001
From: * <8>
Date: Sat, 25 Dec 2021 15:08:34 +0800
Subject: [PATCH 2/3] Match-id-6bc4f99ab260e07592cabc5ef99f2985d3befb0e
---
libs/horizon/src/renderer/ContextSaver.ts | 147 +++++++++
libs/horizon/src/renderer/ErrorHandler.ts | 189 ++++++++++++
libs/horizon/src/renderer/ExecuteMode.ts | 33 +++
libs/horizon/src/renderer/Renderer.ts | 48 +++
libs/horizon/src/renderer/Types.ts | 58 ++++
libs/horizon/src/renderer/utils/compare.ts | 51 ++++
.../horizon/src/renderer/utils/elementType.ts | 11 +
.../horizon/src/renderer/utils/throwIfTrue.ts | 13 +
.../src/renderer/vnode/ProcessingVNode.ts | 8 +
libs/horizon/src/renderer/vnode/VNode.ts | 98 ++++++
libs/horizon/src/renderer/vnode/VNodeFlags.ts | 92 ++++++
.../src/renderer/vnode/VNodeShouldUpdate.ts | 66 +++++
libs/horizon/src/renderer/vnode/VNodeTags.ts | 21 ++
libs/horizon/src/renderer/vnode/VNodeUtils.ts | 278 ++++++++++++++++++
14 files changed, 1113 insertions(+)
create mode 100644 libs/horizon/src/renderer/ContextSaver.ts
create mode 100644 libs/horizon/src/renderer/ErrorHandler.ts
create mode 100644 libs/horizon/src/renderer/ExecuteMode.ts
create mode 100644 libs/horizon/src/renderer/Renderer.ts
create mode 100644 libs/horizon/src/renderer/Types.ts
create mode 100644 libs/horizon/src/renderer/utils/compare.ts
create mode 100644 libs/horizon/src/renderer/utils/elementType.ts
create mode 100644 libs/horizon/src/renderer/utils/throwIfTrue.ts
create mode 100644 libs/horizon/src/renderer/vnode/ProcessingVNode.ts
create mode 100644 libs/horizon/src/renderer/vnode/VNode.ts
create mode 100644 libs/horizon/src/renderer/vnode/VNodeFlags.ts
create mode 100644 libs/horizon/src/renderer/vnode/VNodeShouldUpdate.ts
create mode 100644 libs/horizon/src/renderer/vnode/VNodeTags.ts
create mode 100644 libs/horizon/src/renderer/vnode/VNodeUtils.ts
diff --git a/libs/horizon/src/renderer/ContextSaver.ts b/libs/horizon/src/renderer/ContextSaver.ts
new file mode 100644
index 00000000..b5200eaa
--- /dev/null
+++ b/libs/horizon/src/renderer/ContextSaver.ts
@@ -0,0 +1,147 @@
+/**
+ * 保存与深度遍历相关的一些context。
+ * 在深度遍历过程中,begin阶段会修改一些全局的值,在complete阶段会恢复。
+ */
+
+import type {VNode, ContextType} from './Types';
+import type {Container} from '../dom/DOMOperator';
+
+import {getNSCtx} from '../dom/DOMOperator';
+import {ContextProvider} from './vnode/VNodeTags';
+
+// 保存的是“http://www.w3.org/1999/xhtml”或“http://www.w3.org/2000/svg”,
+// 用于识别是使用document.createElement()还是使用document.createElementNS()创建DOM
+const CTX_NAMESPACE = 'CTX_NAMESPACE';
+
+// 保存的是Horizon.createContext()的值,或Provider重新设置的值
+const CTX_CONTEXT = 'CTX_CONTEXT';
+
+// 旧版context API,是否更改。
+const CTX_OLD_CHANGE = 'CTX_OLD_CHANGE';
+// 旧版context API,保存的是的当前组件提供给子组件使用的context。
+const CTX_OLD_CONTEXT = 'CTX_OLD_CONTEXT';
+// 旧版context API,保存的是的上一个提供者提供给后代组件使用的context。
+const CTX_OLD_PREVIOUS_CONTEXT = 'CTX_OLD_PREVIOUS_CONTEXT';
+
+let ctxNamespace: string = '';
+
+let ctxOldContext: Object = {};
+let ctxOldChange: Boolean = false;
+let ctxOldPreviousContext: Object = {};
+
+// capture阶段设置
+function setNamespaceCtx(vNode: VNode, dom?: Container) {
+ const nextContext = getNSCtx(dom, ctxNamespace, vNode.type);
+
+ vNode.setContext(CTX_NAMESPACE, ctxNamespace);
+ ctxNamespace = nextContext;
+}
+
+// bubble阶段恢复
+function resetNamespaceCtx(vNode: VNode) {
+ ctxNamespace = vNode.getContext(CTX_NAMESPACE);
+}
+
+function getNamespaceCtx(): string {
+ return ctxNamespace;
+}
+
+function setContextCtx(
+ initialState: (() => S) | S
+ ): [S, Trigger<((S) => S) | S>]
+};
+export type UseReducerHookType = {
+ useReducer(
+ reducer: (S, A) => S,
+ initArg: P, init?: (P) => S,
+ ): [S, Trigger]
+};
+export type UseContextHookType = { useContext