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

@@ -131,17 +131,17 @@ func (c *Context) getAction(id string) (actionEntry, error) {
return actionEntry{}, fmt.Errorf("action '%s' not found", id)
}
// OnInterval starts a go routine that sets a time.Ticker with the given duration and executes
// the given handler func() on every tick. Use *Routine.UpdateInterval to update the interval.
func (c *Context) OnInterval(duration time.Duration, handler func()) *OnIntervalRoutine {
// OnInterval starts a goroutine that executes handler on every tick of the given duration.
// The goroutine is tied to the context lifecycle and will stop when the context is disposed.
// Returns a func() that stops the interval when called.
func (c *Context) OnInterval(duration time.Duration, handler func()) func() {
var cn chan struct{}
if c.isComponent() { // components use the chan on the parent page ctx
cn = c.parentPageCtx.ctxDisposedChan
} else {
cn = c.ctxDisposedChan
}
r := newOnIntervalRoutine(cn, duration, handler)
return r
return newOnInterval(cn, duration, handler)
}
// Signal creates a reactive signal and initializes it with the given value.
@@ -379,7 +379,7 @@ func (c *Context) dispose() {
}
// stopAllRoutines closes ctxDisposedChan, broadcasting to all listening
// goroutines (OnIntervalRoutine, SSE loop) that this context is done.
// goroutines (OnInterval, SSE loop) that this context is done.
func (c *Context) stopAllRoutines() {
select {
case <-c.ctxDisposedChan: