Skip to content

Commit ab1d8e5

Browse files
Merge pull request #65 from Aerion/listen-optim
Optimize listen function
2 parents dabdb49 + 349f205 commit ab1d8e5

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

pulseaudio-control.bash

+9-17
Original file line numberDiff line numberDiff line change
@@ -283,26 +283,18 @@ function showOSD() {
283283

284284

285285
function listen() {
286-
local firstRun=0
286+
# If this is the first time start by printing the current state. Otherwise,
287+
# directly wait for events. This is to prevent the module being empty until
288+
# an event occurs.
289+
output
287290

288291
# Listen for changes and immediately create new output for the bar.
289292
# This is faster than having the script on an interval.
290-
pactl subscribe 2>/dev/null | {
291-
while true; do
292-
{
293-
# If this is the first time just continue and print the current
294-
# state. Otherwise wait for events. This is to prevent the
295-
# module being empty until an event occurs.
296-
if [ $firstRun -eq 0 ]; then
297-
firstRun=1
298-
else
299-
read -r event || break
300-
# Avoid double events
301-
if ! echo "$event" | grep -e "on card" -e "on sink" -e "on server"; then
302-
continue
303-
fi
304-
fi
305-
} &>/dev/null
293+
pactl subscribe 2>/dev/null | grep --line-buffered -e "on card" -e "on sink" -e "on server" | {
294+
while read -r; do
295+
# 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.
297+
read -r -d '' -t 0.1 -n 10000
306298
output
307299
done
308300
}

0 commit comments

Comments
 (0)