MCPcopy Create free account
hub / github.com/APIParkLab/APIPark / usePluginLoader

Function usePluginLoader

frontend/packages/common/src/hooks/pluginLoader.ts:341–529  ·  view source on GitHub ↗
(apipark: ApiparkPluginDriverType, routerMap: Map<string, RouterMapConfig>)

Source from the content-addressed store, hash-verified

339}
340
341const 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?.({

Callers 1

RenderRoutesFunction · 0.85

Calls 5

useFetchFunction · 0.90
useGlobalContextFunction · 0.90
usePluginEventHubFunction · 0.90
usePluginSlotHubFunction · 0.90
loadExecutedPluginFunction · 0.85

Tested by

no test coverage detected