5

Inside the sensors component I have the following code which polls data each 5 seconds:

  Observable
    .interval(this.sensorsRefreshRate * 1000)
    .startWith(0)
    .switchMap(() => Observable.fromPromise(this.getSensorsPromise(this.hubId)))
    .subscribe(res => {
      this.sensors = res.data;
    })

In the component's template I am generating a child Sensor component for each item in the sensors array.

         <app-sensor
            [sensor]="sensor" 
            *ngFor="let sensor of sensors; trackBy: trackByFn">
          </app-sensor>

Child sensor component is responsible for updating the sensor.

The problem occurs if update is made just before the API returns polled data. In that case it looks like the changed data is overridden by old unchanged data. Next time when the data is polled, the change a user has made will be reflected.

Short explanation:

  1. Updating sensor status flag to "true"
  2. Meanwhile, API is returning data from the moment of update, status flag was "false" at that moment, it looks like the status is overridden
  3. After the data is polled again, original change is shown as it should be as the API has fresh data
Developer Thing
  • 2,704
  • 3
  • 21
  • 26

0 Answers0