emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/dtache 4751b4d812 046/158: Implement a general timer fu


From: ELPA Syncer
Subject: [elpa] externals/dtache 4751b4d812 046/158: Implement a general timer function
Date: Wed, 19 Jan 2022 18:57:45 -0500 (EST)

branch: externals/dtache
commit 4751b4d8121a6f295f5e14d7f7ad09830d926332
Author: Niklas Eklund <niklas.eklund@posteo.net>
Commit: Niklas Eklund <niklas.eklund@posteo.net>

    Implement a general timer function
    
    This patch implements the dtache--session-timer function, which is a
    general function that can be configured through the
    dtache-timer-configuration variable. This function replaces the
    previous solution for remote sessions, and at the same time provides a
    function that can be reused to replace the notification function for
    MacOS users.
---
 README.org | 10 +++-------
 dtache.el  | 44 +++++++++++++++++---------------------------
 2 files changed, 20 insertions(+), 34 deletions(-)

diff --git a/README.org b/README.org
index 5c7890231e..485cb7456d 100644
--- a/README.org
+++ b/README.org
@@ -166,13 +166,9 @@ Add the following to the configuration in order to take 
advantage of this featur
 #+begin_src elisp
   (defun dtache--add-end-of-session-notification-advice (session)
     "Trigger an event when SESSION transition to inactive."
-    (let* ((timer)
-           (callback
-            (lambda ()
-              (when (dtache--session-deactivated-p session)
-                (dtache--session-final-update session)
-                (cancel-timer timer)))))
-      (setq timer (run-with-idle-timer 0.5 0.5 callback))))
+    (let ((dtache-timer-configuration
+           '(:seconds 0.5 :repeat 0.5 :function run-with-idle-timer)))
+      (dtache--session-timer session)))
 
   (advice-add 'dtache--add-end-of-session-notification :override 
#'dtache--add-end-of-session-notification-advice)
 #+end_src
diff --git a/dtache.el b/dtache.el
index f89d5f0bed..538fe5b7f2 100644
--- a/dtache.el
+++ b/dtache.el
@@ -79,6 +79,8 @@
   "Hooks to run when compiling a session.")
 (defvar dtache-metadata-annotators-alist nil
   "An alist of annotators for metadata.")
+(defvar dtache-timer-configuration '(:seconds 10 :repeat 60 :function 
run-with-timer)
+  "A property list defining how often to run a timer.")
 
 (defvar dtache-annotation-format
   `((:width 3 :function dtache--active-str :face dtache-active-face)
@@ -156,8 +158,6 @@
   "Mode of operation for dtach.")
 (defvar dtache--sessions nil
   "A list of sessions.")
-(defvar dtache--remote-session-timer nil
-  "Timer object for remote polling.")
 
 ;;;; Data structures
 
@@ -504,24 +504,6 @@ Sessions running on  current host or localhost are 
updated."
                  (seq-filter #'dtache--session-active)
                  (seq-do #'dtache-setup-notification))))
 
-(defun dtache-update-remote-sessions ()
-  "Update active remote sessions."
-  (let ((predicate
-         (lambda (s) (and (not (string= "localhost" (dtache--session-host s)))
-                     (dtache--session-active s)))))
-
-    ;; Update sessions
-    (thread-last (dtache--db-get-sessions)
-      (seq-do (lambda (it)
-                 (if (funcall predicate it)
-                     (dtache-update-session it)
-                   it))))
-
-    ;; Cancel timer if no active remote sessions
-    (unless (> (seq-count predicate (dtache--db-get-sessions)) 0)
-      (cancel-timer dtache--remote-session-timer)
-      (setq dtache--remote-session-timer nil))))
-
 (defun dtache-cleanup-host-sessions (host)
   "Run cleanuup on HOST sessions."
   (seq-do
@@ -600,7 +582,7 @@ Sessions running on  current host or localhost are updated."
 (defun dtache-setup-notification (session)
   "Setup notification for SESSION."
   (if (file-remote-p (dtache--session-working-directory session))
-      (dtache--create-remote-session-timer)
+      (dtache--session-timer session)
     (dtache--add-end-of-session-notification session)))
 
 (defun dtache-dtach-command (session)
@@ -732,12 +714,6 @@ Sessions running on  current host or localhost are 
updated."
    (file-exists-p
     (dtache-session-file session 'log))))
 
-(defun dtache--create-remote-session-timer ()
-  "Create a new remote session and trigger timer."
-  (unless dtache--remote-session-timer
-    (setq dtache--remote-session-timer
-          (run-with-timer 10 60 #'dtache-update-remote-sessions))))
-
 (defun dtache--session-header (session)
   "Return header for SESSION."
   (mapconcat
@@ -752,6 +728,20 @@ Sessions running on  current host or localhost are 
updated."
      "")
    "\n"))
 
+(defun dtache--session-timer (session)
+  "Create a timmer for SESSION according to `dtache-timer-configuration'."
+  (let* ((timer)
+         (callback
+          (lambda ()
+            (when (dtache--session-deactivated-p session)
+              (dtache--session-final-update session)
+              (cancel-timer timer)))))
+    (setq timer
+          (funcall (plist-get dtache-timer-configuration :function)
+                   (plist-get dtache-timer-configuration :seconds)
+                   (plist-get dtache-timer-configuration :repeat)
+                   callback))))
+
 ;;;;; Database
 
 (defun dtache--db-initialize ()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]