Copy previous variables / eval result across file reloads.
[whenjobs.git] / lib / whenstate.ml
index ddbc302..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 ""
 
@@ -115,7 +145,7 @@ let get_everyjobs t =
   List.filter (function { job_cond = Every_job _ } -> true | _ -> false) t.jobs
 
 let get_job t jobname =
-  try StringMap.find jobname t.jobmap with Not_found -> assert false
+  StringMap.find jobname t.jobmap
 
 let evaluate_whenjob ?(onload = false) t job =
   match job with