lang: funcs: Fixup race in vumeter example

The vumeter example was written quickly and without much care. This
fixes a possible race (panic) and also removes the busy loop that wastes
CPU while we're waiting for the first value to come in.
This commit is contained in:
James Shubin
2021-02-06 23:59:06 -05:00
parent db95b6381f
commit 28451d1e14

View File

@@ -23,6 +23,7 @@ import (
"os/exec"
"strconv"
"strings"
"sync"
"syscall"
"time"
@@ -89,7 +90,8 @@ func (obj *VUMeterFunc) Stream() error {
// FIXME: this goChan seems to work better than the ticker :)
// this is because we have a ~1sec delay in capturing the value in exec
goChan := make(chan struct{})
close(goChan)
once := &sync.Once{}
onceFunc := func() { close(goChan) } // only run once!
for {
select {
case input, ok := <-obj.init.Input:
@@ -109,9 +111,11 @@ func (obj *VUMeterFunc) Stream() error {
obj.symbol = input.Struct()["symbol"].Str()
obj.multiplier = input.Struct()["multiplier"].Int()
obj.peak = input.Struct()["peak"].Float()
once.Do(onceFunc)
continue // we must wrap around and go in through goChan
//case <-ticker.C: // received the timer event
case <-goChan:
case <-goChan: // triggers constantly
if obj.last == nil {
continue // still waiting for input values