Skip to content

Commit

Permalink
improve async debouncing
Browse files Browse the repository at this point in the history
see #109
  • Loading branch information
minad committed Jan 2, 2021
1 parent 2fd52e0 commit 3169944
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,10 @@ that the main package `consult.el` only depends on Emacs core components.
|-------------------------------|------------------|---------------------------------------------------------|
| consult-after-jump-hook | '(recenter) | Functions to call after jumping to a location |
| consult-async-default-split | "#" | Separator character used for splitting #async#filter |
| consult-async-input-delay | 0.5 | Input delay for asynchronous commands |
| consult-async-input-throttle | 0.5 | Input throttle for asynchronous commands |
| consult-async-input-debounce | 0.25 | Input debounce for asynchronous commands |
| consult-async-min-input | 3 | Minimum numbers of letters needed for async process |
| consult-async-refresh-delay | 0.2 | Refresh delay for asynchronous commands |
| consult-async-refresh-delay | 0.25 | Refresh delay for asynchronous commands |
| consult-goto-line-numbers | t | Show line numbers for `consult-goto-line` |
| consult-imenu-narrow || Mode-specific narrowing keys for `consult-imenu` |
| consult-imenu-toplevel || Mode-specific toplevel names used by `consult-imenu` |
Expand Down
56 changes: 41 additions & 15 deletions consult.el
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,24 @@ If this key is unset, defaults to 'consult-narrow-key SPC'."
"Function which returns project root, used by `consult-buffer' and `consult-grep'."
:type 'function)

(defcustom consult-async-refresh-delay 0.2
"Refreshing delay of the completion ui for asynchronous commands."
(defcustom consult-async-refresh-delay 0.25
"Refreshing delay of the completion ui for asynchronous commands.
The ui is only updated every `consult-async-refresh-delay' seconds."
:type 'float)

(defcustom consult-async-input-throttle 0.5
"Input throttle for asynchronous commands.
The asynchronous process is started only every
`consult-async-input-throttle' seconds."
:type 'float)

(defcustom consult-async-input-delay 0.5
"Input delay of the completion ui for asynchronous commands."
(defcustom consult-async-input-debounce 0.25
"Input debounce for asynchronous commands.
The asynchronous process is started only when there has not been new input for
`consult-async-input-debounce' seconds."
:type 'float)

(defcustom consult-async-min-input 3
Expand Down Expand Up @@ -1016,22 +1028,36 @@ CMD is the command argument list."
(funcall async 'setup))
(_ (funcall async action))))))

(defun consult--async-throttle (async &optional delay)
"Create async function from ASYNC which limits the input rate by DELAY.
(defun consult--async-throttle (async &optional throttle debounce)
"Create async function from ASYNC which throttles input.
The delay defaults to `consult-async-input-delay'."
(let ((delay (or delay consult-async-input-delay)) (input "") (timer))
The THROTTLE delay defaults to `consult-async-input-throttle'.
The DEBOUNCE delay defaults to `consult-async-input-debounce'."
(let* ((throttle (or throttle consult-async-input-throttle))
(debounce (or debounce consult-async-input-debounce))
(input "")
(throttle-timer)
(debounce-timer))
(lambda (action)
(pcase action
('setup
(funcall async 'setup)
(setq timer (run-at-time delay delay
(lambda ()
(unless (string= input "")
(funcall async input))))))
((pred stringp) (setq input action))
('destroy (cancel-timer timer)
(funcall async 'destroy))
(setq throttle-timer (run-at-time throttle throttle (lambda () (setq throttle t)))
throttle nil))
((pred stringp)
(when debounce-timer
(cancel-timer debounce-timer))
(unless (or (string= action "") (string= action input))
(setq debounce-timer (run-at-time debounce nil
(lambda ()
(when throttle
(setq throttle nil input action)
(funcall async input)))))))
('destroy
(cancel-timer throttle-timer)
(when debounce-timer
(cancel-timer debounce-timer))
(funcall async 'destroy))
(_ (funcall async action))))))

(defun consult--async-refresh-immediate (async)
Expand Down

0 comments on commit 3169944

Please sign in to comment.