Browse Source

Refactor closing connections when connecting

When opening a connection, make sure that any previous connection is closed
first.

* indium-interaction.el: Handle closing previous connections.
* indium-chrome.el:
* indium-nodejs.el: Do not handle closing connections (this is now always done
  in indium-interaction.el).
* test/unit/indium-nodejs-test.el: Remove obsolete test.
* test/unit/indium-interaction-test.el: Add tests.
workspaces
Nicolas Petton 2 years ago
parent
commit
587c98ae21
No known key found for this signature in database GPG Key ID: E8BCD7866AFCF978
5 changed files with 44 additions and 49 deletions
  1. +3
    -7
      indium-chrome.el
  2. +14
    -10
      indium-interaction.el
  3. +9
    -13
      indium-nodejs.el
  4. +17
    -0
      test/unit/indium-interaction-test.el
  5. +1
    -19
      test/unit/indium-nodejs-test.el

+ 3
- 7
indium-chrome.el View File

@ -66,13 +66,9 @@
(defun indium-connect-to-chrome ()
"Open a connection to a Chrome tab."
(when (or (null indium-current-connection)
(yes-or-no-p "This requires closing the current connection. Are you sure? "))
(when-indium-connected
(indium-quit))
(let* ((host (indium-chrome--host))
(port (indium-chrome--port)))
(indium-chrome--get-tabs-data host port #'indium-chrome--connect-to-tab))))
(let* ((host (indium-chrome--host))
(port (indium-chrome--port)))
(indium-chrome--get-tabs-data host port #'indium-chrome--connect-to-tab)))
(defun indium-launch-chrome ()
"Start chrome/chromium with remote debugging enabled."


+ 14
- 10
indium-interaction.el View File

@ -56,21 +56,25 @@
(defun indium-connect ()
"Open a new connection to a runtime."
(interactive)
(with-indium-workspace-configuration
(pcase (map-elt indium-workspace-configuration 'type)
("node" (indium-connect-to-nodejs))
("chrome" (indium-connect-to-chrome))
(_ (user-error "Invalid project type, check the .indium.json project file")))))
(indium-maybe-quit)
(unless-indium-connected
(with-indium-workspace-configuration
(pcase (map-elt indium-workspace-configuration 'type)
("node" (indium-connect-to-nodejs))
("chrome" (indium-connect-to-chrome))
(_ (user-error "Invalid project type, check the .indium.json project file"))))))
;;;###autoload
(defun indium-launch ()
"Start a process (web browser or NodeJS) and attempt to connect to it."
(interactive)
(with-indium-workspace-configuration
(pcase (map-elt indium-workspace-configuration 'type)
("node" (indium-launch-nodejs))
("chrome" (indium-launch-chrome))
(_ (user-error "Invalid project type, check the .indium.json project file")))))
(indium-maybe-quit)
(unless-indium-connected
(with-indium-workspace-configuration
(pcase (map-elt indium-workspace-configuration 'type)
("node" (indium-launch-nodejs))
("chrome" (indium-launch-chrome))
(_ (user-error "Invalid project type, check the .indium.json project file"))))))
(defun indium-quit ()
"Close the current connection and kill its REPL buffer if any.


+ 9
- 13
indium-nodejs.el View File

@ -74,19 +74,15 @@ Execute the command based on `indium-nodejs--command', adding the
connection on it.
If `indium-nodejs--inspect-brk' is set to non-nil, break the
execution at the first statement.
If a connection is already open, close it."
(indium-maybe-quit)
(unless indium-current-connection
(let* ((default-directory (indium-workspace-root))
(process (make-process :name "indium-nodejs-process"
:buffer "*node process*"
:filter #'indium-nodejs--process-filter
:command (list shell-file-name
shell-command-switch
(indium-nodejs--command-with-flags)))))
(switch-to-buffer (process-buffer process)))))
execution at the first statement."
(let* ((default-directory (indium-workspace-root))
(process (make-process :name "indium-nodejs-process"
:buffer "*node process*"
:filter #'indium-nodejs--process-filter
:command (list shell-file-name
shell-command-switch
(indium-nodejs--command-with-flags)))))
(switch-to-buffer (process-buffer process))))
(defun indium-connect-to-nodejs ()
"Open a connection to an existing NodeJS process."


+ 17
- 0
test/unit/indium-interaction-test.el View File

@ -76,6 +76,23 @@
(indium-reconnect)
'foo)))
(describe "Killing previous connections when connecting"
(it "should kill the previous connection process when there is one"
(let ((indium-current-connection (make-indium-connection
:process 'first-process)))
(spy-on #'indium-connect-to-nodejs)
(spy-on 'y-or-n-p :and-return-value t)
(spy-on 'kill-process)
(spy-on 'process-buffer)
(spy-on 'process-status :and-return-value 'run)
(spy-on 'indium-backend-close-connection)
(with-js2-file (indium-launch))
(expect #'kill-process :to-have-been-called-with 'first-process)
(expect #'indium-backend-close-connection :to-have-been-called))))
(describe "Finding the AST node to evaluate"
(it "can find variable nodes"
(with-js2-buffer "var foo = 2;\nfoo"


+ 1
- 19
test/unit/indium-nodejs-test.el View File

@ -45,25 +45,7 @@
;; Regression for GitHub issue #150
(spy-on #'indium-nodejs--command :and-return-value "ENV_VAR=\"VAL\" node foo")
(expect (indium-nodejs--command-with-flags)
:to-equal "ENV_VAR=\"VAL\" node --inspect-brk foo"))
(it "should kill the previous connection process when there is one"
(let ((indium-current-connection (make-indium-connection
:process 'first-process)))
(spy-on #'indium-nodejs--command :and-return-value "node index.js")
(spy-on 'make-process :and-return-value 'second-process)
(spy-on 'y-or-n-p :and-return-value t)
(spy-on 'switch-to-buffer)
(spy-on 'kill-process)
(spy-on 'process-buffer)
(spy-on 'process-status :and-return-value 'run)
(spy-on 'indium-backend-close-connection)
(with-js2-file (indium-launch-nodejs))
(expect #'kill-process :to-have-been-called-with 'first-process)
(expect #'indium-backend-close-connection :to-have-been-called))))
:to-equal "ENV_VAR=\"VAL\" node --inspect-brk foo")))
(describe "Connecting to a NodeJS process"
(it "should find the websocket URL from the process output"


Loading…
Cancel
Save