forked from NeuralNomadsAI/CodeNomad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-manifest.ts
More file actions
74 lines (64 loc) · 2.28 KB
/
Copy pathplugin-manifest.ts
File metadata and controls
74 lines (64 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import type { RightPanelModule, RightPanelSectionModule, RightPanelTabModule } from "./registry"
export type RightPanelPluginCleanup = () => void
export interface RightPanelPluginContext {
instanceId: string
}
export interface RightPanelPluginLifecycle {
onLoad?: (context: RightPanelPluginContext) => void | RightPanelPluginCleanup
onUnload?: (context: RightPanelPluginContext) => void
}
export interface RightPanelPluginManifest {
id: string
tabs?: readonly RightPanelTabModule[]
statusSections?: readonly RightPanelSectionModule[]
lifecycle?: RightPanelPluginLifecycle
}
export interface RightPanelPluginLoadError {
pluginId: string
phase: "load" | "unload"
error: unknown
}
export interface LoadedRightPanelPlugins {
modules: RightPanelModule[]
errors: RightPanelPluginLoadError[]
unload: () => RightPanelPluginLoadError[]
}
export function loadRightPanelPluginManifests(
manifests: readonly RightPanelPluginManifest[],
context: RightPanelPluginContext,
): LoadedRightPanelPlugins {
const modules: RightPanelModule[] = []
const cleanupStack: { manifest: RightPanelPluginManifest; cleanup?: RightPanelPluginCleanup }[] = []
const errors: RightPanelPluginLoadError[] = []
const seen = new Set<string>()
for (const manifest of manifests) {
if (!manifest.id || seen.has(manifest.id)) {
errors.push({ pluginId: manifest.id || "<missing>", phase: "load", error: new Error("Duplicate or missing right panel plugin id") })
continue
}
seen.add(manifest.id)
try {
const cleanup = manifest.lifecycle?.onLoad?.(context)
modules.push({ id: manifest.id, tabs: manifest.tabs, statusSections: manifest.statusSections })
cleanupStack.push({ manifest, cleanup: typeof cleanup === "function" ? cleanup : undefined })
} catch (error) {
errors.push({ pluginId: manifest.id, phase: "load", error })
}
}
return {
modules,
errors,
unload: () => {
const unloadErrors: RightPanelPluginLoadError[] = []
for (const { manifest, cleanup } of cleanupStack.slice().reverse()) {
try {
cleanup?.()
manifest.lifecycle?.onUnload?.(context)
} catch (error) {
unloadErrors.push({ pluginId: manifest.id, phase: "unload", error })
}
}
return unloadErrors
},
}
}