(apipark: ApiparkPluginDriverType, routerMap: Map<string, RouterMapConfig>)
| 339 | } |
| 340 | |
| 341 | const usePluginLoader = (apipark: ApiparkPluginDriverType, routerMap: Map<string, RouterMapConfig>) => { |
| 342 | const [modules, setModules] = useState(new Map()) |
| 343 | const [executeList, setExecuteList] = useState<ExecutePluginType[]>([]) |
| 344 | const [baseHref, setBaseHref] = useState('') |
| 345 | const [pendingTasks, setPendingTasks] = useState(0) |
| 346 | const { fetchData } = useFetch() |
| 347 | const pluginProvider = useGlobalContext() |
| 348 | const pluginEventHub = usePluginEventHub() |
| 349 | const pluginSlotHub = usePluginSlotHub() |
| 350 | const { getMenuList, dispatch } = pluginProvider |
| 351 | const { modal, message } = App.useApp() |
| 352 | const [startLoadExecutePlugin, setStartLoadExecutePlugin] = useState<boolean>(false) |
| 353 | const messageService = message |
| 354 | const modalService = modal |
| 355 | let startInstallPlugin = false |
| 356 | |
| 357 | useEffect(() => { |
| 358 | if (startLoadExecutePlugin && pendingTasks === 0 && executeList.length > 0) { |
| 359 | loadExecutedPlugin() |
| 360 | } |
| 361 | }, [pendingTasks, executeList]) |
| 362 | |
| 363 | const getModule = (routerPrefix: string, specific = false) => { |
| 364 | if (routerPrefix.startsWith('/')) { |
| 365 | routerPrefix = routerPrefix.substring(1) |
| 366 | } |
| 367 | if (specific) { |
| 368 | return modules.get(routerPrefix) |
| 369 | } |
| 370 | let matchedModule = null |
| 371 | let matchedLength = 0 |
| 372 | |
| 373 | modules.forEach((value, key) => { |
| 374 | if (routerPrefix.startsWith(key) && key.length > matchedLength) { |
| 375 | matchedModule = value |
| 376 | matchedLength = key.length |
| 377 | } |
| 378 | }) |
| 379 | return matchedModule |
| 380 | } |
| 381 | |
| 382 | const loadModule = async (routerPrefix: string, pluginName: any, exposedModule: string, pluginPath: any) => { |
| 383 | if (!modules.get(routerPrefix)) { |
| 384 | try { |
| 385 | const loadedModule = await loadRemoteModule(generateRemoteModuleTemplate(pluginName, exposedModule, pluginPath)) |
| 386 | const Module = loadedModule.default ?? loadedModule |
| 387 | let ModuleBootstrap |
| 388 | try { |
| 389 | ModuleBootstrap = await loadRemoteModule(generateRemoteModuleTemplate(pluginName, 'Bootstrap', pluginPath)) |
| 390 | } catch (error) { |
| 391 | console.warn('Bootstrap module not found:', error) |
| 392 | } |
| 393 | setModules((prevModules) => new Map(prevModules).set(routerPrefix, Module[exposedModule])) |
| 394 | if (!validateExportLifecycle(Module)) { |
| 395 | console.error('需要导出插件生命周期函数') |
| 396 | return |
| 397 | } |
| 398 | await Module.bootstrap?.({ |
no test coverage detected