Slightly better style
[8sync.git] / loopy.scm
index b93a1debbfab0bc6701c3b536394f3442c353c30..5c1455ecc86cc8f0f5eeed9b6f21da86d8a98a32 100644 (file)
--- a/loopy.scm
+++ b/loopy.scm
 ;;;    and with reasonable separation from functional components?
 
 (define-immutable-record-type <agenda>
-  (make-agenda-intern queue prompt-tag port-mapping schedule)
+  (make-agenda-intern queue prompt-tag port-mapping schedule time)
   agenda?
   (queue agenda-queue)
   (prompt-tag agenda-prompt-tag)
   (port-mapping agenda-port-mapping)
-  (schedule agenda-schedule))
+  (schedule agenda-schedule)
+  (time agenda-time))
 
 (define (make-async-prompt-tag)
   (make-prompt-tag "prompt"))
@@ -74,8 +75,9 @@
                       (queue (make-q))
                       (prompt (make-prompt-tag))
                       (port-mapping (make-port-mapping))
-                      (schedule (make-schedule)))
-  (make-agenda-intern queue prompt port-mapping schedule))
+                      (schedule (make-schedule))
+                      (time (gettimeofday)))
+  (make-agenda-intern queue prompt port-mapping schedule time))
 
 
 \f
              (agenda-run-once agenda))))
       (if (and stop-condition (stop-condition agenda))
           'done
-          (loop new-agenda)))))
+          (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
@@ -309,15 +316,19 @@ based on the results"
       (let* ((proc (q-pop! queue))
              (proc-result (call-proc proc))
              (enqueue
-              (lambda (new-proc)
-                (enq! next-queue new-proc))))
+              (lambda (run-request)
+                (cond
+                 ((run-request-when run-request)
+                  (error "TODO"))
+                 (else
+                  (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
           ;;   intent to run.  Use a (run foo) procedure
-          ((? procedure? new-proc)
+          ((? run-request? new-proc)
            (enqueue new-proc))
-          (((? procedure? new-procs) ...)
+          (((? run-request? new-procs) ...)
            (for-each
             (lambda (new-proc)
               (enqueue new-proc))