Skip to content

Commit

Permalink
Improve resource leftovers
Browse files Browse the repository at this point in the history
There are two things here that are both aimed at reducing the amount of
resource clutter:

1. I've seen many tmp files left.  Looking at the code, it seems to me
   like a reasonable thing to get rid of them when a file is saved.  I
   did this via adding `tide-remove-tmp-file` on `after-save-hook`.

2. I also saw that there is a general tendency to accumulate many
   servers since they're never removed (actually, more than just the
   servers -- the whole project resources are kept, but the server is
   the main problem wrt resources).  This is also mentioned in ananthakumaran#256.

   So I implemented a function that scans all buffers and cleanup all
   projects that have no live buffers.  It looks to me like a good idea
   to do this, since you can just kill old buffers to reduce resource
   usage.  (And killing old buffers is more obvious than explicitly
   openning the server list to kill old ones, especially since there's
   no way to tell if a server is used by some buffer or not.)

   (I added this function onto `kill-buffer-hook`, but if that's too
   extreme, then a more mild option is to not do that and just let
   people add it themselves.)
  • Loading branch information
elibarzilay committed Oct 30, 2019
1 parent e4c76a4 commit 7e0dfaa
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tide.el
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,18 @@ If TIDE-TSSERVER-EXECUTABLE is set by the user use it. Otherwise check in the n
(remhash project-name tide-tsserver-unsupported-commands)
(remhash project-name tide-project-configs))

(defun tide-cleanup-dead-projects* ()
(tide-cleanup-dead-projects (current-buffer)))
(defun tide-cleanup-dead-projects (ignore)
(let ((live-projects '()))
(dolist (b (-remove-item ignore (buffer-list)))
(-when-let (proj (with-current-buffer b
(and (bound-and-true-p tide-mode)
(tide-project-name))))
(cl-pushnew proj live-projects)))
(dolist (proj (-difference (hash-table-keys tide-servers) live-projects))
(tide-cleanup-project proj))))

(defun tide-start-server-if-required ()
(unless (tide-current-server)
(tide-start-server)))
Expand Down Expand Up @@ -1953,15 +1965,19 @@ code-analysis."
(progn
(add-hook 'after-save-hook 'tide-sync-buffer-contents nil t)
(add-hook 'after-save-hook 'tide-auto-compile-file nil t)
(add-hook 'after-save-hook 'tide-remove-tmp-file nil t)
(add-hook 'after-change-functions 'tide-handle-change nil t)
(add-hook 'kill-buffer-hook 'tide-cleanup-buffer nil t)
(add-hook 'kill-buffer-hook 'tide-cleanup-dead-projects* nil t)
(add-hook 'hack-local-variables-hook 'tide-configure-buffer nil t)
(when (commandp 'typescript-insert-and-indent)
(eldoc-add-command 'typescript-insert-and-indent)))
(remove-hook 'after-save-hook 'tide-sync-buffer-contents t)
(remove-hook 'after-save-hook 'tide-auto-compile-file t)
(remove-hook 'after-save-hook 'tide-remove-tmp-file t)
(remove-hook 'after-change-functions 'tide-handle-change t)
(remove-hook 'kill-buffer-hook 'tide-cleanup-buffer t)
(remove-hook 'kill-buffer-hook 'tide-cleanup-dead-projects* t)
(remove-hook 'hack-local-variables-hook 'tide-configure-buffer t)
(tide-cleanup-buffer)))

Expand Down

0 comments on commit 7e0dfaa

Please sign in to comment.