diff options
| author | Teddy Wing | 2021-01-31 19:51:45 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2021-01-31 19:51:45 +0100 | 
| commit | 13110208dbfb74a67fdd8a6165ed5857c8806d43 (patch) | |
| tree | fc553ebf4dbf051987ad92fddb22d0be41056d5d | |
| parent | d730aaba8371012c9a0fb7afd154a44a92ef8a96 (diff) | |
| download | extreload-13110208dbfb74a67fdd8a6165ed5857c8806d43.tar.bz2 | |
main: Replace `sleep` call with a `wait-group`
The `sleep` call allowed me to test the behaviour of the program, since
without it, it would exit before the WebSocket messages had a chance to
be sent and received.
But we shouldn't be waiting a fixed number of seconds for the program to
execute. Instead, we should only keep the program alive as long as there
are messages to be sent and received.
This adds a Go-style wait group using my wait-group library that
increments the wait group when we send a WebSocket message, and
decrements it when we receive a WebSocket response. That allows us to
keep the program alive only for the amount of time necessary for the
messages to be exchanged.
| -rw-r--r-- | l/extreload.asd | 3 | ||||
| -rw-r--r-- | l/src/main.lisp | 18 | 
2 files changed, 15 insertions, 6 deletions
| diff --git a/l/extreload.asd b/l/extreload.asd index 21a5329..8a1cfc2 100644 --- a/l/extreload.asd +++ b/l/extreload.asd @@ -1,7 +1,8 @@  (asdf:defsystem extreload    :version "0.0.1"    :depends-on (:jsown -               :websocket-driver-client) +                :wait-group +                :websocket-driver-client)    :components ((:module "src"                  :serial t                  :components ((:file "package") diff --git a/l/src/main.lisp b/l/src/main.lisp index 42c0ccd..7ad0611 100644 --- a/l/src/main.lisp +++ b/l/src/main.lisp @@ -2,14 +2,16 @@  (defvar *client* (wsd:make-client "ws://127.0.0.1:55755/devtools/browser/ec1d4b1c-ced0-47ab-a32e-6fdd5b51e1ba")) +(defvar *wg* (wait-group:make-wait-group)) +  (defun main ()    (wsd:start-connection *client*)    (wsd:on :message *client* #'ws-on-message) -  (wsd:send *client* (target-get-targets-msg 1)) +  (websocket-send *client* (target-get-targets-msg 1)) -  (sleep 5) +  (wait-group:wait *wg*)    (wsd:close-connection *client*)) @@ -49,7 +51,9 @@          (reload-extension            (json-obj-get              (json-obj-get response "params") -            "sessionId"))))) +            "sessionId"))) + +    (wait-group:done *wg*)))  (defun parse-get-targets-response (response)    (let* ((result (json-obj-get response "result")) @@ -78,11 +82,11 @@  (defun attach-to-target (extension)    (let ((target-id (json-obj-get extension "targetId"))) -    (wsd:send *client* +    (websocket-send *client*                (target-attach-to-target-msg 2 target-id))))  (defun reload-extension (session-id) -  (wsd:send *client* +  (websocket-send *client*              (runtime-evaluate-msg 1 session-id "chrome.runtime.reload()")))  (defun extension-targets (targets) @@ -91,3 +95,7 @@                        "background_page")))      (filter #'extensionp targets))) + +(defun websocket-send (client data) +  (wsd:send *client* data) +  (wait-group:add *wg*)) | 
