From e0ac32cda970fc1d29d6287651e1811aedc06ba0 Mon Sep 17 00:00:00 2001 From: * <8> Date: Thu, 7 Apr 2022 19:15:39 +0800 Subject: [PATCH 1/3] Match-id-30ecadc80ad5388949a0fc159b3355ae65b8f9b3 --- .eslintrc.js | 25 +++++++------------ libs/horizon/src/dom/utils/Common.ts | 2 +- .../src/renderer/vnode/VNodeCreator.ts | 6 ++--- .../src/renderer/vnode/VNodeShouldUpdate.ts | 2 +- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index ab410fd1..7aedabc1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,19 +1,13 @@ module.exports = { extends: [ 'eslint:recommended', - "plugin:@typescript-eslint/eslint-recommended", - "plugin:@typescript-eslint/recommended", + 'plugin:@typescript-eslint/eslint-recommended', + 'plugin:@typescript-eslint/recommended', 'prettier', ], root: true, - plugins: [ - 'jest', - 'no-for-of-loops', - 'no-function-declare-after-return', - 'react', - '@typescript-eslint', - ], + plugins: ['jest', 'no-for-of-loops', 'no-function-declare-after-return', 'react', '@typescript-eslint'], parser: '@typescript-eslint/parser', parserOptions: { @@ -34,8 +28,8 @@ module.exports = { rules: { '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/no-non-null-assertion': 'off', - 'semi': ['warn', 'always'], - 'quotes': ['warn', 'single'], + semi: ['warn', 'always'], + quotes: ['warn', 'single'], 'accessor-pairs': 'off', 'brace-style': ['error', '1tbs'], 'func-style': ['warn', 'declaration', { allowArrowFunctions: true }], @@ -44,19 +38,18 @@ module.exports = { // 尾随逗号 'comma-dangle': ['error', 'only-multiline'], + 'no-constant-condition': 'off', 'no-for-of-loops/no-for-of-loops': 'error', 'no-function-declare-after-return/no-function-declare-after-return': 'error', }, globals: { - isDev: true + isDev: true, }, overrides: [ { - files: [ - 'scripts/__tests__/**/*.js' - ], + files: ['scripts/__tests__/**/*.js'], globals: { - container: true + container: true, }, }, ], diff --git a/libs/horizon/src/dom/utils/Common.ts b/libs/horizon/src/dom/utils/Common.ts index 09602c82..dae06d2b 100644 --- a/libs/horizon/src/dom/utils/Common.ts +++ b/libs/horizon/src/dom/utils/Common.ts @@ -6,7 +6,7 @@ import {Props} from '../DOMOperator'; * @param doc 指定 document */ export function getFocusedDom(doc?: Document): HorizonDom | null { - let currentDocument = doc ?? document; + const currentDocument = doc ?? document; return currentDocument.activeElement ?? currentDocument.body; } diff --git a/libs/horizon/src/renderer/vnode/VNodeCreator.ts b/libs/horizon/src/renderer/vnode/VNodeCreator.ts index 932c7fa7..8b7ad630 100644 --- a/libs/horizon/src/renderer/vnode/VNodeCreator.ts +++ b/libs/horizon/src/renderer/vnode/VNodeCreator.ts @@ -38,7 +38,7 @@ const typeMap = { [TYPE_LAZY]: LazyComponent, }; -function newVirtualNode (tag: VNodeTag, key?: null | string, vNodeProps?: any, realNode?: any): VNode { +function newVirtualNode(tag: VNodeTag, key?: null | string, vNodeProps?: any, realNode?: any): VNode { return new VNode(tag, vNodeProps, key, realNode); } @@ -201,7 +201,7 @@ export function onlyUpdateChildVNodes(processing: VNode): VNode | null { sibling = sibling.next; } } - } + }; putChildrenIntoQueue(processing.child); @@ -210,7 +210,7 @@ export function onlyUpdateChildVNodes(processing: VNode): VNode | null { markVNodePath(vNode); - putChildrenIntoQueue(vNode) + putChildrenIntoQueue(vNode); } } // 子树无需工作 diff --git a/libs/horizon/src/renderer/vnode/VNodeShouldUpdate.ts b/libs/horizon/src/renderer/vnode/VNodeShouldUpdate.ts index 72afb236..ec5dce84 100644 --- a/libs/horizon/src/renderer/vnode/VNodeShouldUpdate.ts +++ b/libs/horizon/src/renderer/vnode/VNodeShouldUpdate.ts @@ -53,7 +53,7 @@ export function setParentsChildShouldUpdate(parent: VNode | null) { // 设置节点的所有父节点的childShouldUpdate export function updateParentsChildShouldUpdate(vNode: VNode) { let node = vNode.parent; - let isShouldUpdate = vNode.shouldUpdate || vNode.childShouldUpdate; + const isShouldUpdate = vNode.shouldUpdate || vNode.childShouldUpdate; if (isShouldUpdate) { // 开始节点是shouldUpdate或childShouldUpdate // 更新从当前节点到根节点的childShouldUpdate为true From 3de86a411cc0651c9dc7b4ed9a892aa4c54c2112 Mon Sep 17 00:00:00 2001 From: * <8> Date: Wed, 23 Mar 2022 10:28:58 +0800 Subject: [PATCH 2/3] Match-id-a642a9a90565e0bc7897482ce61d1ac65f8120c5 --- libs/horizon/src/renderer/ContextSaver.ts | 2 +- .../ComponentTest/HookTest/UseContext.test.js | 48 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/libs/horizon/src/renderer/ContextSaver.ts b/libs/horizon/src/renderer/ContextSaver.ts index 6fd6ef9a..cd21da0b 100644 --- a/libs/horizon/src/renderer/ContextSaver.ts +++ b/libs/horizon/src/renderer/ContextSaver.ts @@ -51,7 +51,7 @@ export function recoverParentContext(vNode: VNode) { while (parent !== null) { if (parent.tag === ContextProvider) { - parent.context = parent.props.value; + setContext(parent, parent.props.value); } parent = parent.parent; } diff --git a/scripts/__tests__/ComponentTest/HookTest/UseContext.test.js b/scripts/__tests__/ComponentTest/HookTest/UseContext.test.js index d94443b7..c0fc0a53 100644 --- a/scripts/__tests__/ComponentTest/HookTest/UseContext.test.js +++ b/scripts/__tests__/ComponentTest/HookTest/UseContext.test.js @@ -2,7 +2,7 @@ import * as Horizon from '@cloudsop/horizon/index.ts'; import { act } from '../../jest/customMatcher'; describe('useContext Hook Test', () => { - const { useState, useContext } = Horizon; + const { useState, useContext, createContext } = Horizon; const { unmountComponentAtNode } = Horizon; it('简单使用useContext', () => { @@ -14,16 +14,12 @@ describe('useContext Hook Test', () => { const SystemLanguageContext = Horizon.createContext(defaultValue); const SystemLanguageProvider = ({ type, children }) => { - return ( - - {children} - - ); + return {children}; }; const TestFunction = () => { const context = useContext(SystemLanguageContext); return

{context.type}

; - } + }; let setValue; const App = () => { const [value, _setValue] = useState(LanguageTypes.JAVA); @@ -34,8 +30,8 @@ describe('useContext Hook Test', () => { - ) - } + ); + }; Horizon.render(, container); // 测试当Provider未提供时,获取到的默认值'JavaScript'。 expect(container.querySelector('p').innerHTML).toBe('JavaScript'); @@ -47,4 +43,38 @@ describe('useContext Hook Test', () => { act(() => setValue(LanguageTypes.JAVASCRIPT)); expect(container.querySelector('p').innerHTML).toBe('JavaScript'); }); + + it('更新后useContext仍能获取到context', () => { + const Context = createContext({}); + const ref = Horizon.createRef(); + + function App() { + return ( + + + + ); + } + + let update; + + function Child() { + const context = useContext(Context); + const [_, setState] = useState({}); + update = () => setState({}); + + return
{context.text}
; + } + + Horizon.render(, container); + expect(ref.current.innerHTML).toBe('context'); + + update(); + + expect(ref.current.innerHTML).toBe('context'); + }); }); From a7e601ad08ef1f54abfe84f89e7658a574ca0246 Mon Sep 17 00:00:00 2001 From: * <8> Date: Fri, 8 Apr 2022 14:27:16 +0800 Subject: [PATCH 3/3] Match-id-55d57069bcbae4d9269d2060b72e9784ec558608 --- scripts/__tests__/ComponentTest/HookTest/UseContext.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/__tests__/ComponentTest/HookTest/UseContext.test.js b/scripts/__tests__/ComponentTest/HookTest/UseContext.test.js index dfa2aec9..bce485c6 100644 --- a/scripts/__tests__/ComponentTest/HookTest/UseContext.test.js +++ b/scripts/__tests__/ComponentTest/HookTest/UseContext.test.js @@ -1,6 +1,6 @@ import * as Horizon from '@cloudsop/horizon/index.ts'; -describe('useContext Hook Test', () => {; +describe('useContext Hook Test', () => { const { useState, useContext, createContext, act, unmountComponentAtNode } = Horizon; it('简单使用useContext', () => {