refactor: simplify OnInterval API to auto-start and return stop func

Replace the exported OnIntervalRoutine struct (Start/Stop/UpdateInterval)
with a single function that auto-starts the goroutine and returns an
idempotent stop closure. Uses close(channel) instead of send-on-channel,
fixing a potential deadlock when the goroutine exits via context disposal.

Closes #5 item 4.
This commit is contained in:
Ryan Hamamura
2026-02-12 12:27:50 -10:00
parent 2310e45d35
commit 532651552a
4 changed files with 39 additions and 79 deletions

View File

@@ -37,29 +37,33 @@ func main() {
return 1000 / time.Duration(refreshRate.Int()) * time.Millisecond
}
updateData := c.OnInterval(computedTickDuration(), func() {
ts := time.Now().UnixMilli()
val := rand.ExpFloat64() * 10
var stopUpdate func()
startInterval := func() {
stopUpdate = c.OnInterval(computedTickDuration(), func() {
ts := time.Now().UnixMilli()
val := rand.ExpFloat64() * 10
c.ExecScript(fmt.Sprintf(`
if (myChart) {
myChart.appendData({seriesIndex: 0, data: [[%d, %f]]});
myChart.setOption({},{notMerge:false,lazyUpdate:true});
};
`, ts, val))
})
updateData.Start()
c.ExecScript(fmt.Sprintf(`
if (myChart) {
myChart.appendData({seriesIndex: 0, data: [[%d, %f]]});
myChart.setOption({},{notMerge:false,lazyUpdate:true});
};
`, ts, val))
})
}
startInterval()
updateRefreshRate := c.Action(func() {
updateData.UpdateInterval(computedTickDuration())
stopUpdate()
startInterval()
})
toggleIsLive := c.Action(func() {
isLive = isLiveSig.Bool()
if isLive {
updateData.Start()
startInterval()
} else {
updateData.Stop()
stopUpdate()
}
})
c.View(func() h.H {