#lang imperative-syndicate

(provide (struct-out external-event))

(message-struct external-event (descriptor values))

(spawn #:name 'external-event-relay
       (during/spawn (observe (inbound (external-event $desc _)))
         (define ch (make-channel))
         (thread (lambda ()
                   (let loop ()
                     (sync ch
                           (handle-evt desc
                                       (lambda results
                                         (ground-send! (inbound (external-event desc results)))
                                         (loop)))))))
         (signal-background-activity! +1)
         (on-stop (channel-put ch 'quit)
                  (signal-background-activity! -1))))
