From 501bb7294d3f3086ee7d0c20fc7de155e6e8cc5e Mon Sep 17 00:00:00 2001
From: Jake Read <jake.read@cba.mit.edu>
Date: Sat, 14 Dec 2019 15:38:38 -0500
Subject: [PATCH] shoot the gap

---
 core/scope.js  |  9 +-------
 hunks/view.js  | 60 ++++++++++++++++++++++++++++++++++++++++++--------
 view/blocks.js |  3 +--
 3 files changed, 53 insertions(+), 19 deletions(-)

diff --git a/core/scope.js b/core/scope.js
index 330e166..c555969 100644
--- a/core/scope.js
+++ b/core/scope.js
@@ -230,7 +230,6 @@ export default function Scope(wrapper, tlv) {
     // we don't have to handle anything else
     if (view.hasCompleteRedraw) {
       console.log("SCOPE run for complete redraw")
-      console.time('redraw-complete')
       blocks.wipeContext(view)
       for (let i = 0; i < view.defs.length; i++) {
         blocks.redrawDef(view.defs[i])
@@ -240,7 +239,6 @@ export default function Scope(wrapper, tlv) {
       view.hasConnectivityChange = false
       view.hasDefUpdate = false
       view.hasCompleteRedraw = false
-      console.timeEnd('redraw-complete')
       return
     }
 
@@ -251,7 +249,6 @@ export default function Scope(wrapper, tlv) {
     // and probably connection / rewires (add / sub inputs)
     if (view.hasDefUpdate) {
       console.log('SCOPE run for hasDefUpdate')
-      console.time('redraw-defupdate')
       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
@@ -263,21 +260,17 @@ export default function Scope(wrapper, tlv) {
         }
       }
       view.hasDefUpdate = false
-      console.timeEnd('redraw-defupdate')
     }
 
     // also, we do this globally. tough. scrape and move on
     if (view.hasConnectivityChange) {
       console.log('SCOPE run for connectivity change')
-      console.time('redraw-connectivity')
       blocks.redrawContexWires(view)
       view.hasConnectivityChange = false
-      console.timeEnd('redraw-connectivity')
     }
 
     if (view.hasDefPositionUpdate) {
       console.log('SCOPE run for reposition from view')
-      console.time('redraw-position')
       for (let i = 0; i < view.defs.length; i++) {
         if (view.defs[i].hasPositionUpdate) {
           blocks.repositionDef(view.defs[i])
@@ -285,7 +278,6 @@ export default function Scope(wrapper, tlv) {
         }
       }
       view.hasDefPositionUpdate = false
-      console.timeEnd('redraw-position')
     }
   }
 
@@ -295,6 +287,7 @@ 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 6ea1e40..300ab51 100644
--- a/hunks/view.js
+++ b/hunks/view.js
@@ -327,8 +327,6 @@ function View() {
 
   // dessicate
   this.cleanDef = (def) => {
-    // take it down to its constituent, non-circular elements.
-    console.log(`VIEW CLEANS ${def.ind}`)
     // rm everything ...
     for (let key in def) {
       if (def.hasOwnProperty(key)) {
@@ -439,6 +437,7 @@ function View() {
   // and the manager is responsible for reporting back to us all existing wires ...
   let replaceDef = (spec) => {
     let od = defs[spec.ind]
+    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)
@@ -446,12 +445,45 @@ function View() {
     for (let op of od.outputs) {
       rmAllWires(op)
     }
-    // keep position,
-    if (od.position) spec.position = od.position
-    defs[spec.ind] = spec
+    // now, since we are occasionally mid-recipe when we replace a def,
+    // we don't want to just nuke that old handle - so we really should walk this thing,
+    // replacing name, type, etc,
+    od.name = spec.name
+    od.type = spec.type
+    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]){
+        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]){
+        od.states[s].name = spec.states[s].name
+        od.states[s].type = spec.states[s].type
+        od.states[s].value = spec.states[s].value
+      } else {
+        od.states.push(spec.states[s])
+      }
+    }
     this.setCompleteRedraw()
+    console.log('replaced', od)
     // occasionally useful,
+    return od
+    /*
+    defs[spec.ind] = spec
+    this.setCompleteRedraw()
     return spec
+    */
   }
 
   /* ---------------------------    ---------------------------- */
@@ -1212,21 +1244,21 @@ function View() {
           console.log('REC thru', context, entrance)
           for (let df of context.defs) {
             if (df.type === 'link' && df !== entrance) {
-              if (thru(df)) {
+              if (df.thru()) {
                 // bit of a lazy break: we could do a trace-copy,
                 // manually copying each element ... then we can retain ptrs and
                 // avoid handling all of these names later...
                 let uniquetrace = traceClone(trace)
                 uniquetrace.push({
                   entrance: df,
-                  exit: thru(df)
+                  exit: df.thru()
                 })
                 // if this is it, trace building is complete,
-                if (thru(df).context == ipdef.parent.context) {
+                if (df.thru().context == ipdef.parent.context) {
                   // other branches will run to completion, route will remain...
                   route = uniquetrace
                 } else {
-                  recursor(thru(df).context, thru(df), traceClone(trace))
+                  recursor(df.thru().context, df.thru(), traceClone(trace))
                 }
               } else {
                 console.log('no thru', df)
@@ -1274,9 +1306,19 @@ function View() {
           let porthole = route[route.length - 1].exit
           let portlist = porthole.states[3].value
           portlist += `, at_${porthole.outputs.length} (${type})`
+          /*
+          the eternal recurrence of the pointer statefulness problem
+          when we replace, our porthole <ptr to ldef> is no longer the ldef we want or need, it's a dead ptr
+          to something that exists nowhere else in the tree
+          ok, so, two moves: one: re-reference here,
+          two: probably more robust: write replaceDef in such a way that existing ptrs stay all gucci gucci
+          ok, ok, party, burrito, etc.
+          */
           await ipdef.parent.context.requestStateChange(porthole.states[3], portlist)
+          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])
           await gateway.context.requestAddLink(opdef, gateway.inputs[gateway.inputs.length - 1])
           resolve()
         } catch (err) {
diff --git a/view/blocks.js b/view/blocks.js
index e618372..b17c402 100644
--- a/view/blocks.js
+++ b/view/blocks.js
@@ -99,6 +99,7 @@ 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,
@@ -327,8 +328,6 @@ let wipeWires = (context) => {
 
 // this will scrape the def, removing any of its div elements, etc
 let cleanDef = (def) => {
-  // keep position
-  console.log(`blocks cleans ${def.ind}`)
   // keep this local for holdover,
   let ps = def.position
   // rules for us: only ever attach blocks to
-- 
GitLab