139 lines
4.7 KiB
Smarty
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
defineComponent,
ref,
watch,
computed,
onBeforeUnmount,
onMounted,
} from "vue";
import { loadMicroApp } from "qiankun";
import mergeWith from "lodash/mergeWith";
// eslint-disable-next-line import/extensions
import { getMasterOptions } from "./masterOptions";
function unmountMicroApp(microApp) {
if (microApp && microApp.mountPromise) {
microApp.mountPromise.then(() => microApp.unmount());
}
}
export const MicroApp = defineComponent({
props: {
name: {
type: String,
required: true,
},
},
setup(props) {
const {
masterHistoryType,
apps = [],
lifeCycles: globalLifeCycles,
...globalSettings
} = getMasterOptions();
const {
name,
settings: settingsFromProps = {},
loader,
lifeCycles,
className,
...propsFromParams
} = props;
const containerRef = ref(null);
const microAppRef = ref();
const updatingPromise = ref();
const updatingTimestamp = ref(Date.now());
const getAppConfig = () => {
const appConfig = apps.find((app) => app.name === name);
if (!appConfig) {
throw new Error(
`[@fesjs/plugin-qiankun]: Can not find the configuration of ${name} app!`
);
}
return appConfig;
};
const loadApp = () => {
const appConfig = getAppConfig();
const { name, entry, props: propsFromConfig = {} } = appConfig;
//
microAppRef.value = loadMicroApp(
{
name: name,
entry: entry,
container: containerRef.value,
props: { ...propsFromConfig, ...propsFromParams },
},
{
...globalSettings,
...settingsFromProps,
},
mergeWith({}, globalLifeCycles, lifeCycles, (v1, v2) =>
concat(v1 ?? [], v2 ?? [])
)
);
};
onMounted(() => {
loadApp();
});
onBeforeUnmount(() => {
unmountMicroApp(microAppRef.value);
});
watch(props, () => {
unmountMicroApp(microAppRef.value);
loadApp();
});
watch(microAppRef, () => {
const microApp = microAppRef.value;
const appConfig = getAppConfig();
const { props: propsFromConfig = {} } = appConfig;
if (microApp) {
if (!updatingPromise.value) {
// updatingPromise microApp.mountPromise mount
updatingPromise.value = microApp.mountPromise;
} else {
// microApp.update
updatingPromise.value = updatingPromise.value.then(() => {
const canUpdate = (app) =>
app?.update && app.getStatus() === "MOUNTED";
if (canUpdate(microApp)) {
if (process.env.NODE_ENV === "development") {
if (
Date.now() - updatingTimestamp.value <
200
) {
console.warn(
`[@fesjs/plugin-qiankun] It seems like microApp ${props.name} is updating too many times in a short time(200ms), you may need to do some optimization to avoid the unnecessary re-rendering.`
);
}
console.info(
`[@fesjs/plugin-qiankun] MicroApp ${props.name} is updating with props: `,
props
);
updatingTimestamp.value = Date.now();
}
// microApp.update
return microApp.update({
...propsFromConfig,
...propsFromParams,
});
}
});
}
}
});
return () => <div ref={containerRef} className={className}></div>;
},
});