Browse Source

Add the ability to reconnect when a connection is closed.

workspaces
Nicolas Petton 5 years ago
parent
commit
8a3310c228
Signed by: nico GPG Key ID: 233587A47C207910
5 changed files with 66 additions and 11 deletions
  1. +10
    -3
      jade-backend.el
  2. +5
    -0
      jade-faces.el
  3. +15
    -0
      jade-render.el
  4. +27
    -6
      jade-repl.el
  5. +9
    -2
      jade-webkit.el

+ 10
- 3
jade-backend.el View File

@ -57,12 +57,14 @@ BACKEND should be a symbol."
(add-to-list 'jade-backends backend))
(defun jade-quit ()
"Close the current connection and kill its REPL buffer if any."
"Close the current connection and kill its REPL buffer if any.
When called interactively, prompt for a confirmation first."
(interactive)
(unless jade-connection
(user-error "No active connection to close"))
(when (y-or-n-p (format "Do you really want to close the connection to %s ? "
(map-elt jade-connection 'url)))
(when (or (not (called-interactively-p))
(y-or-n-p (format "Do you really want to close the connection to %s ? "
(map-elt jade-connection 'url))))
(jade-backend-close-connection jade-backend jade-connection)
(setq jade-connections (remq jade-connection jade-connections))
(kill-buffer (jade-repl-get-buffer))))
@ -72,6 +74,11 @@ BACKEND should be a symbol."
(cl-defgeneric jade-backend-close-connection (backend connection)
"Close CONNECTION.")
(cl-defgeneric jade-backend-reconnect (backend)
"Try to re-establish a connection.
The new connection is created based on the current
`jade-connection'.")
(cl-defgeneric jade-backend-evaluate (backend string &optional callback)
"Evaluate STRING then call CALLBACK.
CALLBACK is called with two arguments, the value returned by the


+ 5
- 0
jade-faces.el View File

@ -34,6 +34,11 @@
"Face for the keywords."
:group 'jade-faces)
(defface jade-button-face
'((t (:inherit custom-button)))
"Face for buttons."
:group 'jade-faces)
(defface jade-header-face
'((t (:inherit header-line)))
"Face use in headers."


+ 15
- 0
jade-render.el View File

@ -48,6 +48,16 @@
'rear-nonsticky '(font-lock-face))))
(defun jade-render-button (string action)
"Render a button with the label STRING.
When clicked, evaluate ACTION.
ACTION should be a function that takes no argument."
(insert
(propertize string
'font-lock-face 'jade-button-face
'jade-action action
'reat-nons '(font-lock-face jade-action))))
(defun jade-description-string (value &optional full)
"Return a short string describing VALUE.
@ -99,5 +109,10 @@ definitions."
(reference (jade-inspector-inspect reference))
(action (funcall action)))))
(defun jade-perform-action ()
"Evaluate the button action at point."
(let ((function (get-text-property (point) 'jade-action)))
(funcall function)))
(provide 'jade-render)
;;; jade-render.el ends here

+ 27
- 6
jade-repl.el View File

@ -128,12 +128,11 @@ If URL is nil, use the current connection."
(defun jade-repl-return ()
"Depending on the position of point, jump to a reference of evaluate the input."
(interactive)
(if (get-text-property (point) 'jade-reference)
(jade-follow-link)
(progn
(unless (jade-repl--in-input-area-p)
(error "No input at point"))
(jade-repl-evaluate (jade-repl--input-content)))))
(cond
((get-text-property (point) 'jade-reference) (jade-follow-link))
((get-text-property (point) 'jade-action) (jade-perform-action))
((jade-repl--in-input-area-p) (jade-repl-evaluate (jade-repl--input-content)))
(t (error "No input or action at point"))))
(defun jade-repl-inspect ()
"Inspect the result of the evaluation of the input at point."
@ -248,6 +247,28 @@ DIRECTION is `forward' or `backard' (in the history list)."
(beginning-of-buffer)
(delete-region (point) jade-repl-prompt-start-marker))))
(defun jade-repl--handle-connection-closed ()
"Display a message when the connection is closed."
(with-current-buffer (jade-repl-get-buffer)
(save-excursion
(end-of-buffer)
(insert-before-markers "\n")
(set-marker jade-repl-output-start-marker (point))
(insert "Connection closed. ")
(jade-repl--insert-connection-buttons)
(insert "\n")
(set-marker jade-repl-input-start-marker (point))
(set-marker jade-repl-output-end-marker (point)))
(jade-repl-insert-prompt)))
(defun jade-repl--insert-connection-buttons ()
(jade-render-button "Reconnect"
(lambda ()
(jade-backend-reconnect jade-backend)))
(insert " or ")
(jade-render-button "close all buffers" #'jade-quit)
(insert "."))
(defun company-jade-repl (command &optional arg &rest _args)
"Jade REPL backend for company-mode.
See `company-backends' for more info about COMMAND and ARG."


+ 9
- 2
jade-webkit.el View File

@ -43,6 +43,14 @@
"Close the websocket associated with CONNECTION."
(websocket-close (map-elt connection 'ws)))
(cl-defmethod jade-backend-reconnect ((backend (eql webkit)))
;; close all outdated buffers first
(let* ((connection jade-connection)
(url (map-elt connection 'url))
(websocket-url (websocket-url (map-elt connection 'ws))))
(jade-quit)
(jade-webkit--open-ws-connection url websocket-url)))
(cl-defmethod jade-backend-evaluate ((backend (eql webkit)) string &optional callback)
"Evaluate STRING then call CALLBACK.
CALLBACK is called with two arguments, the value returned by the
@ -155,7 +163,6 @@ same url."
(map-put connection 'url url)
(map-put connection 'backend 'webkit)
(map-put connection 'callbacks (make-hash-table))
(map-put connection 'ws ws)
(add-to-list 'jade-connections connection)
connection))
@ -204,7 +211,7 @@ same url."
(jade-debugger-resumed))
(defun jade-webkit--handle-ws-closed (_ws)
)
(jade-repl--handle-connection-closed))
(defun jade-webkit--handle-ws-error (ws action error)
(message "WS Error! %s %s" action error))


Loading…
Cancel
Save