Moritz Heidkamp avatar Moritz Heidkamp committed 4317e84

implement result channels as channel-receive/delay which returns regular promises

Comments (0)

Files changed (2)

  make-receiving-channel
 
  ;; operations
- channel-enqueue channel-receive channel-remove-receiver close-channel
+ channel-enqueue channel-receive channel-receive/delay
+ channel-remove-receiver close-channel
 
  ;; callbacks
  on-channel-receive on-channel-close on-channel-drain
          (default (and timeout 
                        (pair? (cdr args))
                        (cadr args)))
-         (sync?   (or timeout (null? args))))
+         (sync?   (or timeout (null? args) (not (procedure? (car args))))))
 
     (when (have-lock? mutex)
       (error "can't receive from already locked channel"))
               (mutex-unlock! mutex)
               #t)))))
 
+(define (channel-receive/delay channel #!optional timeout default)
+  (let ((result (thread-start!
+                 (lambda ()
+                   (channel-receive channel timeout default)))))
+    (thread-yield!)
+    (delay (thread-join! result))))
+
 (define (close-channel channel)
   (set! (channel-closed? channel) #t)
   (run-callback-queue! (channel-on-close-handlers channel)))
   (test (channel-messages m) '(2 4 6))
   (test (channel-messages c) '()))
 
+(test-group "channel-receive/delay"
+  (define c (make-channel))
+  (define p1 (channel-receive/delay c))
+  (define p2 (channel-receive/delay c))
+  (test-assert (promise? p1))
+  (test-assert (promise? p2))
+  (channel-enqueue c 'foo)
+  (test 'foo (force p1))
+  (test 'foo (force p2))
+  (define p3 (channel-receive/delay c 0.5))
+  (test-assert (not (force p3))))
+
 (test-end)
 
 (test-exit)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.