From 602b0494773517ba9b72c4d9070df187a6444c91 Mon Sep 17 00:00:00 2001 From: Samuel MT Date: Thu, 10 Oct 2024 21:02:43 -0300 Subject: [PATCH] prune bundle unit input data specs --- src/Primitive.ts | 2 +- src/bundle.ts | 4 ++ src/spec/bundleClass.ts | 7 ++- src/spec/fromSpec.ts | 73 ++------------------------ src/test/spec/createClass.ts | 3 +- src/test/system/core/RandomHEXColor.ts | 3 +- 6 files changed, 17 insertions(+), 75 deletions(-) diff --git a/src/Primitive.ts b/src/Primitive.ts index af067644bb..3cdc67d2a2 100644 --- a/src/Primitive.ts +++ b/src/Primitive.ts @@ -89,7 +89,7 @@ export class Primitive< if (event === 'data') { this._activateInput(name as keyof I, data) - + if (ref) { this.__onRefInputData(name as keyof I, data) } else { diff --git a/src/bundle.ts b/src/bundle.ts index 21c486b91f..a43e19cb18 100644 --- a/src/bundle.ts +++ b/src/bundle.ts @@ -116,6 +116,10 @@ function _bundleUnit( } _bundleUnit(bundle.unit, specs, custom, branch) + + if (bundle.specs) { + delete bundle.specs + } } } } diff --git a/src/spec/bundleClass.ts b/src/spec/bundleClass.ts index f247f9923a..9d714e0c69 100644 --- a/src/spec/bundleClass.ts +++ b/src/spec/bundleClass.ts @@ -4,6 +4,7 @@ import { UnitBundle } from '../types/UnitBundle' import { UnitBundleSpec } from '../types/UnitBundleSpec' import { UnitClass } from '../types/UnitClass' import { parseMemorySpec } from './evaluate/parseMemorySpec' +import { remapSpecs } from './remapBundle' export function bundleClass( Class: UnitClass, @@ -18,7 +19,11 @@ export function bundleClass( static __bundle = bundle constructor(system: System) { - bundle.specs && system.injectSpecs(bundle.specs) + if (bundle.specs) { + const map = system.injectSpecs(bundle.specs) + + remapSpecs(bundle, map) + } super(system, id) diff --git a/src/spec/fromSpec.ts b/src/spec/fromSpec.ts index 56ef375754..8d58cf42de 100644 --- a/src/spec/fromSpec.ts +++ b/src/spec/fromSpec.ts @@ -1,78 +1,15 @@ +import { unitBundleSpec } from '../bundle' import { Graph } from '../Class/Graph' import { System } from '../system' import { Classes, PinSpec, Specs } from '../types' import { Dict } from '../types/Dict' import { GraphBundle, GraphClass } from '../types/GraphClass' import { GraphSpec } from '../types/GraphSpec' -import { GraphSpecs } from '../types/GraphSpecs' import { GraphUnitPinSpec } from '../types/GraphUnitPinSpec' import { GraphUnitSpec } from '../types/GraphUnitSpec' import { io } from '../types/IOOf' -import { deepGet } from '../util/object' import { weakMerge } from '../weakMerge' import { bundleClass } from './bundleClass' -import { evaluateDataValue } from './evaluateDataValue' - -export function extractGraphSpecs( - spec: GraphSpec, - specs: Specs, - graphs: GraphSpecs = {}, - classes: Classes = {} -): GraphSpecs { - graphs[spec.id] = spec - - const { units } = spec - - for (const unit_id in units) { - const unit = units[unit_id] - - const { id, input } = unit - - for (const inputId in input) { - const _input = input[inputId] ?? {} - - const { data } = _input - - if (data !== undefined) { - const dataRef = evaluateDataValue(data, specs, classes) - - for (const path of dataRef.ref ?? []) { - const bundle = deepGet(dataRef.data, path) - - for (const specId in bundle.specs) { - const spec = bundle.specs[specId] - - graphs[specId] = spec - } - - for (const specId in bundle.specs) { - const spec = bundle.specs[specId] - - extractGraphSpecs(spec, weakMerge(specs, graphs), graphs) - } - } - } - } - - const unit_spec = specs[id] - - if (!unit_spec) { - return - } - - const { system } = unit_spec - - if (!system) { - if (!graphs[id]) { - graphs[id] = unit_spec as GraphSpec - - extractGraphSpecs(graphs[id], specs, graphs) - } - } - } - - return graphs -} export function fromSpec = any, O extends Dict = any>( spec: GraphSpec, @@ -88,11 +25,9 @@ export function fromSpec = any, O extends Dict = any>( throw new Error('spec id is required') } - const specs = extractGraphSpecs(spec, specs_, {}) - - const unit = { id } + const bundle = unitBundleSpec({ id }, weakMerge({ [id]: spec }, specs_)) - const Bundle = bundleClass(Class, { unit, specs }) + const Bundle = bundleClass(Class, bundle) return Bundle } @@ -156,7 +91,7 @@ export function classFromSpec( class Class extends Graph { constructor(system: System) { - const spec = system.getSpec(id) as GraphSpec + const spec = specs[id] as GraphSpec super(spec, branch, system, id) } diff --git a/src/test/spec/createClass.ts b/src/test/spec/createClass.ts index a9a8795352..e6e26f8e46 100644 --- a/src/test/spec/createClass.ts +++ b/src/test/spec/createClass.ts @@ -2,7 +2,6 @@ import * as assert from 'assert' import { UNTITLED } from '../../constant/STRING' import { watchGraphAndLog } from '../../debug' import { fromSpec } from '../../spec/fromSpec' -import _specs from '../../system/_specs' import { system } from '../util/system' const spec = system.newSpec({ @@ -25,7 +24,7 @@ const spec = system.newSpec({ outputs: {}, }) -const Class = fromSpec(spec, _specs, system.classes) +const Class = fromSpec(spec, system.specs, system.classes) const composition = new Class(system) diff --git a/src/test/system/core/RandomHEXColor.ts b/src/test/system/core/RandomHEXColor.ts index 376c226662..40dbe723bc 100644 --- a/src/test/system/core/RandomHEXColor.ts +++ b/src/test/system/core/RandomHEXColor.ts @@ -2,7 +2,6 @@ import * as assert from 'assert' import { Graph } from '../../../Class/Graph' import { watchGraphAndLog, watchUnitAndLog } from '../../../debug' import { fromSpec } from '../../../spec/fromSpec' -import _specs from '../../../system/_specs' import { GraphSpec } from '../../../types/GraphSpec' import { system } from '../../util/system' @@ -68,7 +67,7 @@ const spec = { }, } as GraphSpec -const RandomHEXColor = fromSpec(spec, _specs) +const RandomHEXColor = fromSpec(spec, system.specs) const randomHEXColor = new RandomHEXColor(system)