Newer
Older
// HEADER
import {
Hunkify,
Input,
Output,
State
} from './hunks.js'
// END HEADER
Hunkify(this, 'Link')
// data in/out
// assumed that whatever is on the other end of bytes
// does byte / packet level flowcontrol
let dtin = new Input('byteArray', 'data')
this.inputs.push(dtin)
let dtout = new Output('byteArray', 'data')
this.outputs.push(dtout)
let inputList = new State('string', 'inputList', "msgs (byteArray)")
let outputList = new State('string', 'outputList', "msgs (byteArray)")
this.state.push(inputList, outputList)
// ok, easy to add them at init,
// will be the trick to add-and-then-report-to-manager when already running
// here's a few nice functions to have at
// returns an array of {type: tk, name: nk}
let getTypeAndNameKeys = (str) => {
let keys = str.split(',')
console.log('keys', keys)
let ks = new Array()
for(let i in keys){
let tk = keys[i].substring(keys[i].indexOf('(') + 1, keys[i].indexOf(')'))
let nk = keys[i].substring(0, keys[i].indexOf(' ('))
if(nk[0] == ' ') nk = nk.substring(1)
if(tk.length < 2 || nk.length < 2){
this.log('bad key pair on inputs / outputs at link')
} else {
ks.push({typeKey: tk, nameKey: nk})
}
}
return ks
}
this.init = () => {
// manager calls this once
// it is loaded and state is updated (from program)
this.log('LINK INIT BEGINS ->')
// safe to assume that inports and outports are emp-t at the moment,
let ipKeys = getTypeAndNameKeys(inputList.value)
for(let k of ipKeys){
let tip = new Input(k.typeKey, k.nameKey)
this.inputs[k.nameKey] = tip
}
let opKeys = getTypeAndNameKeys(outputList.value)
for(let k of opKeys){
let top = new Output(k.typeKey, k.nameKey)
this.outputs[k.nameKey] = top
}
// now the changes,
inputList.change = (value) => {
// OK: back up to this ...
// msgs (byteArray), another (type)
// OK: doing a lot of the same as before, and as output, rewrite to do that ...
// use those here ... check that list against existing, moving where appropriate
// and adding where appropriate, but not wholeheartedly re-writing
//console.log('value', value)
let nks = getTypeAndNameKeys(value)
//console.log('nks', nks)
let oks = getTypeAndNameKeys(inputList.value)
//console.log('oks', oks)
// the algo:
// (1) initializes new pairs,
// (2) deletes pairs that are not existing
// (3) reorders to match
// a list of indices to delete
// how does it sound out for object keys ?
// or like?
let newports = {data: dtin}
for(let i in nks){
let ind = oks.indexOf(nks[i])
if(ind === -1){
// didn't exist before, we make it
let newport = new Input(nks[i].typeKey, nks[i].nameKey)
newports[nks[i].nameKey] = newport
//console.log('making', newport)
// pull from old list
//console.log('found', this.inputs[nks[i].nameKey])
newports[nks[i].nameKey] = this.inputs[nks[i].nameKey]
// I feel no remorse,
delete this.inputs
this.inputs = {}
this.inputs = newports
// if OK
inputList.set(value)
// else
console.log('reset inputs:', this.inputs)
console.log('should match newports:', newports)
//this.log('no bueno inputlist change request')
// to wrap on this before (and in segue to) recasting inputs / outputs as arrays,
// we should connect with the manager to send an updated version of ourselves
// use the backdoor
this.mgr.sendHunkAsDef(this)
}
outputList.change = (value) => {
// TODO: same as above,
}
let outbuffer = new Array()
this.loop = () => {
// hurm
// FOOTER
export default Link
// END FOOTER