|
| 1 | +/** |
| 2 | + * Helpers for tracking which runtime toolboxes a graph depends on. |
| 3 | + * |
| 4 | + * - `collectRequiredToolboxes`: scans a node tree, finds all non-builtin |
| 5 | + * block types, and returns the matching `ToolboxRequirement` entries from |
| 6 | + * the active toolbox store. Used when saving a file. |
| 7 | + * - `findMissingRequirements`: filters a list of requirements down to the |
| 8 | + * ones not currently installed. Used when loading a file. |
| 9 | + */ |
| 10 | + |
| 11 | +import { get } from 'svelte/store'; |
| 12 | +import { nodeRegistry, BUILTIN_SOURCE } from '$lib/nodes/registry'; |
| 13 | +import { NODE_TYPES } from '$lib/constants/nodeTypes'; |
| 14 | +import type { NodeInstance } from '$lib/types/nodes'; |
| 15 | +import type { ToolboxRequirement } from '$lib/types/schema'; |
| 16 | +import { toolboxes } from './store'; |
| 17 | +import type { ToolboxConfig } from './types'; |
| 18 | + |
| 19 | +function walkNodeTypes(nodes: NodeInstance[], out: Set<string>): void { |
| 20 | + for (const node of nodes) { |
| 21 | + if (node.type !== NODE_TYPES.SUBSYSTEM && node.type !== NODE_TYPES.INTERFACE) { |
| 22 | + out.add(node.type); |
| 23 | + } |
| 24 | + if (node.graph?.nodes) walkNodeTypes(node.graph.nodes, out); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function toRequirement(t: ToolboxConfig): ToolboxRequirement { |
| 29 | + return { |
| 30 | + id: t.id, |
| 31 | + displayName: t.displayName, |
| 32 | + source: t.source, |
| 33 | + importPath: t.importPath, |
| 34 | + eventsImportPath: t.eventsImportPath |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Walk a node tree, find all block types whose registry source is a |
| 40 | + * runtime toolbox, and return the matching requirement records. |
| 41 | + */ |
| 42 | +export function collectRequiredToolboxes(nodes: NodeInstance[]): ToolboxRequirement[] { |
| 43 | + const seenTypes = new Set<string>(); |
| 44 | + walkNodeTypes(nodes, seenTypes); |
| 45 | + |
| 46 | + const sourceIds = new Set<string>(); |
| 47 | + for (const type of seenTypes) { |
| 48 | + const src = nodeRegistry.getSource(type); |
| 49 | + if (src && src !== BUILTIN_SOURCE) sourceIds.add(src); |
| 50 | + } |
| 51 | + |
| 52 | + const installed = get(toolboxes); |
| 53 | + const result: ToolboxRequirement[] = []; |
| 54 | + for (const id of sourceIds) { |
| 55 | + const t = installed.find((tb) => tb.id === id); |
| 56 | + if (t) result.push(toRequirement(t)); |
| 57 | + } |
| 58 | + return result; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Filter a list of toolbox requirements to those that are NOT currently |
| 63 | + * installed. Used at load time to figure out what to prompt for. |
| 64 | + */ |
| 65 | +export function findMissingRequirements(reqs: ToolboxRequirement[]): ToolboxRequirement[] { |
| 66 | + if (!reqs || reqs.length === 0) return []; |
| 67 | + const installed = new Set(get(toolboxes).map((t) => t.id)); |
| 68 | + return reqs.filter((r) => !installed.has(r.id)); |
| 69 | +} |
0 commit comments