(bugfix): prevent multiple async build processes (#149)

Prevent multiple async cache builds.  This can happen when restoring a session
or loading multiple org-roam files before a build has completed.
This commit is contained in:
Herbert Jones
2020-02-21 01:45:54 -06:00
committed by GitHub
parent 22f596d275
commit d02d48e559

View File

@@ -368,23 +368,34 @@ If PREFIX, downcase the title before insertion."
(when-let ((name (completing-read "Choose a buffer: " names-and-buffers))) (when-let ((name (completing-read "Choose a buffer: " names-and-buffers)))
(switch-to-buffer (cdr (assoc name names-and-buffers)))))) (switch-to-buffer (cdr (assoc name names-and-buffers))))))
(defvar org-roam--ongoing-async-build nil
"Prevent multiple async cache builds. This can happen when
restoring a session or loading multiple org-roam files before a
build has completed.")
;;; Building the org-roam cache ;;; Building the org-roam cache
(defun org-roam--build-cache-async () (defun org-roam--build-cache-async ()
"Builds the caches asychronously." "Builds the caches asychronously."
(interactive) (interactive)
(async-start (unless org-roam--ongoing-async-build
`(lambda () (setq org-roam--ongoing-async-build t)
(setq load-path ',load-path) (async-start
(package-initialize) `(lambda ()
(require 'org-roam-utils) (setq load-path ',load-path)
,(async-inject-variables "org-roam-directory") (package-initialize)
(org-roam--build-cache org-roam-directory)) (require 'org-roam-utils)
(lambda (cache) ,(async-inject-variables "org-roam-directory")
(setq org-roam-forward-links-cache (plist-get cache :forward)) (cons org-roam-directory (org-roam--build-cache org-roam-directory)))
(setq org-roam-backward-links-cache (plist-get cache :backward)) (lambda (pair)
(setq org-roam-titles-cache (plist-get cache :titles)) (let ((directory (car pair))
(setq org-roam-cache-initialized t) (cache (cdr pair)))
(message "Org-roam cache built!")))) (setq org-roam-directory directory ;; ensure dir matches cache
org-roam-forward-links-cache (plist-get cache :forward)
org-roam-backward-links-cache (plist-get cache :backward)
org-roam-titles-cache (plist-get cache :titles)
org-roam-cache-initialized t
org-roam--ongoing-async-build nil)) ;; Remove lock
(message "Org-roam cache built!")))))
(defun org-roam--clear-cache () (defun org-roam--clear-cache ()
"Clears all entries in the caches." "Clears all entries in the caches."