From 93d02eaa8bd123dd96f190b5b481544b8642acd2 Mon Sep 17 00:00:00 2001
From: Jake Read <jake.read@cba.mit.edu>
Date: Sat, 14 Dec 2019 17:20:43 -0500
Subject: [PATCH] save heirarchic, refresh heirarchic

---
 core/scope.js       |   3 +-
 hunks/view.js       | 116 ++++++++++++++++++++++++++------------------
 view/blocks.js      |   3 +-
 view/contextmenu.js |  50 -------------------
 4 files changed, 71 insertions(+), 101 deletions(-)

diff --git a/core/scope.js b/core/scope.js
index c555969..625e592 100644
--- a/core/scope.js
+++ b/core/scope.js
@@ -248,7 +248,7 @@ export default function Scope(wrapper, tlv) {
     // defs w/ updates are for: state items, name changes,
     // and probably connection / rewires (add / sub inputs)
     if (view.hasDefUpdate) {
-      console.log('SCOPE run for hasDefUpdate')
+      console.log('SCOPE run for def update')
       for (let i = 0; i < view.defs.length; i++) {
         if (view.defs[i].hasUpdate) {
           // for now, we just redraw the entire definition, whenever we have updates for it
@@ -287,7 +287,6 @@ export default function Scope(wrapper, tlv) {
     let ldef = context.exterior()
     if (!ldef) throw new Error("no apparent link container for this context")
     // things should now be redrawn,
-    console.warn('RD for open')
     blocks.redrawDef(ldef)
   }
 
diff --git a/hunks/view.js b/hunks/view.js
index 300ab51..6f6f0a5 100644
--- a/hunks/view.js
+++ b/hunks/view.js
@@ -436,8 +436,13 @@ function View() {
   // because of link-reconn-issues, we also assume all existing links are gone,
   // and the manager is responsible for reporting back to us all existing wires ...
   let replaceDef = (spec) => {
+    // I'll admit I'm not 100% stoked about this, and it's first order for another protocol
+    // implementation. graph state is a trip, but seems like it shouldn't have to be. more focus on a tighter
+    // underlying rep, and operations on it - i.e. I like .interior() etc as functions that walk the static
+    // graph state, rather than things like .context, .parent etc that are static ptrs that might get 'yeeted'
+    // as the kids might say
     let od = defs[spec.ind]
-    if(od.type != spec.type) throw new Error('this is a bad idea')
+    if (od.type != spec.type) throw new Error('this is a bad idea')
     // as a rule, when we replace defs, we unhook everything.
     for (let ip of od.inputs) {
       rmAllWires(ip)
@@ -450,24 +455,24 @@ function View() {
     // replacing name, type, etc,
     od.name = spec.name
     od.type = spec.type
-    for(let i in spec.inputs){
-      if(od.inputs[i]){
+    for (let i in spec.inputs) {
+      if (od.inputs[i]) {
         od.inputs[i].name = spec.inputs[i].name
         od.inputs[i].type = spec.inputs[i].type
       } else {
         od.inputs.push(spec.inputs[i])
       }
     }
-    for(let o in spec.outputs){
-      if(od.outputs[o]){
+    for (let o in spec.outputs) {
+      if (od.outputs[o]) {
         od.outputs[o].name = spec.outputs[o].name
         od.outputs[o].type = spec.outputs[o].type
       } else {
         od.outputs.push(spec.outputs[o])
       }
     }
-    for(let s in spec.states){
-      if(od.states[s]){
+    for (let s in spec.states) {
+      if (od.states[s]) {
         od.states[s].name = spec.states[s].name
         od.states[s].type = spec.states[s].type
         od.states[s].value = spec.states[s].value
@@ -476,7 +481,6 @@ function View() {
       }
     }
     this.setCompleteRedraw()
-    console.log('replaced', od)
     // occasionally useful,
     return od
     /*
@@ -806,7 +810,7 @@ function View() {
   and then handle etcs afterwards, i.e. probably just cleaning up .clean() (ha)
   */
   this.refresh = () => {
-    console.log('REFRESH')
+    console.log(`REFRESH ${this.name}`)
     // well, I won't say it's clean or beautiful, but we are interested in keeping the
     // positions of things intact, so I am here for this:
     let psns = []
@@ -819,7 +823,13 @@ function View() {
       }
     }
     // now we wipe,
-    return new Promise((resolve, reject) => {
+    return new Promise(async (resolve, reject) => {
+      // we want to run this down levels,
+      for(let df of this.defs){
+        if(df.type == 'link'){
+          if(df.interior()) await df.interior().refresh()
+        }
+      }
       // wipe ya docs, and ask yonder manager for a complete description
       // setting ahn JS array's length to zero effectively erases it, unless
       // there are other references to them someplace ... then GC will keepalive
@@ -927,51 +937,63 @@ function View() {
   }
 
   this.save = () => {
-    return new Promise((resolve, reject) => {
+    return new Promise(async (resolve, reject) => {
       // now is the thyme to write-out our defs,
       let patch = {
         // cerntakt
         interpreterName: this.interpreterName,
         interpreterVersion: this.interpreterVersion,
+        open: this.open,
+        size: this.size,
         hunks: []
       }
       for (let df of defs) {
-        // want to scrape out just-the-necessary bits
-        // proposal is to save minimum viable stuff
-        let hnk = {
-          name: df.name,
-          type: df.type,
-          inputs: [],
-          outputs: [],
-          states: []
-        }
-        for (let st of df.states) {
-          hnk.states.push({
-            name: st.name,
-            type: st.type,
-            value: st.value
-          })
-        }
-        for (let ip of df.inputs) {
-          hnk.inputs.push({
-            name: ip.name,
-            type: ip.type
-          })
-        }
-        for (let op of df.outputs) {
-          let oph = {
-            name: op.name,
-            type: op.type,
-            conn: []
+        try {
+          // want to scrape out just-the-necessary bits
+          // proposal is to save minimum viable stuff
+          let hnk = {
+            name: df.name,
+            type: df.type,
+            inputs: [],
+            outputs: [],
+            states: []
           }
-          // de-reference to indice-indice type,
-          for (let cn of op.conn) {
-            oph.conn.push([parseInt(cn.parent.ind), parseInt(cn.ind)])
+          for (let st of df.states) {
+            hnk.states.push({
+              name: st.name,
+              type: st.type,
+              value: st.value
+            })
+          }
+          for (let ip of df.inputs) {
+            hnk.inputs.push({
+              name: ip.name,
+              type: ip.type
+            })
+          }
+          for (let op of df.outputs) {
+            let oph = {
+              name: op.name,
+              type: op.type,
+              conn: []
+            }
+            // de-reference to indice-indice type,
+            for (let cn of op.conn) {
+              oph.conn.push([parseInt(cn.parent.ind), parseInt(cn.ind)])
+            }
+            hnk.outputs.push(oph)
+          }
+          if (df.position) hnk.position = df.position
+          if (df.type == 'link') {
+            if (df.interior()) {
+              hnk.contains = await df.interior().save() // this should recurse, yeah ?
+            }
           }
-          hnk.outputs.push(oph)
+          patch.hunks.push(hnk)
+        } catch (err) {
+          console.error(err)
+          reject('trouble saving patch')
         }
-        if (df.position) hnk.position = df.position
-        patch.hunks.push(hnk)
       }
       resolve(patch)
     })
@@ -1315,10 +1337,10 @@ function View() {
           ok, ok, party, burrito, etc.
           */
           await ipdef.parent.context.requestStateChange(porthole.states[3], portlist)
-          console.warn('porthole', porthole.outputs[porthole.outputs.length - 1], ipdef)
+          //console.warn('porthole', porthole.outputs[porthole.outputs.length - 1], ipdef)
           await ipdef.parent.context.requestAddLink(porthole.outputs[porthole.outputs.length - 1], ipdef)
           // to avoid piping data into the void, we can do this last ->
-          console.log('gateway', opdef, gateway.inputs[gateway.inputs.length - 1])
+          //console.log('gateway', opdef, gateway.inputs[gateway.inputs.length - 1])
           await gateway.context.requestAddLink(opdef, gateway.inputs[gateway.inputs.length - 1])
           resolve()
         } catch (err) {
@@ -1478,7 +1500,7 @@ function View() {
             console.error('received hunk replace for non existent hunk')
             console.error('here is the spec:', repSpec)
           } else {
-            console.log("REPLACE", repSpec.ind)
+            console.log(`REPLACED ${repSpec.name}`)
             let nd = replaceDef(repSpec)
             if (nd.type == 'link') addLinkHelpers(nd)
             if (cbd) cbd.callback(nd)
diff --git a/view/blocks.js b/view/blocks.js
index b17c402..d289fb8 100644
--- a/view/blocks.js
+++ b/view/blocks.js
@@ -99,7 +99,6 @@ let makeLink = (ldef, titleblock) => {
   // first we want to get some state for the link,
   // assuming that other levels will rebuild this when those
   // change (scope.js)
-  console.log('LDEF at makeLink', ldef)
   let context = ldef.interior()
   if(context){
     // we are hooked to a view,
@@ -558,7 +557,7 @@ let repositionDef = (def) => {
 }
 
 let redrawDef = (def) => {
-  console.log('REDRAW for def', def.name)
+  console.log('BLOCKS REDRAW for', def.name)
   // this wipes and redraws,
   rebuildDef(def)
   // place them all in the context,
diff --git a/view/contextmenu.js b/view/contextmenu.js
index 97ec1c6..902a373 100644
--- a/view/contextmenu.js
+++ b/view/contextmenu.js
@@ -355,56 +355,6 @@ function planeMenu(evt, context) {
       console.error('err during patch save')
       console.error(err)
     })
-    /*
-    let debug = true
-    if (debug) console.log(`SS: running GO`)
-    view.globalOrganize()
-    // the top ... context = view, so
-    let tlp = view.patchset.writeCurrent()
-    // heirarchichal traverse,
-    let recursor = (context, branch, ld) => {
-      if (debug) console.log(`SS: recurse at ${context.name} thru ${ld.name}`)
-      // ld -> link def
-      // context -> view that this link lives within
-      let localcontext = view.trace(ld.outputs[1], debug)
-      if (debug) console.log("SAVESYS: traces to ", localcontext)
-      // if this link traces up to a view,
-      if (localcontext && localcontext.parent.type === 'view' && localcontext.parent.hunk.hasRefreshed) {
-        // for the reciprocal link, nest
-        localcontext = localcontext.parent.hunk
-        if (debug) console.log("SAVESYS: traces to view")
-        // we can use this view to write its contents
-        let nlp = localcontext.patchset.writeCurrent()
-        // now we need to put it ...
-        branch.hunks[ld.ind].contains = nlp
-        // find the 'otherLink' state object
-        let oli
-        for (let st of ld.states) {
-          if (st.name === 'otherLink') {
-            oli = st.value
-          }
-        }
-        // and continue,
-        for (let dfi in localcontext.defs) {
-          // don't walk backwards !
-          if (dfi === oli) continue
-          let df = localcontext.defs[dfi]
-          if (df.type === 'link') {
-            recursor(localcontext, nlp, df)
-          }
-        }
-      } else {
-        // no view for this link, don't traverse
-      }
-    } // fin traverse
-    //
-    // kick off firstverse ...
-    for (let df of view.defs) {
-      if (df.type === 'link') {
-        recursor(view, tlp, df)
-      }
-    } // end recursive cycle ?
-    */
   }) // end save entire system
 
   // MERGE A SYSTEM
-- 
GitLab