branch: elpa/emacsql commit 5962268f83b0221e5a3b994e3a5abbf126685272 Author: Christopher Wellons <well...@nullprogram.com> Commit: Christopher Wellons <well...@nullprogram.com>
Split off reaper code. --- Makefile | 4 ++-- emacsql.el | 44 ++------------------------------------------ 2 files changed, 4 insertions(+), 44 deletions(-) diff --git a/Makefile b/Makefile index 175cf583c3..dc9bb14523 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,8 @@ BATCH := $(EMACS) -batch -Q -L . COMPILE := $(BATCH) -f batch-byte-compile TEST := $(BATCH) -l $(PACKAGE)-tests.elc -f ert-run-tests-batch -EL = emacsql-compiler.el emacsql.el emacsql-sqlite.el emacsql-psql.el \ - emacsql-tests.el +EL = emacsql-reap.el emacsql-compiler.el emacsql.el emacsql-sqlite.el \ + emacsql-psql.el emacsql-tests.el ELC = $(EL:.el=.elc) diff --git a/emacsql.el b/emacsql.el index ad279ee013..47687f6e3c 100644 --- a/emacsql.el +++ b/emacsql.el @@ -59,6 +59,7 @@ (require 'cl-lib) (require 'eieio) +(require 'emacsql-reap) (require 'emacsql-compiler) (defclass emacsql-connection () @@ -194,50 +195,9 @@ specific error conditions." ;; Automatic connection cleanup: -(defvar emacsql-connections () - "Collection of all known emacsql connections. -This collection exists for cleanup purposes.") - -(defvar emacsql-reap-timer nil - "Timer used to check for dead emacsql connections.") - (defun emacsql-register (connection) "Add CONNECTION to the global connection list." - (emacsql-start-reap-timer) - (push (cons (copy-sequence connection) (emacsql--ref connection)) - emacsql-connections)) - -(defun emacsql--ref (thing) - "Create a weak reference to THING." - (let ((ref (make-hash-table :test 'eq :size 1 :weakness 'value))) - (prog1 ref - (setf (gethash t ref) thing)))) - -(defun emacsql--deref (ref) - "Retrieve value from REF." - (gethash t ref)) - -(defun emacsql-reap () - "Clean up after lost connections." - (cl-loop for (conn-copy . ref) in emacsql-connections - when (null (emacsql--deref ref)) - count (prog1 t (ignore-errors (emacsql-close conn-copy))) - into total - else collect (cons conn-copy ref) into connections - finally (progn - (setf emacsql-connections connections) - (cl-return total)))) - -(cl-defun emacsql-start-reap-timer (&optional (interval 60)) - "Start the automatic `emacql-reap' timer." - (unless emacsql-reap-timer - (setf emacsql-reap-timer (run-at-time interval interval #'emacsql-reap)))) - -(defun emacsql-stop-reap-timer () - "Stop the automatic `emacsql-reap' timer." - (when (timerp emacsql-reap-timer) - (cancel-timer emacsql-reap-timer) - (setf emacsql-reap-timer nil))) + (emacsql-reap-register connection #'emacsql-close (copy-sequence connection))) ;; Useful macros: