Skip to content

Instantly share code, notes, and snippets.

@samdphillips
Created December 1, 2023 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samdphillips/008669dee5b230fbce6df75bb5795aed to your computer and use it in GitHub Desktop.
Save samdphillips/008669dee5b230fbce6df75bb5795aed to your computer and use it in GitHub Desktop.
polling evt
(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