Skip to content

Commit 26388ed

Browse files
committed
Fix buffering output delay
Events always come with multiple lines to process. In order to process it once per event, do the following: - Call output to display the changed state - Wait 100ms to discard all the lines of the event - Call output again to display the state (which could have changed if the user did an action within the 100ms window) Before, we were always waiting 100ms after an event to display the output. There was only one call to output, but late. Without waiting for 100ms, we would be calling output too many times for a single event. Having two calls to output per 100ms is the best of the two solutions: there is responsiveness, and the number of calls is limited.
1 parent d3df2cb commit 26388ed

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

pulseaudio-control.bash

+8-1
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,16 @@ function listen() {
292292
# This is faster than having the script on an interval.
293293
pactl subscribe 2>/dev/null | grep --line-buffered -e "on card" -e "on sink" -e "on server" | {
294294
while read -r; do
295+
# Output the new state
296+
output
297+
295298
# Read all stdin to flush unwanted pending events, i.e. if there are
296-
# 15 events at the same time (100ms window), output is called once.
299+
# 15 events at the same time (100ms window), output is only called
300+
# twice.
297301
read -r -d '' -t 0.1 -n 10000
302+
303+
# After the 100ms waiting time, output again the state, as it may
304+
# have changed if the user did an action during the 100ms window.
298305
output
299306
done
300307
}

0 commit comments

Comments
 (0)