|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | "log" |
6 | 5 | "time" |
7 | 6 |
|
@@ -145,49 +144,39 @@ var StatsPanel = app.DefineComponent("StatsPanel", func(props StatsPanelProps) a |
145 | 144 | var App = app.DefineComponent("App", func(_ struct{}) any { |
146 | 145 | app.UseSetAppTitle("CPU Usage Monitor") |
147 | 146 |
|
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() |
159 | 153 |
|
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) |
162 | 156 |
|
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 |
185 | 169 | } |
186 | | - } |
187 | | - } |
188 | 170 |
|
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{}) |
191 | 180 |
|
192 | 181 | handleClear := func() { |
193 | 182 | // Reset with empty data points based on current config |
|
0 commit comments