git.fiddlerwoaroof.com
js-executor.lisp
be6f8571
 (cl:in-package :hhgbot-augmented-assistant)
 
 (defclass js-executor ()
111b7353
   ((%thread :accessor thread :initform nil)
    (%inp :accessor work-queue :initform (make-instance 'chanl:unbounded-channel))))
be6f8571
 
 (defparameter *js-executor* (make-instance 'js-executor))
 
 (defun submit-js (executor js)
   (with-accessors ((work-queue work-queue)) executor
     (let ((promise (blackbird-base:make-promise :name "js-execution")))
       (chanl:send work-queue
29fd86f0
 		              (list promise js))
be6f8571
       promise)))
 
 (defmethod slacker:start-module ((event-pump event-pump) (exe js-executor))
   (declare (ignorable event-pump))
801447ba
   
be6f8571
   (values exe
801447ba
           (setf (thread exe)
                 (bt:make-thread
                  (lambda ()
                    (loop
                      (multiple-value-bind (message message-p) (chanl:recv (work-queue exe))
                        (when message-p
                          (destructuring-bind (promise script) message
                            (handler-case
                                (blackbird-base:finish promise
                                                       (cl-js:run-js script))
                              (serious-condition (c) (blackbird:signal-error promise c)))))
 		                   (sleep 0.4))))
 	               :name "js-executor"))))
111b7353
 
 (defmethod slacker:stop-module ((event-pump event-pump) (exe js-executor))
   (declare (ignorable event-pump))
   (with-accessors ((thread thread)) exe
801447ba
     (format t "~&executor: ~s~&" thread)
111b7353
     (when thread
       (bt:destroy-thread thread))))