Skip to content

Commit bb523ba

Browse files
committed
new ticker pattern for cpu
1 parent 3d18fa7 commit bb523ba

1 file changed

Lines changed: 29 additions & 40 deletions

File tree

tsunami/demo/cpuchart/app.go

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"log"
65
"time"
76

@@ -145,49 +144,39 @@ var StatsPanel = app.DefineComponent("StatsPanel", func(props StatsPanelProps) a
145144
var App = app.DefineComponent("App", func(_ struct{}) any {
146145
app.UseSetAppTitle("CPU Usage Monitor")
147146

148-
// Timer function for continuous CPU data collection
149-
timerFn := func(ctx context.Context) {
150-
for {
151-
select {
152-
case <-ctx.Done():
153-
return
154-
case <-time.After(time.Second):
155-
// Collect new CPU data point and shift the data window
156-
cpuDataAtom.SetFn(func(currentData []CPUDataPoint) []CPUDataPoint {
157-
newPoint := generateCPUDataPoint()
158-
currentDataPointCount := dataPointCountAtom.Get()
147+
// Use UseTicker for continuous CPU data collection - automatically cleaned up on unmount
148+
app.UseTicker(time.Second, func() {
149+
// Collect new CPU data point and shift the data window
150+
cpuDataAtom.SetFn(func(currentData []CPUDataPoint) []CPUDataPoint {
151+
newPoint := generateCPUDataPoint()
152+
currentDataPointCount := dataPointCountAtom.Get()
159153

160-
// Make a safe copy to avoid mutation issues
161-
data := app.DeepCopy(currentData)
154+
// Make a safe copy to avoid mutation issues
155+
data := app.DeepCopy(currentData)
162156

163-
// Ensure we have the right size array
164-
if len(data) != currentDataPointCount {
165-
// Resize array if config changed
166-
resized := make([]CPUDataPoint, currentDataPointCount)
167-
copyCount := currentDataPointCount
168-
if len(data) < copyCount {
169-
copyCount = len(data)
170-
}
171-
if copyCount > 0 {
172-
copy(resized[currentDataPointCount-copyCount:], data[len(data)-copyCount:])
173-
}
174-
data = resized
175-
}
176-
177-
// Append new point and keep only the last currentDataPointCount elements
178-
data = append(data, newPoint)
179-
if len(data) > currentDataPointCount {
180-
data = data[len(data)-currentDataPointCount:]
181-
}
182-
return data
183-
})
184-
app.SendAsyncInitiation()
157+
// Ensure we have the right size array
158+
if len(data) != currentDataPointCount {
159+
// Resize array if config changed
160+
resized := make([]CPUDataPoint, currentDataPointCount)
161+
copyCount := currentDataPointCount
162+
if len(data) < copyCount {
163+
copyCount = len(data)
164+
}
165+
if copyCount > 0 {
166+
copy(resized[currentDataPointCount-copyCount:], data[len(data)-copyCount:])
167+
}
168+
data = resized
185169
}
186-
}
187-
}
188170

189-
// Use UseGoRoutine for managed goroutine lifecycle
190-
app.UseGoRoutine(timerFn, []any{})
171+
// Append new point and keep only the last currentDataPointCount elements
172+
data = append(data, newPoint)
173+
if len(data) > currentDataPointCount {
174+
data = data[len(data)-currentDataPointCount:]
175+
}
176+
return data
177+
})
178+
app.SendAsyncInitiation()
179+
}, []any{})
191180

192181
handleClear := func() {
193182
// Reset with empty data points based on current config

0 commit comments

Comments
 (0)