Match-id-4b13b18c30c711bb1f976790916f8e43047f285b

This commit is contained in:
* 2022-09-28 16:39:59 +08:00 committed by *
parent 853adf82da
commit ccbf2b44c7
3 changed files with 148 additions and 1 deletions

View File

@ -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;

67
libs/horizon/src/external/HorizonIs.ts vendored Normal file
View File

@ -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;
}

View File

@ -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(<div />)).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(<TestContext.Provider />)).toBe(true);
expect(Horizon.isElement(<TestContext.Consumer />)).toBe(true);
expect(Horizon.isElement(<></>)).toBe(true);
expect(Horizon.isElement(<Horizon.Suspense />)).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(<ForwardRefComp />)).toBe(true);
});
it('should identify lazy', () => {
const LazyComp = Horizon.lazy(() => App);
expect(Horizon.isLazy(<LazyComp />)).toBe(true);
});
it('should identify portal', () => {
const portal = Horizon.createPortal(<div />, container);
expect(Horizon.isPortal(portal)).toBe(true);
});
it('should identify ContextProvider', () => {
const TestContext = Horizon.createContext(false);
expect(Horizon.isContextProvider(<TestContext.Provider />)).toBe(true);
expect(Horizon.isContextProvider(<TestContext.Consumer />)).toBe(false);
});
});