dep.js: Add optional debugging blather.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 30 Jun 2018 21:54:56 +0000 (22:54 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 30 Jun 2018 21:59:48 +0000 (22:59 +0100)
Set `DEP.DEBUG' true for a firehose.

dep.js

diff --git a/dep.js b/dep.js
index f9a9a18..989e871 100644 (file)
--- a/dep.js
+++ b/dep.js
@@ -25,6 +25,19 @@ var DEP = { }; (function () {
 
 /*----- Utility functions and classes -------------------------------------*/
 
+DEP.DEBUG = false;
+function blather(msg) {
+  /* Report MSG to the log if we're debugging. */
+  if (DEP.DEBUG) console.log(";; " + msg);
+}
+function show(what, value) {
+  /* Report that WHAT has the value VALUE, if we're debugging; and,
+   * regardless, return VALUE.
+   */
+  blather(what + " = " + value);
+  return value;
+}
+
 function dolist(list, func) {
   /* Apply FUNC to each element of LIST, discarding the results.
    *
@@ -203,9 +216,11 @@ function Dep(value, maybefunc) {
   this._dependencies = { };            // Set of objects we depend on.
 
   // If we have a recomputation function then exercise it.
+  blather("new dep " + me);
   if (func !== null) {
     with_frozen(function () {
       PENDING.push(me);
+      blather("push new dep " + me);
     });
   }
 }
@@ -268,6 +283,7 @@ Dep.prototype = {
      * function which doesn't handle propagation or anything like that.
      */
 
+    blather("_update " + this + ": " + value);
     if (value === BAD ?
        this._value === BAD :
        (this._value !== BAD &&
@@ -293,8 +309,9 @@ Dep.prototype = {
     this._dependencies = { };
     return try_finally(function () {
       EVALUATING = me;
-      return orelse(function () { return me._value_function(); },
-                   function () { return BAD; });
+      return show("_new_value for " + me,
+                 orelse(function () { return me._value_function(); },
+                        function () { return show("ret", BAD); }));
     }, function() {
       EVALUATING = old;
     });
@@ -402,6 +419,7 @@ Dep.prototype = {
       }
     }
     val = this._value;
+    blather("value " + this + ": " + val);
     if (val === BAD) throw BAD;
     return val;
   },
@@ -471,6 +489,7 @@ function recompute_pending() {
   var d, f;
   var old_state = STATE;
   STATE = 'recomputing';
+  blather("STATE <- :recomputing");
   try_finally(function () {
     while (PENDING.length) {
       d = PENDING.shift();
@@ -487,8 +506,10 @@ function recompute_pending() {
     while (PENDING.length) {
       d = PENDING.shift();
       d._value = BAD;
+      blather("recompute_pending mark " + d);
     }
     STATE = old_state;
+    blather("STATE <- :" + old_state);
   });
 }
 
@@ -519,6 +540,7 @@ function with_frozen(body, delay) {
     old_pending = PENDING;
     old_state = STATE;
     STATE = "frozen";
+    blather("STATE <- :frozen");
     try_finally(function () {
       DELAYED = [];
       PENDING = [];
@@ -534,6 +556,7 @@ function with_frozen(body, delay) {
       DELAYED = old_delayed;
       PENDING = old_pending;
       STATE = old_state;
+      blather("STATE <- :" + old_state);
     });
     break;
   }