import App from './app.vue' import { appEl, SceneTypeDesc, scenes } from '@/store' import { mount, deepIsRevise } from '@/utils' import { reactive, ref } from 'vue' import type { Scene } from '@/store' import type { ModelExpose } from './platform' export type FuseModelType = typeof fuseModel export type SceneModelType = Pick export type ModelType = FuseModelType | SceneModelType export type ModelProps = { type: ModelType, callback: ((expose?: ModelExpose, err?: Error) => void) | null } export type { ModelExpose } export const fuseModel = Symbol('fuse') export const currentModel = ref(fuseModel) export const modelProps: ModelProps = reactive({ type: currentModel, callback: null }) export const getModelTypeDesc = (model: ModelType) => { if (model === fuseModel) { return '融合场景' } else { return SceneTypeDesc[model.type] } } export const getModelDesc = (model: ModelType) => { console.log(model) if (model === fuseModel) { return '融合场景' } else { return scenes.value.find(scene => scene.num === model.num && scene.type === model.type)?.name } } const _loadModel = (() => { let oldModelType: ModelType let oldResult: Promise return (modelType: ModelType) => { if (!deepIsRevise(oldModelType, modelType)) { return oldResult } oldModelType = modelType return oldResult = new Promise((resolve, reject) => { modelProps.callback = (data: any, err) => { if (err) { reject(err) } else { resolve(data) } } currentModel.value = modelType }) } })(); let isInitial = false export const loadModel = async (modelType: ModelType): Promise => { // 给 vue 加载时间,防止一进入就直接加载模型导致 vue 尚未初始化 if (!isInitial) { await new Promise((resolve) => setTimeout(resolve, 10)); } const modelPromise = _loadModel(modelType) if (!isInitial) { if (!appEl.value) { throw new Error('appEl 未初始化') } else { mount(appEl.value, App) isInitial = true } } return modelPromise }