wails: EventsOn (Frontend) receives inconsistent data

Description

When trying to read data from file and pass file data to string channel and after that emit event with data from string channel to Frontend, one of the event delivers faster, but sends later (async behavior)

To Reproduce

  1. Read from file and pass data to string channel (I guess that’s no matter)
for scanner.Scan() {
    text := scanner.Text()
    d.infoStream <- text
}
  1. Get data from channel and emit event with that data to Frontend
go func() {
    for info := range streams.InfoStream {
        fmt.Println("+++", info) // consistent data
	runtime.EventsEmit(*m.ctx, "sensorsData", info)
	// time.Sleep(100 * time.Microsecond) // fixes that (crutch)
    }
}()
  1. Receive data on Frontend (onclick)
const runMeasuringMachine = async () => {
  Run().then(() => {
    EventsOn('sensorsData', (result) => {
      console.log("***", result); // here inconsistent data (in one row!)
      if ((sensorsDataRef.value.length + 1) > 100) {
        sensorsDataRef.value.shift();
      }
      sensorsDataRef.value.push(result)
    })
    isMeasuringMachineRunning.value = true;
  });
}
  1. In my file, I have data:
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
10 10 0 190 190
20 20 0 180 180
30 30 0 170 170
40 40 0 160 160
50 50 0 150 150
60 60 0 140 140
70 70 0 130 130
80 80 0 120 120
90 90 180 110 110
100 100 180 100 100
110 110 180 90 90
120 120 0 80 80
130 130 0 70 70
140 140 0 60 60
150 150 0 50 50
160 160 0 40 40
170 170 0 30 30
180 180 0 20 20
190 190 0 10 10
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

but the most of the time, I’m getting on Frontend:

Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
Weight: 120
0 0 0 0 0 // inconsistent data
Weight: 120 // inconsistent data
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
10 10 0 190 190
20 20 0 180 180
30 30 0 170 170
40 40 0 160 160
50 50 0 150 150
60 60 0 140 140
70 70 0 130 130
80 80 0 120 120
90 90 180 110 110
100 100 180 100 100
110 110 180 90 90
120 120 0 80 80
130 130 0 70 70
140 140 0 60 60
150 150 0 50 50
160 160 0 40 40
170 170 0 30 30
180 180 0 20 20
190 190 0 10 10
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

Expected behaviour

Expected, that I have the same data order like in my file

Screenshots

No response

Attempted Fixes

No response

System Details

MacOS Ventura 13.4.1 (Apple M1 Max)

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 24 (10 by maintainers)

Most upvoted comments

Is there any reason you aren’t just sending it in one go?

Right now I’m doing so. I’m getting sections data in a batch and then send it. But it will be more beaty, If I would draw my sections one by one) It will be very cool to have instrument for doing something like this!

We could add sync events but it sounds like we’d be solving the wrong problem. What you want is a stream. There is some support for that over the asset server (depending on platform). Let me think about it 👍

EDIT: The best I can suggest right now is to add a “packet number” to the emitted data and order it on the front end.

Are you sure this isn’t just a race condition with your logging?

That’s the problem, my logging is in EventOn callback function. So race condition is going in it.