Skip to content

Commit cd116be

Browse files
committed
Add input queue with delayed processing.
This avoids spamming `update-source' when the input is updated too fast (< input-delay).
1 parent 55eeffe commit cd116be

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

prompter.lisp

+39-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@
2525
:accessor nil
2626
:reader input
2727
:documentation "User input.")
28+
(input-queue
29+
(lpara.queue:make-queue)
30+
:type lpara.queue:queue
31+
:export nil
32+
:documentation "All user inputs.
33+
See `input-delay'.")
34+
(input-delay
35+
0.05
36+
:type float
37+
:documentation "When input is changed, time after which the source is updated.
38+
This is useful to avoid updating the sources")
39+
(input-reader
40+
nil
41+
:export nil
42+
:documentation "The Lparallel `future' that reads the various inputs during `input-delay'.")
2843

2944
(prompt
3045
""
@@ -199,14 +214,32 @@ Only if the PROMPTER current source has `actions-on-current-suggestion-enabled-p
199214
(:documentation "Set the action to be run on the newly selected suggestion.
200215
See also `run-action-on-current-suggestion'."))
201216

217+
(defun drain-queue (queue)
218+
"Drain and return last element of the `lparallel.queue:queue'."
219+
(labels ((pop (last-value)
220+
(multiple-value-bind (element hit?)
221+
(lparallel.queue:try-pop-queue queue)
222+
(if hit?
223+
(pop element)
224+
last-value))))
225+
(pop nil)))
226+
202227
(export-always 'input)
203228
(defmethod (setf input) (text (prompter prompter))
204-
"Update PROMPTER sources and return TEXT."
205-
(let ((old-input (slot-value prompter 'input)))
206-
(unless (string= old-input text)
207-
(setf (slot-value prompter 'input) text)
208-
(update-sources prompter text)
209-
(first-suggestion prompter)))
229+
"Update PROMPTER sources and return TEXT.
230+
This is non-blocking: the source update is done in parallel."
231+
(when (or (null (input-reader prompter))
232+
(lpara:fulfilledp (input-reader prompter)))
233+
(setf (input-reader prompter)
234+
(lparallel:future
235+
(sleep (input-delay prompter))
236+
(let ((input (drain-queue (input-queue prompter))))
237+
(let ((old-input (slot-value prompter 'input)))
238+
(unless (string= old-input text)
239+
(setf (slot-value prompter 'input) text)
240+
(update-sources prompter text)
241+
(first-suggestion prompter)))))))
242+
(lpara.queue:push-queue input (input-queue prompter))
210243
text)
211244

212245
(export-always 'destroy)

0 commit comments

Comments
 (0)