Created
December 1, 2023 03:20
-
-
Save samdphillips/008669dee5b230fbce6df75bb5795aed to your computer and use it in GitHub Desktop.
polling evt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (poll-evt poll-thunk | |
#:interval [interval-thunk (const 1000)] | |
#:cleanup [cleanup-thunk void]) | |
(poll-guard-evt | |
(lambda (polling?) | |
(if polling? | |
(cond [(poll-thunk) => | |
(lambda (v) | |
(wrap-evt always-evt (const v)))] | |
[else never-evt]) | |
(nack-guard-evt | |
(lambda (nack-evt) | |
(define ch (make-channel)) | |
(define cancel-evt | |
(handle-evt nack-evt | |
(thunk* (cleanup-thunk)))) | |
(define (poll) | |
(cond [(poll-thunk) => (lambda (v) | |
(sync cancel-evt | |
(channel-put-evt ch v)))] | |
[else #f])) | |
(define (poller) | |
(sync cancel-evt | |
(handle-evt (timer-evt (interval-thunk)) | |
(thunk* (or (poll) (poller)))))) | |
(thread poller) | |
ch)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment