From ccbf2b44c725a5ea2d370c65a2a7d834ec694bca Mon Sep 17 00:00:00 2001 From: * <8> Date: Wed, 28 Sep 2022 16:39:59 +0800 Subject: [PATCH 01/33] 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(