dep.js: Fix the way `STATE' is handled.
[dep-ui] / dep.js
diff --git a/dep.js b/dep.js
index 7b55207..d21f94a 100644 (file)
--- a/dep.js
+++ b/dep.js
@@ -118,7 +118,7 @@ var F_CHANGED = 4;                  // Changed in current phase.
 var F_RECOMPUTING = 8;                 // Currently being recomputed.
 var F_QUEUED = 16;                     // Queued for recomputation.
 
-var BAD = Tag('BAD')                   // Used for the value of `bad' deps.
+var BAD = new Tag('BAD');              // Used for the value of `bad' deps.
 
 var DELAYED = [];                      // Actions delayed by `with_frozen'.
 var PENDING = [];                      // Deps awaiting recomputation.
@@ -178,7 +178,7 @@ function Dep(value, maybefunc) {
     func = value;
     f |= F_QUEUED;
   } else {
-    val = value;
+    val = value === undefined ? BAD : value;
     func = null;
     f |= F_VALUE | F_DEPS;
   }
@@ -255,7 +255,7 @@ Dep.prototype = {
      * If the receiver isn't up-to-date then we synthesize the flags.
      */
 
-    if (this.state === 'ready') return F_VALUE | F_DEPS;
+    if (STATE === 'ready') return F_VALUE | F_DEPS;
     else if (this._generation === GENERATION) return this.__flags;
     else if (this._value_function === null) return F_VALUE | F_DEPS;
     else return 0;
@@ -394,7 +394,7 @@ Dep.prototype = {
 
     var val;
 
-    if (state === 'recomputing') {
+    if (STATE === 'recomputing') {
       if (EVALUATING) {
        this._dependents[EVALUATING._seq] = EVALUATING;
        EVALUATING._dependencies[this._seq] = this;
@@ -469,8 +469,8 @@ function recompute_pending() {
    */
 
   var d, f;
-
-  state = 'recomputing';
+  var old_state = STATE;
+  STATE = 'recomputing';
   try_finally(function () {
     while (PENDING.length) {
       d = PENDING.shift();
@@ -488,6 +488,7 @@ function recompute_pending() {
       d = PENDING.shift();
       d._value = BAD;
     }
+    STATE = old_state;
   });
 }
 
@@ -497,14 +498,13 @@ function with_frozen(body, delay) {
    * If the BODY function changes any dep values (using D.set_value(...))
    * then dependents won't be updated until the BODY completes.
    *
-   * It's very
-   * bad to do this during a recomputation phase.  If DELAY is true, then the
-   * BODY is delayed until the recomputation completes; otherwise you get an
-   * exception.
+   * It's very bad to do this during a recomputation phase.  If DELAY is
+   * true, then the BODY is delayed until the recomputation completes;
+   * otherwise you get an exception.
    */
 
   var op, val;
-  var old_delayed, old_pending;
+  var old_delayed, old_pending, old_state;
 
   switch (STATE) {
   case 'frozen':
@@ -517,6 +517,8 @@ function with_frozen(body, delay) {
   case 'ready':
     old_delayed = DELAYED;
     old_pending = PENDING;
+    old_state = STATE;
+    STATE = "frozen";
     try_finally(function () {
       DELAYED = [];
       PENDING = [];
@@ -531,6 +533,7 @@ function with_frozen(body, delay) {
     }, function () {
       DELAYED = old_delayed;
       PENDING = old_pending;
+      STATE = old_state;
     });
     break;
   }