fix: 修复乾坤插件内存问题

This commit is contained in:
wanchun 2022-06-06 15:10:19 +08:00
parent d37d6b480b
commit c9d957bac1
8 changed files with 35 additions and 44 deletions

View File

@ -1,6 +1,7 @@
<template> <template>
<div class="haizekuo"> <div class="haizekuo">
app1 - index app1 - index
<input />
</div> </div>
</template> </template>
<config> <config>

View File

@ -1,6 +1,7 @@
<template> <template>
<div class="haizekuo"> <div class="haizekuo">
app1 - test app1 - test
<input />
</div> </div>
</template> </template>
<config> <config>

View File

@ -12,13 +12,13 @@ import {mergeWith} from "{{{LODASH_ES}}}";
// eslint-disable-next-line import/extensions // eslint-disable-next-line import/extensions
import { getMasterOptions } from "./masterOptions"; import { getMasterOptions } from "./masterOptions";
function unmountMicroApp(microApp) { async function unmountMicroApp(microApp) {
if (!microApp) { if (!microApp) {
return; return;
} }
const status = microApp.getStatus(); const status = microApp.getStatus();
if (status === 'MOUNTED') { if (status === 'MOUNTED') {
microApp.unmount(); await microApp.unmount();
} }
} }
@ -80,7 +80,7 @@ export const MicroApp = defineComponent({
microAppRef.value = loadMicroApp( microAppRef.value = loadMicroApp(
{ {
// 保证唯一 // 保证唯一
name: `${name}_${Date.now()}`, name: `${name}`,
entry: entry, entry: entry,
container: containerRef.value, container: containerRef.value,
props: {...propsConfigRef.value} props: {...propsConfigRef.value}

View File

@ -1,4 +1,4 @@
import { plugin, ApplyPluginsType, getRouter, getHistory, destroyRouter } from '@@/core/coreExports'; import { plugin, ApplyPluginsType } from '@@/core/coreExports';
{{#HAS_PLUGIN_MODEL}} {{#HAS_PLUGIN_MODEL}}
import { setModelState } from './qiankunModel'; import { setModelState } from './qiankunModel';
{{/HAS_PLUGIN_MODEL}} {{/HAS_PLUGIN_MODEL}}
@ -18,6 +18,7 @@ function isPromise(obj) {
let render = () => {}; let render = () => {};
let cacheAppPromise = null; let cacheAppPromise = null;
let hasMountedAtLeastOnce = false; let hasMountedAtLeastOnce = false;
const cache = {};
export default () => defer.promise; export default () => defer.promise;
@ -91,6 +92,11 @@ export function genMount(mountElementId) {
defer.resolve(); defer.resolve();
} }
hasMountedAtLeastOnce = true; hasMountedAtLeastOnce = true;
cacheAppPromise.then((app)=>{
if(!cache[props.name]) {
cache[props.name] = app;
}
})
}; };
} }
@ -109,9 +115,15 @@ export function genUpdate() {
// 子应用生命周期钩子Unmount // 子应用生命周期钩子Unmount
export function genUnmount() { export function genUnmount() {
return async (props) => { return async (props) => {
const history = getHistory(); if (cache[props.name]) {
history.destroy(); // 会触发app.unmount setTimeout(() => {
destroyRouter(); const app = cache[props.name];
app.unmount();
app._container.innerHTML = '';
delete cache[props.name];
cacheAppPromise = null;
}, 0);
}
const slaveRuntime = getSlaveRuntime(); const slaveRuntime = getSlaveRuntime();
if (slaveRuntime.unmount) { if (slaveRuntime.unmount) {
await slaveRuntime.unmount(props); await slaveRuntime.unmount(props);

View File

@ -1,5 +1,5 @@
import { createMemoryHistory, getHistory } from '@@/core/coreExports'; import { createMemoryHistory } from '@@/core/coreExports';
import qiankunRender, { clientRenderOptsStack, history } from './lifecycles'; import qiankunRender, { clientRenderOptsStack, history as historyConfig } from './lifecycles';
export const render = oldRender => qiankunRender().then(oldRender); export const render = oldRender => qiankunRender().then(oldRender);
@ -15,19 +15,18 @@ export function modifyClientRenderOpts(memo) {
} }
export function modifyCreateHistory(memo) { export function modifyCreateHistory(memo) {
if (history.url) { if (historyConfig.url) {
return createMemoryHistory return createMemoryHistory
} }
return memo; return memo;
} }
export function onRouterCreated({ router }) { export function onRouterCreated({ router, history }) {
if(history.url) { if(historyConfig.url) {
const memoryHistory = getHistory(); history.push(historyConfig.url)
memoryHistory.push(history.url)
} }
if(history.onRouterInit){ if(historyConfig.onRouterInit){
history.onRouterInit(router) historyConfig.onRouterInit(router)
} }
} }

View File

@ -61,6 +61,7 @@ const beforeRender = async ({rootElement}) => {
console.error(e); console.error(e);
} }
app.unmount(); app.unmount();
app._container.innerHTML = '';
} }
return initialState; return initialState;
}; };

View File

@ -328,7 +328,7 @@ export default function (api) {
api.addCoreExports(() => [ api.addCoreExports(() => [
{ {
specifiers: ['getRoutes', 'getRouter', 'getHistory', 'destroyRouter', 'defineRouteMeta'], specifiers: ['getRoutes', 'defineRouteMeta'],
source: absCoreFilePath source: absCoreFilePath
} }
]); ]);

View File

@ -7,12 +7,8 @@ export function getRoutes() {
} }
const ROUTER_BASE = '{{{ routerBase }}}'; const ROUTER_BASE = '{{{ routerBase }}}';
let router = null;
let history = null;
export const createRouter = (routes) => { export const createRouter = (routes) => {
if (router) {
return router;
}
const createHistory = plugin.applyPlugins({ const createHistory = plugin.applyPlugins({
key: 'modifyCreateHistory', key: 'modifyCreateHistory',
type: ApplyPluginsType.modify, type: ApplyPluginsType.modify,
@ -21,14 +17,14 @@ export const createRouter = (routes) => {
}, },
initialValue: {{{ CREATE_HISTORY }}}, initialValue: {{{ CREATE_HISTORY }}},
}); });
history = createHistory(ROUTER_BASE); const history = createHistory(ROUTER_BASE);
// 修改routes // 修改routes
plugin.applyPlugins({ plugin.applyPlugins({
key: 'patchRoutes', key: 'patchRoutes',
type: ApplyPluginsType.event, type: ApplyPluginsType.event,
args: { routes }, args: { routes },
}); });
router = createVueRouter({ const router = createVueRouter({
history, history,
routes routes
}); });
@ -36,31 +32,12 @@ export const createRouter = (routes) => {
plugin.applyPlugins({ plugin.applyPlugins({
key: 'onRouterCreated', key: 'onRouterCreated',
type: ApplyPluginsType.event, type: ApplyPluginsType.event,
args: { router }, args: { router, history },
}); });
return router; return router;
}; };
export const getRouter = ()=>{
if(!router){
console.warn(`[preset-build-in] router is null`)
}
return router;
}
export const getHistory = ()=>{
if(!history){
console.warn(`[preset-build-in] history is null`)
}
return history;
}
export const destroyRouter = ()=>{
router = null;
history = null;
}
export const defineRouteMeta = (param)=>{ export const defineRouteMeta = (param)=>{
return param return param
} }