From 11aac39a1b438b130a8f07730c346f8ef8e10907 Mon Sep 17 00:00:00 2001 From: "Charl P. Botha" Date: Wed, 12 Aug 2020 11:07:27 +0200 Subject: [PATCH] (feat): propagate file changes on idle-timer (#1032) Queue up file updates, to be processed on idle timer. This makes saving files more responsive for large files. Co-authored-by: Jethro Kuan --- CHANGELOG.md | 1 + org-roam.el | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 487f6bf..d7851e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ In this release we support fuzzy links of the form `[[Title]]`, `[[*Headline]]` ### Features +- [#1032](https://github.com/org-roam/org-roam/pull/1032) File changes are now propagated to the database on idle timer. This prevents large wait times during file saves. - [#974](https://github.com/org-roam/org-roam/pull/974) Protect region targeted by `org-roam-insert` - [#994](https://github.com/org-roam/org-roam/pull/994) Simplify org-roam-store-link - [#910](https://github.com/org-roam/org-roam/pull/910) Support fuzzy links of the form [[Title]], [[*Headline]] and [[Title*Headline]] diff --git a/org-roam.el b/org-roam.el index 17f256e..d255b7a 100644 --- a/org-roam.el +++ b/org-roam.el @@ -272,6 +272,12 @@ descriptive warnings when certain operations fail (e.g. parsing).") "]")) "Matches a typed link in double brackets.") +(defvar org-roam--file-update-timer nil + "Timer for updating the database on file changes.") + +(defvar org-roam--file-update-queue nil + "List of files that need to be processed for a database update. Processed within `org-roam--file-update-timer'.") + ;;;; Utilities (defun org-roam--plist-to-alist (plist) "Return an alist of the property-value pairs in PLIST." @@ -1404,6 +1410,23 @@ file." (t 'org-link)))) +(defun org-roam--queue-file-for-update (&optional file-path) + "Queue FILE-PATH for `org-roam' database update. +This is a lightweight function that is called during `after-save-hook' +and only schedules the current Org file to be `org-roam' updated +during the next idle slot." + (let ((fp (or file-path buffer-file-name))) + (when (org-roam--org-roam-file-p file-path) + (add-to-list 'org-roam--file-update-queue fp)))) + +(defun org-roam--process-update-queue () + "Process files queued in `org-roam--file-update-queue'." + (when org-roam--file-update-queue + (mapc #'org-roam-db--update-file org-roam--file-update-queue) + (org-roam-message "database updated during idle: %s." + (mapconcat #'file-name-nondirectory org-roam--file-update-queue ", ") ) + (setq org-roam--file-update-queue nil))) + ;;;; Hooks and Advices (defun org-roam--find-file-hook-function () "Called by `find-file-hook' when mode symbol `org-roam-mode' is on." @@ -1411,7 +1434,7 @@ file." (setq org-roam-last-window (get-buffer-window)) (add-hook 'post-command-hook #'org-roam-buffer--update-maybe nil t) (add-hook 'before-save-hook #'org-roam--replace-fuzzy-link-on-save nil t) - (add-hook 'after-save-hook #'org-roam-db--update-file nil t) + (add-hook 'after-save-hook #'org-roam--queue-file-for-update nil t) (add-hook 'completion-at-point-functions #'org-roam-complete-at-point nil t) (org-roam-buffer--update-maybe :redisplay t))) @@ -1562,6 +1585,7 @@ M-x info for more information at Org-roam > Installation > Post-Installation Tas (add-hook 'kill-emacs-hook #'org-roam-db--close-all) (add-hook 'org-open-at-point-functions #'org-roam-open-id-at-point) (add-hook 'org-open-link-functions #'org-roam--open-fuzzy-link) + (setq org-roam--file-update-timer (run-with-idle-timer 2 t #'org-roam--process-update-queue)) (advice-add 'rename-file :after #'org-roam--rename-file-advice) (advice-add 'delete-file :before #'org-roam--delete-file-advice) (when (fboundp 'org-link-set-parameters) @@ -1574,6 +1598,8 @@ M-x info for more information at Org-roam > Installation > Post-Installation Tas (remove-hook 'kill-emacs-hook #'org-roam-db--close-all) (remove-hook 'org-open-at-point-functions #'org-roam-open-id-at-point) (remove-hook 'org-open-link-functions #'org-roam--open-fuzzy-link) + (when org-roam--file-update-timer + (cancel-timer org-roam--file-update-timer)) (advice-remove 'rename-file #'org-roam--rename-file-advice) (advice-remove 'delete-file #'org-roam--delete-file-advice) (when (fboundp 'org-link-set-parameters) @@ -1585,7 +1611,7 @@ M-x info for more information at Org-roam > Installation > Post-Installation Tas (with-current-buffer buf (remove-hook 'post-command-hook #'org-roam-buffer--update-maybe t) (remove-hook 'before-save-hook #'org-roam--replace-fuzzy-link-on-save t) - (remove-hook 'after-save-hook #'org-roam-db--update-file t)))))) + (remove-hook 'after-save-hook #'org-roam--queue-file-for-update t)))))) ;;; Interactive Commands ;;;###autoload