Skip to content
Snippets Groups Projects
link.js 3.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jake Read's avatar
    Jake Read committed
    /*
    
    line input
    
    */
    
    
    Jake Read's avatar
    Jake Read committed
    // HEADER
    import {
      Hunkify,
      Input,
      Output,
      State
    } from './hunks.js'
    // END HEADER
    
    Jake Read's avatar
    Jake Read committed
    
    function Link() {
    
    Jake Read's avatar
    Jake Read committed
      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
      }
    
    Jake Read's avatar
    Jake Read committed
      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
        }
    
      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)
    
    Jake Read's avatar
    Jake Read committed
          } else {
    
            // pull from old list
            //console.log('found', this.inputs[nks[i].nameKey])
            newports[nks[i].nameKey] = this.inputs[nks[i].nameKey]
    
    Jake Read's avatar
    Jake Read committed
        }
    
    
        // I feel no remorse,
        delete this.inputs
        this.inputs = {}
        this.inputs = newports
    
        // if OK
    
        // 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
    
    Jake Read's avatar
    Jake Read committed
      } // end loop
    
    Jake Read's avatar
    Jake Read committed
    }
    
    
    Jake Read's avatar
    Jake Read committed
    // FOOTER
    export default Link
    // END FOOTER