Skip to content

Commit

Permalink
system refactor/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmtimbo committed Dec 1, 2024
1 parent e7db545 commit b2aa028
Show file tree
Hide file tree
Showing 32 changed files with 198 additions and 242 deletions.
28 changes: 11 additions & 17 deletions src/Class/Graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,19 +296,19 @@ export class Graph<I extends Dict<any> = any, O extends Dict<any> = any>

this._element = render

this._initAddUnits(units)
this._initMerges(merges)
this._initInputSets(inputs)
this._initOutputSets(outputs)
this._initSetComponent(component)

this.addListener('reset', this._reset)
this.addListener('play', this._play)
this.addListener('pause', this._pause)
this.addListener('destroy', this._destroy)
this.addListener('take_err', this._takeErr)
this.addListener('take_caught_err', this._takeErr)

this._initAddUnits(units)
this._initMerges(merges)
this._initInputSets(inputs)
this._initOutputSets(outputs)
this._initSetComponent(component)

const { global } = system

deepSet_(global.graph, [this.id, this.__global_id], this)
Expand Down Expand Up @@ -3593,12 +3593,6 @@ export class Graph<I extends Dict<any> = any, O extends Dict<any> = any>
return unit
}

public addUnitBundle = (unitId: string, unitBundle: UnitBundle) => {
this._addUnitBundle(unitId, unitBundle)
}

public _addUnitBundle = (unitId: string, unitBundle: UnitBundle) => {}

