From f22be64b216fd719a58f8db11609ddb1f01ee5e9 Mon Sep 17 00:00:00 2001
From: Jake Read <jake.read@cba.mit.edu>
Date: Mon, 16 Dec 2019 15:15:19 -0500
Subject: [PATCH] carry tolink option through recursive trace

---
 hunks/view.js  | 73 +++++++++++++++++++++++++++++++++-----------------
 view/blocks.js |  1 +
 2 files changed, 50 insertions(+), 24 deletions(-)

diff --git a/hunks/view.js b/hunks/view.js
index 83d2997..befbed5 100644
--- a/hunks/view.js
+++ b/hunks/view.js
@@ -488,10 +488,6 @@ export default function View() {
       }
     }
     this.setCompleteRedraw()
-    // ok,
-    if(od.type == 'link'){
-      console.warn('link', od)
-    }
     // occasionally useful,
     return od
     /*
@@ -990,7 +986,7 @@ export default function View() {
             }
             // de-reference to indice-indice type,
             for (let cn of op.conn) {
-              console.log(`SL ${df.name} op ${op.name} push ${cn.parent.ind}, ${cn.ind}`)
+              //console.log(`SL ${df.name} op ${op.name} push ${cn.parent.ind}, ${cn.ind}`)
               oph.conn.push([parseInt(cn.parent.ind), parseInt(cn.ind)])
             }
             hnk.outputs.push(oph)
@@ -1074,16 +1070,16 @@ export default function View() {
     return new Promise(async (resolve, reject) => {
       let nh = patch.hunks.length
       let recursor = async (n) => {
-        if(debug) console.log(`MLL for ${patch.hunks[n].name}`)
+        if (debug) console.log(`MLL for ${patch.hunks[n].name}`)
         // we want to walk the list of outputs in the patch, and reconn.
         for (let op in patch.hunks[n].outputs) {
-          if(debug) console.log(`MLL for ${patch.hunks[n].outputs[op].name}`)
-          if (!defs[n].outputs[op]){
-            if(debug) console.log(`no output for reciprocal actual-def`)
+          if (debug) console.log(`MLL for ${patch.hunks[n].outputs[op].name}`)
+          if (!defs[n].outputs[op]) {
+            if (debug) console.log(`no output for reciprocal actual-def`)
             break // if the thing-actually-loaded doesn't have an output here, pass
           }
           if (!patch.hunks[n].outputs[op].conn) {
-            if(debug) console.log(`no conn for patch op`)
+            if (debug) console.log(`no conn for patch op`)
             continue // during some serializations / deserializations, empty arrays are culled, so
           }
           for (let cn of patch.hunks[n].outputs[op].conn) {
@@ -1101,7 +1097,7 @@ export default function View() {
               // here is case to put error handling in the ll, at addlink,
               // then just ignore the ones we miss ...
               try {
-                if(debug) console.log('go for add', n, op, 'to', cn)
+                if (debug) console.log('go for add', n, op, 'to', cn)
                 await this.requestAddLinkLikeACaveman(n, op, cn[0], cn[1])
               } catch (err) {
                 console.error('passing by this erroneous link-addition during link-list merging')
@@ -1110,7 +1106,7 @@ export default function View() {
             }
           }
         } // end for-each-output
-        n ++
+        n++
         if (n < nh) {
           recursor(n)
         } else {
@@ -1157,19 +1153,33 @@ export default function View() {
   // return inputdef-to-which connected,
   // assume always 1d path, bail if not
   // if tolink, look only for next link - input at positin 1, this is mgrmsgs,
-  this.trace = (opdef, tolink) => {
+  this.trace = (opdef, tolink, debug) => {
+    if(debug) console.log('TRACE LEVEL', opdef)
     // for now we are 1d, cannot traverse links,
-    if (opdef.conn.length !== 1) return
+    if (opdef.conn.length !== 1){
+      if (debug) console.log('TRACE NO CONN, RET NULL')
+      return
+    }
     // trace is fun bc it recurses thru itself,
     let next = opdef.conn[0]
     if (next.parent.type === 'link') {
       if (tolink && next.ind == 1) {
+        if(debug) console.log('TRACE RETURN', next)
         return next
       } else {
-        try{
+        try {
           let recip = next.parent.thru()
-          if (!recip) return next // no next-link,
-          if (recip.outputs[next.ind]) return this.trace(recip.outputs[next.ind]) // recurse across,
+          if (!recip){
+            if(debug) console.log('TRACE NO RECIP, NEXT', next)
+            return next // no next-link,
+          }
+          if (recip.outputs[next.ind]) {
+            if(debug) console.log('TRACE RECIP, DIVE')
+            return this.trace(recip.outputs[next.ind], tolink, debug) // recurse across,
+          } else {
+            if(debug) console.log('recip', recip)
+            if(debug) console.log('TRACE RECIP, NO NEXT OUTPUT')
+          }
         } catch (err) {
           console.error(next.parent)
           console.error(err)
@@ -1177,6 +1187,7 @@ export default function View() {
         }
       }
     } else {
+      if (debug) console.log('TRACE NEXT != LINK, RETURN', next)
       return next
     }
   }
@@ -1207,14 +1218,20 @@ export default function View() {
       }
     }
 
-    ldef.upstream = () => {
-      let outside = this.exterior() // get the link that this is 'within' ... 'this' is the view, recall
+    ldef.upstream = (debug) => {
+      let outside = this.exterior(debug) // get the link that this is 'within' ... 'this' is the view, recall
       if (!outside) {
+        if (debug) console.log('ldef.upstream return null: no exterior')
         return
       }
       try {
         let reciprocal = outside.context.defs[ldef.states[1].value]
-        if (reciprocal.type == 'link') return reciprocal
+        if (reciprocal.type == 'link') {
+          if (debug) console.log('ldef.upstream finds reciprocal link, returning')
+          return reciprocal
+        } else {
+          if (debug) console.log('ldef.upstream finds reciprocal, not a link')
+        }
       } catch (err) {
         console.error(err)
         return
@@ -1233,15 +1250,23 @@ export default function View() {
   // query to find the link-definition to the link that this view is 'inside of'
   // i.e. which link is on the upstream side of the context for which we are an MVC.
   // ... pretty straightfoward if you ask me
-  this.exterior = () => {
+  this.exterior = (debug) => {
     // skip tlv,
-    if (this == window.tlv) return
-    if (!this.def) return
+    if (this == window.tlv) {
+      if (debug) console.log(`${this.name} this.exterior return null: is top level`)
+      return
+    }
+    if (!this.def) {
+      if (debug) console.log(`${this.name}.exterior return null: has not def reciprocal`)
+      return
+    }
     // here's a funny overlay: this.outputs[0] != this.def outputs ...
-    let res = this.trace(this.def.outputs[0], true)
+    let res = this.trace(this.def.outputs[0], true, debug)
     if (res) {
+      if (debug) console.log(`${this.name} this.exterior returns a trace,`)
       return res.parent
     } else {
+      if (debug) console.log(`${this.name} this.exterior returns null: trace returns no result`)
       return
     }
   }
diff --git a/view/blocks.js b/view/blocks.js
index 16a0b67..8e7cc05 100644
--- a/view/blocks.js
+++ b/view/blocks.js
@@ -198,6 +198,7 @@ let makeLink = (ldef, titleblock) => {
       }
     }
   } else {
+    console.warn(`std link in ${ldef.context.name} ${ldef.name}`)
     // nothing special, just another def. needs help to open,
     squid(ldef, titleblock)
     $(ldef.squid).html('<i class="em em-octopus"></i> build link context')
-- 
GitLab