From ccbf2b44c725a5ea2d370c65a2a7d834ec694bca Mon Sep 17 00:00:00 2001
From: * <8>
Date: Wed, 28 Sep 2022 16:39:59 +0800
Subject: [PATCH 1/3] Match-id-4b13b18c30c711bb1f976790916f8e43047f285b
---
libs/horizon/index.ts | 24 ++++++-
libs/horizon/src/external/HorizonIs.ts | 67 +++++++++++++++++++
scripts/__tests__/HorizonIsTest/index.test.js | 58 ++++++++++++++++
3 files changed, 148 insertions(+), 1 deletion(-)
create mode 100644 libs/horizon/src/external/HorizonIs.ts
create mode 100644 scripts/__tests__/HorizonIsTest/index.test.js
diff --git a/libs/horizon/index.ts b/libs/horizon/index.ts
index 4c067a3e..c11b255a 100644
--- a/libs/horizon/index.ts
+++ b/libs/horizon/index.ts
@@ -30,7 +30,15 @@ import {
import { asyncUpdates } from './src/renderer/TreeBuilder';
import { callRenderQueueImmediate } from './src/renderer/taskExecutor/RenderQueue';
import { runAsyncEffects } from './src/renderer/submit/HookEffectHandler';
-
+import {
+ isContextProvider,
+ isElement,
+ isForwardRef,
+ isFragment,
+ isLazy,
+ isMemo,
+ isPortal,
+} from './src/external/HorizonIs';
import { createStore, useStore, clearStore } from './src/horizonx/store/StoreHandler';
import * as reduxAdapter from './src/horizonx/adapters/redux';
import { watch } from './src/horizonx/proxy/watch';
@@ -87,6 +95,13 @@ const Horizon = {
clearStore,
reduxAdapter,
watch,
+ isFragment,
+ isElement,
+ isForwardRef,
+ isLazy,
+ isMemo,
+ isPortal,
+ isContextProvider
};
export const version = __VERSION__;
@@ -128,6 +143,13 @@ export {
clearStore,
reduxAdapter,
watch,
+ isFragment,
+ isElement,
+ isForwardRef,
+ isLazy,
+ isMemo,
+ isPortal,
+ isContextProvider
};
export default Horizon;
diff --git a/libs/horizon/src/external/HorizonIs.ts b/libs/horizon/src/external/HorizonIs.ts
new file mode 100644
index 00000000..fb666530
--- /dev/null
+++ b/libs/horizon/src/external/HorizonIs.ts
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
+ */
+import {
+ TYPE_COMMON_ELEMENT,
+ TYPE_CONTEXT,
+ TYPE_FORWARD_REF,
+ TYPE_FRAGMENT,
+ TYPE_LAZY,
+ TYPE_MEMO,
+ TYPE_PORTAL,
+ TYPE_PROVIDER,
+ TYPE_SUSPENSE,
+} from './JSXElementType';
+
+function isObject(i) {
+ return Object.prototype.toString.call(i) === '[object Object]';
+}
+
+function getType(ele: any) {
+ if (isObject(ele)) {
+ const type = ele.type;
+ if ([TYPE_FRAGMENT, TYPE_SUSPENSE].includes(type)) {
+ return type;
+ }
+
+ const vtypeOfType = type?.vtype;
+ if ([TYPE_PROVIDER, TYPE_LAZY, TYPE_FORWARD_REF, TYPE_CONTEXT].includes(vtypeOfType)) {
+ return vtypeOfType;
+ }
+
+ const vtype = ele.vtype;
+ if([TYPE_MEMO, TYPE_FORWARD_REF, TYPE_PORTAL].includes(vtype)) {
+ return vtype;
+ }
+ }
+
+ return undefined;
+}
+
+export function isElement(ele: any) {
+ return isObject(ele) && ele.vtype === TYPE_COMMON_ELEMENT;
+}
+
+export function isFragment(ele: any) {
+ return getType(ele) === TYPE_FRAGMENT;
+}
+
+export function isForwardRef(ele: any) {
+ return getType(ele) === TYPE_FORWARD_REF;
+}
+
+export function isLazy(ele: any) {
+ return getType(ele) === TYPE_LAZY;
+}
+
+export function isMemo(ele: any) {
+ return getType(ele) === TYPE_MEMO;
+}
+
+export function isPortal(ele: any) {
+ return getType(ele) === TYPE_PORTAL;
+}
+
+export function isContextProvider(ele: any) {
+ return getType(ele) === TYPE_PROVIDER;
+}
diff --git a/scripts/__tests__/HorizonIsTest/index.test.js b/scripts/__tests__/HorizonIsTest/index.test.js
new file mode 100644
index 00000000..5a4b47e1
--- /dev/null
+++ b/scripts/__tests__/HorizonIsTest/index.test.js
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
+ */
+
+import * as Horizon from '@cloudsop/horizon/index.ts';
+
+function App() {
+ return <>>;
+}
+
+describe('HorizonIs', () => {
+ it('should identify horizon elements', () => {
+ expect(Horizon.isElement(
)).toBe(true);
+ expect(Horizon.isElement('span')).toBe(false);
+ expect(Horizon.isElement(111)).toBe(false);
+ expect(Horizon.isElement(false)).toBe(false);
+ expect(Horizon.isElement(null)).toBe(false);
+ expect(Horizon.isElement([])).toBe(false);
+ expect(Horizon.isElement({})).toBe(false);
+ expect(Horizon.isElement(undefined)).toBe(false);
+
+ const TestContext = Horizon.createContext(false);
+ expect(Horizon.isElement()).toBe(true);
+ expect(Horizon.isElement()).toBe(true);
+ expect(Horizon.isElement(<>>)).toBe(true);
+ expect(Horizon.isElement()).toBe(true);
+ });
+
+ it('should identify Fragment', () => {
+ expect(Horizon.isFragment(<>>)).toBe(true);
+ });
+
+ it('should identify memo component', () => {
+ const memo = Horizon.memo(App);
+ expect(Horizon.isMemo(memo)).toBe(true);
+ });
+
+ it('should identify forwardRef', () => {
+ const ForwardRefComp = Horizon.forwardRef(App);
+ expect(Horizon.isForwardRef()).toBe(true);
+ });
+
+ it('should identify lazy', () => {
+ const LazyComp = Horizon.lazy(() => App);
+ expect(Horizon.isLazy()).toBe(true);
+ });
+
+ it('should identify portal', () => {
+ const portal = Horizon.createPortal(, container);
+ expect(Horizon.isPortal(portal)).toBe(true);
+ });
+
+ it('should identify ContextProvider', () => {
+ const TestContext = Horizon.createContext(false);
+ expect(Horizon.isContextProvider()).toBe(true);
+ expect(Horizon.isContextProvider()).toBe(false);
+ });
+});
From 0c13e7c7de2bd2fac46707bfe197464e9c6cf0a4 Mon Sep 17 00:00:00 2001
From: * <8>
Date: Wed, 28 Sep 2022 17:10:18 +0800
Subject: [PATCH 2/3] Match-id-98ddbf9f0fc3a569ddf1eedfc960b09377dbc737
---
libs/horizon/index.ts | 7 +++++--
libs/horizon/src/external/HorizonIs.ts | 21 +++++++++++++++----
scripts/__tests__/HorizonIsTest/index.test.js | 6 ++++--
3 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/libs/horizon/index.ts b/libs/horizon/index.ts
index c11b255a..05aa5d6f 100644
--- a/libs/horizon/index.ts
+++ b/libs/horizon/index.ts
@@ -32,6 +32,7 @@ import { callRenderQueueImmediate } from './src/renderer/taskExecutor/RenderQueu
import { runAsyncEffects } from './src/renderer/submit/HookEffectHandler';
import {
isContextProvider,
+ isContextConsumer,
isElement,
isForwardRef,
isFragment,
@@ -101,7 +102,8 @@ const Horizon = {
isLazy,
isMemo,
isPortal,
- isContextProvider
+ isContextProvider,
+ isContextConsumer
};
export const version = __VERSION__;
@@ -149,7 +151,8 @@ export {
isLazy,
isMemo,
isPortal,
- isContextProvider
+ isContextProvider,
+ isContextConsumer
};
export default Horizon;
diff --git a/libs/horizon/src/external/HorizonIs.ts b/libs/horizon/src/external/HorizonIs.ts
index fb666530..e2301e9e 100644
--- a/libs/horizon/src/external/HorizonIs.ts
+++ b/libs/horizon/src/external/HorizonIs.ts
@@ -13,10 +13,18 @@ import {
TYPE_SUSPENSE,
} from './JSXElementType';
-function isObject(i) {
- return Object.prototype.toString.call(i) === '[object Object]';
+function isObject(anyThing) {
+ return Object.prototype.toString.call(anyThing) === '[object Object]';
}
+/**
+ * 获取传入的element的类型
+ * 1. fragment, suspense 属于内置标签,类型位于type
+ * 2. memo, lazy, forwardRef 属于包装函数,产生新的对象,类型位于type.vtype
+ * 3. Context.Provider/Consumer 的类型是框架定义的对象,类型位于type.vtype
+ * 4. portal比较特殊,函数结果直接可以作为element,类型位于vtype
+ * @param ele
+ */
function getType(ele: any) {
if (isObject(ele)) {
const type = ele.type;
@@ -25,12 +33,12 @@ function getType(ele: any) {
}
const vtypeOfType = type?.vtype;
- if ([TYPE_PROVIDER, TYPE_LAZY, TYPE_FORWARD_REF, TYPE_CONTEXT].includes(vtypeOfType)) {
+ if ([TYPE_MEMO, TYPE_PROVIDER, TYPE_LAZY, TYPE_FORWARD_REF, TYPE_CONTEXT].includes(vtypeOfType)) {
return vtypeOfType;
}
const vtype = ele.vtype;
- if([TYPE_MEMO, TYPE_FORWARD_REF, TYPE_PORTAL].includes(vtype)) {
+ if(TYPE_PORTAL === vtype) {
return vtype;
}
}
@@ -65,3 +73,8 @@ export function isPortal(ele: any) {
export function isContextProvider(ele: any) {
return getType(ele) === TYPE_PROVIDER;
}
+
+// Context.consumer的类型就是context的类型
+export function isContextConsumer(ele: any) {
+ return getType(ele) === TYPE_CONTEXT;
+}
diff --git a/scripts/__tests__/HorizonIsTest/index.test.js b/scripts/__tests__/HorizonIsTest/index.test.js
index 5a4b47e1..0fe87e87 100644
--- a/scripts/__tests__/HorizonIsTest/index.test.js
+++ b/scripts/__tests__/HorizonIsTest/index.test.js
@@ -31,8 +31,8 @@ describe('HorizonIs', () => {
});
it('should identify memo component', () => {
- const memo = Horizon.memo(App);
- expect(Horizon.isMemo(memo)).toBe(true);
+ const MemoComp = Horizon.memo(App);
+ expect(Horizon.isMemo()).toBe(true);
});
it('should identify forwardRef', () => {
@@ -54,5 +54,7 @@ describe('HorizonIs', () => {
const TestContext = Horizon.createContext(false);
expect(Horizon.isContextProvider()).toBe(true);
expect(Horizon.isContextProvider()).toBe(false);
+ expect(Horizon.isContextConsumer()).toBe(false);
+ expect(Horizon.isContextConsumer()).toBe(true);
});
});
From 83a64c0a7944fcadaa9fb18c2847cebdfa74657a Mon Sep 17 00:00:00 2001
From: * <8>
Date: Thu, 29 Sep 2022 10:49:52 +0800
Subject: [PATCH 3/3] Match-id-ec8a8f9972bcae8740d613b2cd548e345cc6b188
---
libs/horizon/index.ts | 1 +
1 file changed, 1 insertion(+)
diff --git a/libs/horizon/index.ts b/libs/horizon/index.ts
index 05aa5d6f..eae3c451 100644
--- a/libs/horizon/index.ts
+++ b/libs/horizon/index.ts
@@ -145,6 +145,7 @@ export {
clearStore,
reduxAdapter,
watch,
+ // 兼容ReactIs
isFragment,
isElement,
isForwardRef,