Skip to content

Commit

Permalink
prune bundle unit input data specs
Browse files Browse the repository at this point in the history
  • Loading branch information
io-sammt authored and samuelmtimbo committed Oct 11, 2024
1 parent f2505d7 commit 602b049
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/Primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ function _bundleUnit(
}

_bundleUnit(bundle.unit, specs, custom, branch)

if (bundle.specs) {
delete bundle.specs
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/spec/bundleClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Unit = any>(
Class: UnitClass<T>,
Expand All @@ -18,7 +19,11 @@ export function bundleClass<T extends Unit = any>(
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)

Expand Down
73 changes: 4 additions & 69 deletions src/spec/fromSpec.ts
Original file line number Diff line number Diff line change
@@ -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<I extends Dict<any> = any, O extends Dict<any> = any>(
spec: GraphSpec,
Expand All @@ -88,11 +25,9 @@ export function fromSpec<I extends Dict<any> = any, O extends Dict<any> = 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
}
Expand Down Expand Up @@ -156,7 +91,7 @@ export function classFromSpec<I, O>(

class Class extends Graph<I, O> {
constructor(system: System) {
const spec = system.getSpec(id) as GraphSpec
const spec = specs[id] as GraphSpec

super(spec, branch, system, id)
}
Expand Down
3 changes: 1 addition & 2 deletions src/test/spec/createClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)

Expand Down
3 changes: 1 addition & 2 deletions src/test/system/core/RandomHEXColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -68,7 +67,7 @@ const spec = {
},
} as GraphSpec

const RandomHEXColor = fromSpec(spec, _specs)
const RandomHEXColor = fromSpec(spec, system.specs)

const randomHEXColor = new RandomHEXColor(system)

Expand Down

0 comments on commit 602b049

Please sign in to comment.