fixed the start and stop behaviour. it was crashing

This commit is contained in:
2026-09-18 15:38:17 +01:00
parent 2d4875fbd0
commit a6683df10b
5 changed files with 152 additions and 147 deletions
+17 -10
View File
@@ -33,7 +33,8 @@ type IRacing struct {
ticker *time.Ticker // ticker will keep polling intervals constant
// Stream
streamCh chan telemetry.TelemetryData
wg sync.WaitGroup
// streamCh chan telemetry.TelemetryData
streamCancel context.CancelFunc
}
@@ -50,10 +51,10 @@ func NewIRacingProvider(
}
provider := &IRacing{
logger: logger,
SDK: sdk,
data: telemetry.NewTelemetryData(),
streamCh: make(chan telemetry.TelemetryData, 1),
logger: logger,
SDK: sdk,
data: telemetry.NewTelemetryData(),
// streamCh: make(chan telemetry.TelemetryData, 1),
// NOTE: This is because I stupidly recorded a test IBT file in 240
// TODO: make this configurable from the user side
ticker: time.NewTicker(time.Second / 240),
@@ -129,11 +130,14 @@ func (i *IRacing) isDataAvailable() bool {
return true
}
func (i *IRacing) stream(ctx context.Context) {
func (i *IRacing) stream(ctx context.Context) <-chan telemetry.TelemetryData {
i.data.InitialTime = time.Now()
outCh := make(chan telemetry.TelemetryData)
i.wg.Add(1)
go func() {
defer close(i.streamCh)
defer i.wg.Done()
defer close(outCh)
// Put this into the configuration file
consecutiveTimeouts := 0
@@ -153,7 +157,7 @@ func (i *IRacing) stream(ctx context.Context) {
// Publish data
select {
case i.streamCh <- *i.data:
case outCh <- *i.data:
default:
// skip this data, don't allow publishers to lag behind
}
@@ -169,6 +173,8 @@ func (i *IRacing) stream(ctx context.Context) {
}
}
}()
return outCh
}
func (i *IRacing) readData() {
@@ -205,9 +211,9 @@ func (i *IRacing) Stream() (<-chan telemetry.TelemetryData, error) {
ctx, i.streamCancel = context.WithCancel(context.Background())
// Start the stream
i.stream(ctx)
ch := i.stream(ctx)
return i.streamCh, nil
return ch, nil
}
func (i *IRacing) StopStream() {
@@ -216,6 +222,7 @@ func (i *IRacing) StopStream() {
}
i.streamCancel()
i.wg.Wait()
i.streamCancel = nil
}