Newer
Older
const JSUnit = require('./lib/jsunit.js')
let isStateKey = JSUnit.isStateKey
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// source -> heap
if (fs.existsSync(path)) {
var src = require(path)
var mod = new src()
// wants unique module id's
if(program.description.counter == null){
program.description.counter = 0
} else {
program.description.counter ++
}
// make unique name
mod.description.id = mod.description.id = mod.description.name + '-' + program.description.counter
mod.description.path = path
// add to program object
program.modules[mod.description.id] = mod
// input need references for later hookup
for (key in mod.inputs) {
mod.inputs[key].parentId = mod.description.id
mod.inputs[key].key = key
}
// state updating, begs for update
for (key in mod.state) {
if (key == 'onChange' | key == 'emitChange' | key == 'emitters') {
//console.log('rolling past change fn')
} else {
mod.state['_' + key] = mod.state[key]
mod.state[key] = {}
writeStateObject(mod, key)
}
}
if (program.description.id == null) {
if (program.description.name == null) {
if (program.description == null) {
program.description = {}
}
program.description.name = 'unnamed program'
}
program.description.id = program.description.name + '-' + 0
}
console.log('ADDING', mod.description.id, 'to', program.description.id)
// also return it so that we can write programs without the UI
return mod
} else {
console.log('ERR no module found at ', path)
}
}
function writeStateObject(mod, key) {
Object.defineProperty(mod.state, key, {
set: function(x) {
// update internal value
this['_' + key] = x
//console.log('SET', key, this['_' + key])
// push to internal state change handler
// let's call emitChange from the server-side ...
// so that we don't get into any heavy VIR
// when we change it within the module
// this.emitChange(key)
// push to external view
if(link){
pushState(mod, key)
}
}
})
Object.defineProperty(mod.state, key, {
get: function() {
//console.log('GET', key, this['_' + key])
return this['_' + key]
}
})
}
/*
EXTERNAL HOOKS
*/
function assignSocket(sckt){
socket = sckt
}
function pushState(mod, key){
console.log("GONNA PUSH IT OOOOOUT")
console.log('link', socket)
}
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
function saveProgram(prgmem, path) {
// ok, and we're interested in just copying the relevant things ...
var svprgmem = {
description: {
name: prgmem.description.name,
counter: prgmem.description.counter
},
modules: {}
}
var mdls = prgmem.modules
for(key in mdls){
var mdl = mdls[key]
var og = makeRepFromModule(mdl)
svprgmem.modules[mdl.description.id] = og
}
fs.writeFileSync(path, JSON.stringify(svprgmem, null, 2), 'utf8')
}
function openProgram(path){
var program = {}
// get the .json file as an object
var prgRep = JSON.parse(fs.readFileSync(path, 'utf8'))
console.log('OPENING THIS PRGRAM REP', prgRep)
// copy-pasta the program descro,
program.description = {
name: prgRep.description.name,
counter: 0,
id: prgRep.description.name + 1, // in another world, we count
path: path
}
// gonna get those modules from source
program.modules = {}
for(key in prgRep.modules){
var mdlRep = prgRep.modules[key]
for(modName in prgRep.modules){
// keys should be identical for rep and heap
var mdlRep = prgRep.modules[modName]
var mdl = program.modules[modName]
for(outName in mdlRep.outputs){
var outRep = mdlRep.outputs[outName]
// each has some caller ids
for(nestedInputRep in outRep.calls){
// conn from tl program -> this hookup
var nIRParent = outRep.calls[nestedInputRep].parentId
var nIRKey = outRep.calls[nestedInputRep].key
var nI = program.modules[nIRParent].inputs[nIRKey]
console.log("ATTACHING", nIRKey, 'to', outName)
mdl.outputs[outName].attach(nI)
}
}
// restoring state
for(key in mdlRep.state){
if(isStateKey(key)){
console.log("STATE - NEEDS WORK", key)
// want to do this without asserting the change though
// actually, if new paradigm, we don't call .onChange
// rename .onChange for 'onUiChange' or something
}
}
}
// once modules exist, link inputs / outputs / copy state ?
return program
}
module.exports = {
open: openProgram,
save: saveProgram,
loadModuleFromSource: loadModuleFromSource,
assignSocket: assignSocket