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>
<div class="haizekuo">
app1 - index
<input />
</div>
</template>
<config>

View File

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

View File

@ -12,13 +12,13 @@ import {mergeWith} from "{{{LODASH_ES}}}";
// eslint-disable-next-line import/extensions
import { getMasterOptions } from "./masterOptions";
function unmountMicroApp(microApp) {
async function unmountMicroApp(microApp) {
if (!microApp) {
return;
}
const status = microApp.getStatus();
if (status === 'MOUNTED') {
microApp.unmount();
await microApp.unmount();
}
}
@ -80,7 +80,7 @@ export const MicroApp = defineComponent({
microAppRef.value = loadMicroApp(
{
// 保证唯一
name: `${name}_${Date.now()}`,
name: `${name}`,
entry: entry,
container: containerRef.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}}
import { setModelState } from './qiankunModel';
{{/HAS_PLUGIN_MODEL}}
@ -18,6 +18,7 @@ function isPromise(obj) {
let render = () => {};
let cacheAppPromise = null;
let hasMountedAtLeastOnce = false;
const cache = {};
export default () => defer.promise;
@ -91,6 +92,11 @@ export function genMount(mountElementId) {
defer.resolve();
}
hasMountedAtLeastOnce = true;
cacheAppPromise.then((app)=>{
if(!cache[props.name]) {
cache[props.name] = app;
}
})
};
}
@ -109,9 +115,15 @@ export function genUpdate() {
// 子应用生命周期钩子Unmount
export function genUnmount() {
return async (props) => {
const history = getHistory();
history.destroy(); // 会触发app.unmount
destroyRouter();
if (cache[props.name]) {
setTimeout(() => {
const app = cache[props.name];
app.unmount();
app._container.innerHTML = '';
delete cache[props.name];
cacheAppPromise = null;
}, 0);
}
const slaveRuntime = getSlaveRuntime();
if (slaveRuntime.unmount) {
await slaveRuntime.unmount(props);

View File

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

View File

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

View File

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

View File

@ -7,12 +7,8 @@ export function getRoutes() {
}
const ROUTER_BASE = '{{{ routerBase }}}';
let router = null;
let history = null;
export const createRouter = (routes) => {
if (router) {
return router;
}
const createHistory = plugin.applyPlugins({
key: 'modifyCreateHistory',
type: ApplyPluginsType.modify,
@ -21,14 +17,14 @@ export const createRouter = (routes) => {
},
initialValue: {{{ CREATE_HISTORY }}},
});
history = createHistory(ROUTER_BASE);
const history = createHistory(ROUTER_BASE);
// 修改routes
plugin.applyPlugins({
key: 'patchRoutes',
type: ApplyPluginsType.event,
args: { routes },
});
router = createVueRouter({
const router = createVueRouter({
history,
routes
});
@ -36,31 +32,12 @@ export const createRouter = (routes) => {
plugin.applyPlugins({
key: 'onRouterCreated',
type: ApplyPluginsType.event,
args: { router },
args: { router, history },
});
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)=>{
return param
}