2025-07-21 14:01:21 +07:00
|
|
|
import { getPluginDependencies, pMetadata } from '../_internal'
|
2025-07-20 23:06:56 +07:00
|
|
|
import type { AnyPlugin } from '../_internal'
|
|
|
|
|
|
|
|
|
|
/// PLUGIN DEPENDENCY GRAPHING
|
|
|
|
|
|
|
|
|
|
// We don't store the graph as a tree, but rather as a set of nodes.
|
|
|
|
|
|
|
|
|
|
// Root nodes are plugins that have dependencies, but no dependents. These plugins are the starting points of the dependency graph.
|
|
|
|
|
// Leaf nodes are plugins that have no dependencies, but may have dependents. These plugins are the end points of the dependency graph.
|
|
|
|
|
|
|
|
|
|
// Start order: Single nodes (no dependencies & dependents) -> Leaf nodes (no dependencies, maybe dependents) -> Root nodes (with dependencies, no dependents)
|
|
|
|
|
// This way we can ensure that all dependencies are started before the plugin itself.
|
|
|
|
|
|
|
|
|
|
export const pRootNodes = new Set<AnyPlugin>()
|
|
|
|
|
export const pLeafOrSingleNodes = new Set<AnyPlugin>()
|
|
|
|
|
|
2025-07-21 14:01:21 +07:00
|
|
|
// Visited non-leaf nodes
|
2025-07-20 23:34:31 +07:00
|
|
|
const visited = new WeakSet<AnyPlugin>()
|
|
|
|
|
|
2025-07-20 23:06:56 +07:00
|
|
|
// Ordered list of plugins to be started
|
2025-07-20 23:34:31 +07:00
|
|
|
export const pListOrdered: AnyPlugin[] = []
|
2025-07-20 23:06:56 +07:00
|
|
|
// Pending plugins to be computed
|
|
|
|
|
export const pPending = new Set<AnyPlugin>()
|
|
|
|
|
|
|
|
|
|
export function computePendingNodes() {
|
2025-07-21 14:01:21 +07:00
|
|
|
for (const plugin of pLeafOrSingleNodes) pListOrdered.unshift(plugin)
|
|
|
|
|
pLeafOrSingleNodes.clear()
|
|
|
|
|
|
2025-07-20 23:06:56 +07:00
|
|
|
for (const plugin of pPending) resolvePluginGraph(plugin)
|
|
|
|
|
pPending.clear()
|
|
|
|
|
|
|
|
|
|
const stack = [...pRootNodes]
|
|
|
|
|
pRootNodes.clear()
|
|
|
|
|
|
|
|
|
|
while (stack.length) {
|
|
|
|
|
const plugin = stack.pop()!
|
2025-07-20 23:34:31 +07:00
|
|
|
if (visited.has(plugin)) continue
|
2025-07-20 23:06:56 +07:00
|
|
|
|
2025-07-21 14:01:21 +07:00
|
|
|
if (plugin.manifest.dependencies?.length)
|
|
|
|
|
for (const dep of getPluginDependencies(plugin)) stack.push(dep)
|
|
|
|
|
else {
|
2025-07-20 23:34:31 +07:00
|
|
|
pListOrdered.push(plugin)
|
|
|
|
|
visited.add(plugin)
|
|
|
|
|
}
|
2025-07-20 23:06:56 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function resolvePluginGraph(plugin: AnyPlugin) {
|
|
|
|
|
const { manifest } = plugin
|
|
|
|
|
|
|
|
|
|
if (manifest.dependencies?.length) {
|
|
|
|
|
// Optimisitically add to root nodes (if there are dependents, it will be removed later)
|
|
|
|
|
pRootNodes.add(plugin)
|
|
|
|
|
|
2025-07-21 14:01:21 +07:00
|
|
|
for (const dep of getPluginDependencies(plugin)) {
|
2025-07-20 23:06:56 +07:00
|
|
|
const depMeta = pMetadata.get(dep)!
|
|
|
|
|
depMeta.dependents.push(plugin)
|
|
|
|
|
|
|
|
|
|
// Not a root node if it has dependencies
|
|
|
|
|
if (dep.manifest.dependencies?.length) pRootNodes.delete(dep)
|
|
|
|
|
}
|
|
|
|
|
} else pLeafOrSingleNodes.add(plugin)
|
|
|
|
|
}
|