public _addUnitBundleSpec(unitId: string, bundle: UnitBundleSpec): Unit {
// console.log('Graph', '_addUnitBundleSpec', unitId, bundle)

Expand Down Expand Up @@ -3844,7 +3838,7 @@ export class Graph<I extends Dict<any> = any, O extends Dict<any> = any>
if (
path.length > 0 ||
this._destroying ||
this._removingUnit.has(unitId)
(this._removingUnit && this._removingUnit.has(unitId))
) {
bubble()

Expand Down Expand Up @@ -4514,17 +4508,17 @@ export class Graph<I extends Dict<any> = any, O extends Dict<any> = any>
private _createMerge = (mergeId: string): Merge => {
const merge = new Merge(this.__system)

merge.pause()
// merge.pause()

const mergeInputPinId = getMergePinNodeId(mergeId, 'input')

const mergeInputPin = new Pin({}, this.__system)

merge.addInput(mergeInputPinId, mergeInputPin)

if (!this._paused) {
merge.play()
}
// if (!this._paused) {
// merge.play()
// }

return merge
}
Expand Down
2 changes: 2 additions & 0 deletions src/Class/Unit/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { IO } from '../../types/IO'

export type UnitGetGlobalIdData = {}

export type UnitDestroyData = {}

export type UnitPlayData = {}

export type UnitPauseData = {}
Expand Down
2 changes: 1 addition & 1 deletion src/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class Object_<T extends Dict<any> = Dict<any>> implements J<T>, V<T> {
this._set_path([name] as string[], data)
}

public async hasKey<K extends keyof T>(name: K): Promise<boolean> {
public async hasKey(name: string): Promise<boolean> {
return this._obj[name] !== undefined
}

Expand Down
8 changes: 5 additions & 3 deletions src/Pin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { $, $Events } from './Class/$'
import { Unit } from './Class/Unit'
import { System } from './system'
import { PI } from './types/interface/PI'
import { V } from './types/interface/V'
Expand Down Expand Up @@ -141,12 +142,13 @@ export class Pin<T = any> extends $<PinEvents<T>> implements V<T>, PI<T> {
this._embodied = true

data = new data(this.__system)

data.play()
data.register()
}

if (data instanceof $) {
if (data.__.includes('U')) {
;(data as Unit).play()
}

data.register()

this._unlisten = data.addListener('edit', () => {
Expand Down
12 changes: 5 additions & 7 deletions src/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Registry implements R {
specs: Specs
specs_: Object_<Specs>
specsCount: Dict<number>
specsLock: Dict<boolean>
lock: Dict<boolean>

constructor(
specs: Specs,
Expand All @@ -26,7 +26,7 @@ export class Registry implements R {
this.specs = specs
this.specs_ = specs_ ?? new Object_(specs)
this.specsCount = specsCount ?? {}
this.specsLock = {}
this.lock = {}
}

newSpecId(): string {
Expand Down Expand Up @@ -235,9 +235,7 @@ export class Registry implements R {
if (this.specsCount[id] === 0) {
delete this.specsCount[id]

const spec = this.specs[id]

if (!isSystemSpecId(this.specs, id) && !this.specsLock[id]) {
if (!isSystemSpecId(this.specs, id) && !this.lock[id]) {
this.deleteSpec(id)
}
}
Expand All @@ -250,10 +248,10 @@ export class Registry implements R {
}

lockSpec(id: string): void {
this.specsLock[id] = true
this.lock[id] = true
}

unlockSpec(id: string): void {
delete this.specsLock[id]
delete this.lock[id]
}
}
38 changes: 8 additions & 30 deletions src/boot/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { API } from '../API'
import { Graph } from '../Class/Graph'
import { EventEmitter_ } from '../EventEmitter'
import { Object_ } from '../Object'
import { Registry } from '../Registry'
import { Component } from '../client/component'
import { unmount } from '../client/context'
import { icons } from '../client/icons'
import { themeColor } from '../client/theme'
import { fromBundle } from '../spec/fromBundle'
import { start } from '../start'
import { BootOpt, System } from '../system'
import { BundleSpec } from '../types/BundleSpec'
import { Dict } from '../types/Dict'
import { GraphBundle } from '../types/GraphClass'
import { KeyboardState } from '../types/global/KeyboardState'
import { PointerState } from '../types/global/PointerState'
import { ASYNC } from '../types/interface/async/wrapper'
Expand Down Expand Up @@ -60,7 +59,7 @@ export function boot(

const {
specsCount,
specsLock,
lock: specsLock,
newSpecId,
hasSpec,
emptySpec,
Expand Down Expand Up @@ -99,9 +98,8 @@ export function boot(
classes,
components,
icons,
graphs: [],
specsCount,
specsLock,
lock: specsLock,
cache,
feature,
foreground: {
Expand All @@ -124,14 +122,11 @@ export function boot(
defaultInputModeNone: false,
...flags,
},
boot: (opt: BootOpt) => boot(system, api, opt),
fromBundle: (bundle: BundleSpec) => {
return fromBundle(bundle, specs, {})
boot: (opt_: BootOpt = {}) => {
return boot(system, api, weakMerge(opt, opt_))
},
newGraph: (Bundle: GraphBundle) => {
const graph = new Bundle(system)

return graph
start: (bundle: BundleSpec): Graph => {
return start(system, bundle)
},
getSpec: getSpec.bind(registry),
hasSpec: hasSpec.bind(registry),
Expand Down Expand Up @@ -170,25 +165,8 @@ export function boot(

remove(components, component)
},
destroy: function (): void {
for (const graph of system.graphs) {
graph.destroy()
}
},
color,
}

return system
}

export function destroy(system: System) {
const { graphs, context } = system

for (const graph of graphs) {
graph.destroy()
}

for (const context_ of context) {
unmount(context_)
}
}
2 changes: 0 additions & 2 deletions src/client/platform/web/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ export function webBoot(

const unlisten = () => {
root.removeChild(_root)

system.destroy()
}

return [system, unlisten]
Expand Down
1 change: 0 additions & 1 deletion src/client/platform/web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ import { render } from './render'
import bundle = require('./bundle.json')
import system = require('./system.json')

// @ts-ignore
render(bundle, system)
28 changes: 12 additions & 16 deletions src/client/platform/web/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,34 @@ import { Unlisten } from '../../../types/Unlisten'
import { AsyncGraph } from '../../../types/interface/async/AsyncGraph'
import { callAll } from '../../../util/call/callAll'
import { weakMerge } from '../../../weakMerge'
import { render as _render } from '../../render'
import { render as render_ } from '../../render'
import { defaultWebBoot, webBoot } from './boot'
import { webInit } from './init'

export function render(
bundle: BundleSpec,
bootOpt?: BootOpt
): [Graph, System, Unlisten] {
export function render(bundle: BundleSpec, opt?: BootOpt): [System, Unlisten] {
const { spec = {}, specs = {} } = bundle

bundle.spec = spec
bundle.specs = specs

const [system, unlistenSystem] = defaultWebBoot({
const [system, deboot] = defaultWebBoot({
specs: weakMerge(specs, _specs),
classes: _classes,
components: _components,
...bootOpt,
...opt,
})

if (!spec.id || !system.hasSpec(spec.id)) {
system.newSpec(spec)
}

const graph = start(system, bundle)

const $graph = AsyncGraph(graph)

const webUnlisten = webInit(system, window, system.root)
const renderUnlisten = _render(system, $graph)
const deinit = webInit(system, window, system.root)

const unrender = render_(system, $graph)

const unlisten = callAll([webUnlisten, renderUnlisten, unlistenSystem])
const destroy = callAll([deinit, unrender, deboot])

return [graph, system, unlisten]
return [system, destroy]
}

export function renderBundle(
Expand All @@ -53,10 +47,12 @@ export function renderBundle(
// console.log('renderBundle')

const [system, unlistenSystem] = webBoot(window, root, opt)

const graph = start(system, bundle)

const $graph = AsyncGraph(graph)

const unlistenRender = _render(system, $graph)
const unlistenRender = render_(system, $graph)

const unlisten = callAll([unlistenRender, unlistenSystem])

Expand Down
6 changes: 3 additions & 3 deletions src/mirror.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Object_ } from './Object'

export function mirror<T = any>(a: Object_<T>, b: Object_<T>) {
a.subscribe([], '*', (type, path, key, data) => {
export async function mirror<T = any>(a: Object_<T>, b: Object_<T>) {
a.subscribe([], '*', async (type, path, key, data) => {
const specId = path[0] ?? key

if (b[specId] === undefined) {
if (!(await b.hasKey(specId))) {
b.dispatch(type, path, key, data)
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/spec/Lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function lazyFromSpec(
branch: Dict<true>
) => GraphBundle
): UnitClass {
const { inputs, outputs, id } = spec
const { inputs = {}, outputs = {}, id } = spec

class Lazy<
I extends Dict<any> = Dict<any>,
Expand Down
8 changes: 4 additions & 4 deletions src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function start(

const Class = fromSpec(spec, specs, classes, {})

return startClass(system, Class, true)
return startClass(system, Class, play)
}

export function startClass(
Expand All @@ -34,9 +34,9 @@ export function startClass(
): Graph {
const graph = new Class(system)

system.graphs.push(graph)

graph.play()
if (play) {
graph.play()
}

return graph
}
Expand Down
4 changes: 1 addition & 3 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export interface System extends S, Registry {
context: Context[]
theme: Theme
color: string
graphs: Graph[]
async: AllTypes<(unit: any) => any>
cache: {
iframe: any[]
Expand Down Expand Up @@ -65,7 +64,7 @@ export interface System extends S, Registry {
flags: {
defaultInputModeNone?: boolean
}
boot: (opt: BootOpt) => System
boot: (opt?: BootOpt) => System
getLocalComponents: (remoteGlobalId: string) => any[]
registerLocalComponent: (component: any, remoteGlobalId: string) => void
unregisterLocalComponent: (component: any, remoteGlobalId: string) => void
Expand All @@ -87,7 +86,6 @@ export interface System extends S, Registry {
},
callback: (event: PointerEvent, track: Point[]) => void
) => Unlisten
destroy: () => void
}

export type IFilePickerOpt = {
Expand Down
2 changes: 2 additions & 0 deletions src/system/f/meta/New/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default class New<T> extends Holder<I<T>, O<T>> {
f({ class: Class }: I<T>, done): void {
const unit = new Class(this.__system)

unit.play()

this._unit = unit

done({ unit })
Expand Down
Loading

0 comments on commit b2aa028

Please sign in to comment.