diff --git a/watch.el b/watch.el index 523fa24..07dce30 100644 --- a/watch.el +++ b/watch.el @@ -19,6 +19,8 @@ "Seconds between polls. This is the watch rate, not the frame rate.") (defvar clj-watch--timer nil) +(defvar clj-watch--connection nil + "The CIDER connection buffer to poll, captured when watching starts.") (defun clj-watch--paint (text) "Replace the watch buffer's contents with TEXT." @@ -36,17 +38,24 @@ (defun clj-watch--tick () "Poll the snapshot once, asynchronously." - (if (not (get-buffer clj-watch-buffer)) - (clj-watch-stop) + (cond + ((not (get-buffer clj-watch-buffer)) (clj-watch-stop)) + ((not (buffer-live-p clj-watch--connection)) + (clj-watch--paint "error:\nCIDER connection lost") + (clj-watch-stop)) + (t ;; Async, not `cider-nrepl-sync-request': a sync request on a timer blocks - ;; Emacs's UI thread every tick. + ;; Emacs's UI thread every tick. Pass the connection explicitly -- the + ;; timer fires with no reliable "current buffer" to resolve one from. (cider-nrepl-request:eval "(watch/render)" (lambda (response) (nrepl-dbind-response response (value err) (cond (err (clj-watch--paint (format "error:\n%s" err))) - (value (clj-watch--paint (car (read-from-string value)))))))))) + (value (clj-watch--paint (car (read-from-string value))))))) + nil nil nil nil + clj-watch--connection)))) (define-derived-mode clj-watch-mode special-mode "clj-watch" "Major mode for the pinned watch buffer." @@ -56,7 +65,7 @@ (defun clj-watch () "Open the pinned watch buffer and start polling." (interactive) - (cider-current-repl nil 'ensure) + (setq clj-watch--connection (cider-current-repl nil 'ensure)) (with-current-buffer (get-buffer-create clj-watch-buffer) (unless (eq major-mode 'clj-watch-mode) (clj-watch-mode)))