summaryrefslogtreecommitdiff
path: root/timeout.el
blob: a708b04ac3d7d5791108f3ab926460b0a6968bc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
;;; timeout.el --- Throttle or debounce Elisp functions  -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Karthik Chikmagalur

;; Author: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
;; Keywords: convenience, extensions
;; Version: 1.0
;; Package-Requires: ((emacs "24.4"))
;; URL: https://github.com/karthink/timeout

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; timeout is a small Elisp library that provides higher order functions to
;; throttle or debounce Elisp functions.  This is useful for corralling
;; over-eager code that:
;; (i) is slow and blocks Emacs, and
;; (ii) does not provide customization options to limit how often it runs,
;;
;; To throttle a function FUNC to run no more than once every 2 seconds, run
;; (timeout-throttle! 'func 2.0)
;; 
;; To debounce a function FUNC to run after a delay of 0.3 seconds, run
;; (timeout-debounce! 'func 0.3)
;;
;; To create a new throttled or debounced version of FUNC instead, run
;;
;; (timeout-throttle 'func 2.0)
;; (timeout-debounce 'func 0.3)
;;
;; You can bind this via fset or defalias:
;;
;; (defalias 'throttled-func (timeout-throttle 'func 2.0))
;; (fset     'throttled-func (timeout-throttle 'func 2.0))
;;
;; The interactive spec and documentation of FUNC is carried over to the new
;; function.

;;; Code:
(require 'nadvice)

(defun timeout--throttle-advice (&optional timeout)
  "Return a function that throttles its argument function.

TIMEOUT defaults to 1 second.

When FUNC does not run because of the throttle, the result from the
previous successful call is returned.

This is intended for use as function advice."
  (let ((throttle-timer)
        (timeout (or timeout 1.0))
        (result))
    (lambda (orig-fn &rest args)
      "Throttle calls to this function."
      (prog1 result
        (unless (and throttle-timer (timerp throttle-timer))
          (setq result (apply orig-fn args))
          (setq throttle-timer
                (run-with-timer
                 timeout nil
                 (lambda ()
                   (cancel-timer throttle-timer)
                   (setq throttle-timer nil)))))))))

(defun timeout--debounce-advice (&optional delay default)
  "Return a function that debounces its argument function.

DELAY defaults to 0.50 seconds.  The function returns immediately with
value DEFAULT when called the first time.  On future invocations, the
result from the previous call is returned.

This is intended for use as function advice."
  (let ((debounce-timer nil)
        (delay (or delay 0.50)))
    (lambda (orig-fn &rest args)
      "Debounce calls to this function."
      (prog1 default
        (if (timerp debounce-timer)
            (timer-set-idle-time debounce-timer delay)
          (setq debounce-timer
                (run-with-idle-timer
                 delay nil
                 (lambda (buf)
                   (cancel-timer debounce-timer)
                   (setq debounce-timer nil)
                   (setq default
                         (if (buffer-live-p buf)
                             (with-current-buffer buf
                               (apply orig-fn args))
                           (apply orig-fn args))))
                 (current-buffer))))))))

;;;###autoload
(defun timeout-debounce! (func &optional delay default)
  "Debounce FUNC by making it run DELAY seconds after it is called.

This advises FUNC, when called (interactively or from code), to
run after DELAY seconds.   If FUNC is called again within this time,
the timer is reset.

DELAY defaults to 0.5 seconds.  Using a delay of 0 removes any
debounce advice.

The function returns immediately with value DEFAULT when called the
first time.  On future invocations, the result from the previous call is
returned."
  (if (and delay (= delay 0))
      (advice-remove func 'debounce)
    (advice-add func :around (timeout--debounce-advice delay default)
                '((name . debounce)
                  (depth . -99)))))

;;;###autoload
(defun timeout-throttle! (func &optional throttle)
  "Make FUNC run no more frequently than once every THROTTLE seconds.

THROTTLE defaults to 1 second.  Using a throttle of 0 removes any
throttle advice.

When FUNC does not run because of the throttle, the result from the
previous successful call is returned."
  (if (and throttle (= throttle 0))
      (advice-remove func 'throttle)
    (advice-add func :around (timeout--throttle-advice throttle)
                '((name . throttle)
                  (depth . -98)))))

(defun timeout-throttle (func &optional throttle)
  "Return a throttled version of function FUNC.

The throttled function runs no more frequently than once every THROTTLE
seconds.  THROTTLE defaults to 1 second.

When FUNC does not run because of the throttle, the result from the
previous successful call is returned."
  (let ((throttle-timer nil)
        (throttle (or throttle 1))
        (result))
    (if (commandp func)
        ;; INTERACTIVE version
        (lambda (&rest args)
          (:documentation
           (concat
            (documentation func)
            (format "\n\nThrottle calls to this function by %f seconds" throttle)))
          (interactive (advice-eval-interactive-spec
                        (cadr (interactive-form func))))
          (prog1 result
            (unless (and throttle-timer (timerp throttle-timer))
              (setq result (apply func args))
              (setq throttle-timer
                    (run-with-timer
                     throttle nil
                     (lambda ()
                       (cancel-timer throttle-timer)
                       (setq throttle-timer nil)))))))
      ;; NON-INTERACTIVE version
      (lambda (&rest args)
        (:documentation
         (concat
          (documentation func)
          (format "\n\nThrottle calls to this function by %f seconds" throttle)))
        (prog1 result
          (unless (and throttle-timer (timerp throttle-timer))
            (setq result (apply func args))
            (setq throttle-timer
                  (run-with-timer
                   throttle nil
                   (lambda ()
                     (cancel-timer throttle-timer)
                     (setq throttle-timer nil))))))))))

(defun timeout-debounce (func &optional delay default)
  "Return a debounced version of function FUNC.

The debounced function runs DELAY seconds after it is called.  DELAY
defaults to 0.5 seconds.

The function returns immediately with value DEFAULT when called the
first time.  On future invocations, the result from the previous call is
returned."
  (let ((debounce-timer nil)
        (delay (or delay 0.50)))
    (if (commandp func)
        ;; INTERACTIVE version
        (lambda (&rest args)
          (:documentation
           (concat
            (documentation func)
            (format "\n\nDebounce calls to this function by %f seconds" delay)))
          (interactive (advice-eval-interactive-spec
                        (cadr (interactive-form func))))
          (prog1 default
            (if (timerp debounce-timer)
                (timer-set-idle-time debounce-timer delay)
              (setq debounce-timer
                    (run-with-idle-timer
                     delay nil
                     (lambda (buf)
                       (cancel-timer debounce-timer)
                       (setq debounce-timer nil)
                       (setq default
                             (if (buffer-live-p buf)
                                 (with-current-buffer buf
                                   (apply func args))
                               (apply func args))))
                     (current-buffer))))))
      ;; NON-INTERACTIVE version
      (lambda (&rest args)
        (:documentation
         (concat
          (documentation func)
          (format "\n\nDebounce calls to this function by %f seconds" delay)))
        (prog1 default
          (if (timerp debounce-timer)
              (timer-set-idle-time debounce-timer delay)
            (setq debounce-timer
                  (run-with-idle-timer
                   delay nil
                   (lambda (buf)
                     (cancel-timer debounce-timer)
                     (setq debounce-timer nil)
                     (setq default
                           (if (buffer-live-p buf)
                               (with-current-buffer buf
                                 (apply func args))
                             (apply func args))))
                   (current-buffer)))))))))

(provide 'timeout)
;;; timeout.el ends here