import {getComponentCls, getCurrentDesignInstance, markRaw, onBeforeUnmount, Portal, reactive, useRefList} from "@peryl/react-compose"; import {defer, DFD} from '@peryl/utils/defer'; import {createCounter} from "@peryl/utils/createCounter"; import {APPLICATION_SERVICE_CONTAINER_CLASS, ApplicationHooks} from "../utils/application.utils"; import {$previewer} from "../../$previewer"; /** * applications渲染manager数组的时候,给manager传递的 getManagerData函数的返回值类型 * @author 韦胜健 * @date 2022.5.4 12:16 */ export interface iApplicationManagerData { name: string, ManagerComponent: any } /** * iApplicationHolder数据对象类型 * @author 韦胜健 * @date 2022.5.4 12:25 */ export interface iApplicationHolderData { application: iApplicationInstance, root: any } /** * application组件实例的暴露类型 * @author 韦胜健 * @date 2022.5.4 12:17 */ export interface iApplicationInstance { getManager: (name: string, ManagerComponent: ManagerComponent) => Promise; } /** * application实例管理对象 * @author 韦胜健 * @date 2022.5.4 12:17 */ export const ApplicationHolder = (() => { let applications: iApplicationHolderData[] = []; const remove = (application: iApplicationInstance, root: any) => { const index = applications.findIndex(i => i.application === application && i.root === root); if (index > -1) { applications.splice(index, 1); } }; const add = (application: iApplicationInstance, root: any) => { const index = applications.findIndex(i => i.application === application && i.root === root); if (index === -1) { applications.push({ application, root }); } return {}; }; const useRegistryApplication = (ins: iApplicationInstance) => { const { $root } = getCurrentDesignInstance()!.proxy!; add(ins, $root); onBeforeUnmount(() => remove(ins, $root)); }; return { applications, useRegistryApplication }; })(); export const nextManagerId = createCounter('application_manager'); export function useApplicationService() { /*manager代理对象引用*/ let { refList, onRefList } = useRefList<{ props: { getManagerData: () => iApplicationManagerData } }>(); /*当前状态*/ const state = reactive({ managers: [] as { id: string, dfd: DFD, getManagerData: () => iApplicationManagerData, RenderComponent: any, }[], }); const refer: iApplicationInstance = { getManager: async (name, ManagerComponent) => { let item = state.managers.find(i => { const { name: _name, ManagerComponent: _ManagerComponent } = i.getManagerData(); return _name === name && _ManagerComponent === ManagerComponent; }); if (!item) { item = { id: nextManagerId(), getManagerData: () => ({ name, ManagerComponent }), RenderComponent: markRaw(ManagerComponent), dfd: defer(), }; /*当前引用中没有该实例,手动创建一个*/ state.managers.push(item); } return item.dfd.promise; }, }; ApplicationHolder.useRegistryApplication(refer); onBeforeUnmount( ApplicationHooks.onRender.use({ processor: ({ list, render }) => {list.push({ render, key: 'application-service', seq: 1 });}, render: () => (
{$previewer.render()} {state.managers.map(({ getManagerData, RenderComponent, dfd, id }, index) => { return ( dfd.resolve(refList[index])} /> ); })}
) }) ); return { refer }; }