repl: Add spawn-repl, a simpler cooperative repl interface using spawn-fiber.
authorChristopher Allan Webber <cwebber@dustycloud.org>
Sat, 12 Aug 2017 15:21:53 +0000 (10:21 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Sat, 12 Aug 2017 15:23:13 +0000 (10:23 -0500)
Keeping around the old actor for now because it's displaying
memory leak problems, and I want to figure out what those are.

* 8sync/repl.scm (spawn-repl): New variable.

8sync/repl.scm

index 4cf60b255df6b4b85031a6bed9b3e0d670b609fa..ddcd50b0f168d82748dc2ee0e74182c8407af9a1 100644 (file)
 (define-module (8sync repl)
   #:use-module (oop goops)
   #:use-module (8sync)
+  #:use-module (fibers)
   #:use-module (8sync daydream)
   #:use-module (srfi srfi-1)
+  #:use-module (ice-9 atomic)
   #:use-module (system repl server)
   #:use-module (system repl coop-server)
-  #:export (<repl-manager>))
+  #:export (<repl-manager>
+            spawn-repl))
 
 (define-actor <repl-manager> (<actor>)
   ((add-subscriber repl-manager-add-subscriber)
   (set! (.subscribers repl-manager)
         (remove (lambda (x) (equal? x (message-from message)))
                 (.subscribers repl-manager))))
+
+\f
+
+(define* (spawn-repl #:key (path "/tmp/8sync-socket")
+                     (socket (make-unix-domain-server-socket #:path path))
+                     (poll-every 1/30)
+                     (stop? (make-atomic-box #f))
+                     (on-update #f))
+  "Spawn a cooperative REPL in a new fiber."
+  (define server
+    (spawn-coop-repl-server socket))
+  (spawn-fiber
+   (lambda ()
+     (while (not (atomic-box-ref stop?))
+       ;; Poll the server
+       (poll-coop-repl-server server)
+       ;; Call update hook, if any
+       (and on-update (on-update))
+       ;; Take a break!
+       (sleep poll-every))
+     (close socket))))