Scaffolding for handling the "when" of the run-request
[8sync.git] / loopy.scm
index 6f1516fdb51449be60c4c952b2d024b97369e968..389d8e7a20dfe1583a8a911e47e39524eb4223eb 100644 (file)
--- a/loopy.scm
+++ b/loopy.scm
             make-time-segment time-segment?
             time-segment-time time-segment-queue
 
-            time-< time-= time-<=
+            time-< time-= time-<= time-+
+
+            <time-delta>
+            make-time-delta time-delta?
+            time-delta-sec time-delta-usec
 
             <schedule>
             make-schedule schedule?
   (or (time-< time1 time2)
       (time-= time1 time2)))
 
+
+(define-record-type <time-delta>
+  (make-time-delta-intern sec usec)
+  time-delta?
+  (sec time-delta-sec)
+  (usec time-delta-usec))
+
+(define* (make-time-delta sec #:optional usec)
+  (make-time-delta-intern sec (or usec 0)))
+
+(define (time-+ time time-delta)
+  (cons (+ (car time) (time-delta-sec time-delta))
+        (+ (cdr time) (time-delta-usec time-delta))))
+
+
 (define-record-type <schedule>
   (make-schedule-intern segments)
   schedule?
              (agenda-run-once agenda))))
       (if (and stop-condition (stop-condition agenda))
           'done
-          (loop
-           ;; Adjust the agenda's time just in time
-           ;; We do this here rather than in agenda-run-once to make
-           ;; agenda-run-once's behavior fairly predictable
-           (set-field new-agenda (agenda-time) (gettimeofday)))))))
+          (let ((updated-agenda
+                 ;; Adjust the agenda's time just in time
+                 ;; We do this here rather than in agenda-run-once to make
+                 ;; agenda-run-once's behavior fairly predictable
+                 (set-field new-agenda (agenda-time) (gettimeofday))))
+            (loop updated-agenda))))))
 
 (define (agenda-run-once agenda)
   "Run once through the agenda, and produce a new agenda
@@ -316,11 +336,16 @@ based on the results"
              (proc-result (call-proc proc))
              (enqueue
               (lambda (run-request)
-                (cond
-                 ((run-request-when run-request)
-                  (error "TODO"))
-                 (else
-                  (enq! next-queue (run-request-proc run-request)))))))
+                (match (run-request-when run-request)
+                  ((? time-delta? _)
+                   (error "TODO"))
+                  ((? integer? sec)
+                   (let ((time (cons sec 0)))
+                     (error "Also TODO")))
+                  (((? integer? sec) . (? integer? usec))
+                   (error "Also also TODO"))
+                  (#f
+                   (enq! next-queue (run-request-proc run-request)))))))
         ;; @@: We might support delay-wrapped procedures here
         (match proc-result
           ;; TODO: replace procedure with something that indicates