Copy previous variables / eval result across file reloads.
[whenjobs.git] / lib / whenstate.ml
index fe53a16..e3aad07 100644 (file)
@@ -77,6 +77,36 @@ let set_variable t name value =
 let copy_variables old t =
   { t with variables = StringMap.fold StringMap.add old.variables t.variables }
 
+let copy_prev_state old t =
+  let is_explicit jobname =
+    String.length jobname < 4 || String.sub jobname 0 4 <> "job$"
+  in
+
+  let prev_variables = StringMap.fold (
+    fun jobname _ map ->
+      try
+        if not (is_explicit jobname) then raise Not_found;
+        (* See if we can find a job with the same name in the old state. *)
+        let old_vars = StringMap.find jobname old.prev_variables in
+        StringMap.add jobname old_vars map
+      with
+        Not_found -> map
+  ) t.jobmap t.prev_variables in
+
+  let prev_eval_result = StringMap.fold (
+    fun jobname _ map ->
+      try
+        if not (is_explicit jobname) then  raise Not_found;
+        (* See if we can find a job with the same name in the old state. *)
+        let old_result = StringMap.find jobname old.prev_eval_result in
+        StringMap.add jobname old_result map
+      with
+        Not_found -> map
+  ) t.jobmap t.prev_eval_result in
+
+  { t with
+    prev_variables = prev_variables; prev_eval_result = prev_eval_result }
+
 let get_variable t name =
   try StringMap.find name t.variables with Not_found -> T_string ""