Last active
August 19, 2025 18:31
-
-
Save thierryvolpiatto/f237ce4dd6bead32869573068aa66450 to your computer and use it in GitHub Desktop.
timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;; timeout.el --- a modified version of timeout package -*- lexical-binding: t -*- | |
| ;;; Commentary: Remove unuseful stuff from | |
| ;; https://github.com/karthink/timeout | |
| ;; | |
| ;; Use oclosure to allow access to arguments. | |
| ;; | |
| ;; Usage: | |
| ;; | |
| ;; (fset 'foo-debounced (timeout-debounce 'foo 0.6)) | |
| ;; (fset 'foo-throttled (timeout-throttle 'foo 0.6)) | |
| ;; | |
| ;; To have a docstring use defalias instead. | |
| ;; | |
| ;; To advice a function: | |
| ;; (advice-add 'foo :override (timeout-debounce 'foo 0.6) | |
| ;; '((name . foo-debounced))) | |
| ;; | |
| ;; To modify delay: | |
| ;; | |
| ;; (setf (foo-debounced--delay (symbol-function 'foo-debounced)) 0.7) | |
| ;; | |
| ;; To get the result of last call of a function: | |
| ;; | |
| ;; (foo-debounced--result (symbol-function 'foo-debounced)) | |
| ;; FIXME: Not sure how we refer to foo-debounced with an advice, | |
| ;; advice-function-member-p maybe ? | |
| ;; | |
| ;; En programmation, "debounce" veut dire ignorer des événements (ou | |
| ;; signaux) qui arrivent trop rapidement les uns après les autres, | |
| ;; pour ne pas les traiter plusieurs fois inutilement. | |
| ;; En programmation, "throttle" signifie limiter la fréquence à | |
| ;; laquelle une fonction est appelée. | |
| ;;; Code: | |
| (eval-when-compile (require 'oclosure)) | |
| (oclosure-define timeout-closure | |
| "Build a delayed function for `timeout-debounce' or `timeout-throttle'." | |
| function method (timer :mutable t) | |
| (delay :type 'float :mutable t) (result :mutable t)) | |
| (defun timeout--debounce-or-throttle (func method &optional delay default) | |
| (oclosure-lambda (timeout-closure | |
| (function func) | |
| (method method) | |
| (timer nil) | |
| (delay (or delay | |
| (pcase method | |
| ('debounce 0.5) | |
| ('throttle 1)))) | |
| (result default)) | |
| (&rest args) | |
| (prog1 result | |
| (unless (timerp timer) | |
| (when (eq method 'throttle) | |
| (setq result (apply func args))) | |
| (setq timer | |
| (funcall (pcase method | |
| ('debounce #'run-with-idle-timer) | |
| ('throttle #'run-with-timer)) | |
| delay nil | |
| (lambda (buf) | |
| (cancel-timer timer) | |
| (setq timer nil) | |
| (when (eq method 'debounce) | |
| (setq result | |
| (if (buffer-live-p buf) | |
| (with-current-buffer buf | |
| (apply function args)) | |
| (apply function args))))) | |
| (current-buffer))))))) | |
| (defun timeout-debounce (func &optional delay default) | |
| "Return a debounced version of function FUNC. | |
| FUNC will be called only when emacs become idle that DELAY many | |
| seconds, all the calls happening meanwhile are ignored. | |
| DELAY defaults to 0.5 second. DEFAULT is the immediate return value | |
| before FUNC is called, the next return values will be the result of FUNC | |
| call." | |
| (timeout--debounce-or-throttle func 'debounce delay default)) | |
| (defun timeout-throttle (func &optional delay default) | |
| "Return a throttled version of function FUNC. | |
| Limit the frequency of FUNC calls to DELAY seconds, all the calls | |
| happening meanwhile are ignored. | |
| DELAY defaults to 1 second. DEFAULT is the immediate return value | |
| before FUNC is called, the next return values will be the result of FUNC | |
| call." | |
| (timeout--debounce-or-throttle func 'throttle delay default)) | |
| (provide 'timeout) | |
| ;;; timeout.el ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment