DD.
DD.

Reputation: 21971

GWT single threaded callbacks

positionService.call(new PositionCallback(){ onPositionUpdate(Position position)
{
   this.position=position;
   if (isLoaded){
      refreshDataWithPosition();
   }
});

dataService.call(new DataCallback(){ onDataUpdate(Data data)
{
   //does this need synchronization?
   updateTable(data,position);
   isLoaded=true;
});

I have the following code above. It loads the GPS geolocation position and all some data at the same time. If the GPS data updates first updateTable will update taking this into account. If the GPS data updates second it will call refreshDataWithPosition to refresh its contents. I want to updateTable with or without position for a more responsive experience.

How does the GWT async callback work with single threading (or possibly a multithreaded javascript engine)? The danger here is the position callback gets called after updateTable finishes but before isLoaded is set to true. In this scenario the table won't be refreshed with the position data.

Upvotes: 0

Views: 435

Answers (1)

ArtemGr
ArtemGr

Reputation: 12547

GWT behaviour there isn't any different from the JavaScript behaviour. The events are handled only when the control flow is realeased by the JavaScript code. Do an infinite cycle for(;;); and no events will be handled after that, because the control is never released.

The danger here is the position callback gets called after updateTable finishes but before isLoaded is set to true. In this scenario the table won't be refreshed with the position data.

There's no danger as long as the updateTable is synchronous (e.g. doesn't use callbacks internally).

P.S. Another option for the updateTable to happen after refreshDataWithPosition is to save the data from both sources and call a function which works when both are present:

source1callback (position) {
  this.position = position;
  join();
}
source2callback (data) {
  this.data = data;
  join();
}
join() {
  if (this.position && this.data) updateTable (this.data, this.position)
}

Upvotes: 2

Related Questions