0
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.

DD.
  • 21,498
  • 52
  • 157
  • 246

1 Answers1

2

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)
}
ArtemGr
  • 11,684
  • 3
  • 52
  • 85
  • Its not really what I want though. I want to load first with or without position to have better responsiveness. If position comes in later I want to refresh the data. – DD. Apr 04 '13 at 21:20
  • Also according to this http://stackoverflow.com/questions/9999056/are-event-handlers-guaranteed-to-complete-before-ajax-callbacks-are-invoked the ajax callback will never interrupt the JS code so synchronization shouldnt be an issue in single threaded JS engines – DD. Apr 04 '13 at 21:29
  • 1
    "I want to load first with or without position to have better responsiveness" - so call updateTable in source2callback, then in join; "the ajax callback will never interrupt the JS code" - that's what I said. – ArtemGr Apr 04 '13 at 21:37
  • I said that the event won't be handled until the JS code finishes. – ArtemGr Apr 04 '13 at 22:39
  • You also said "this might happen sure". That can't happen as the callback cannot interrupt in between those two lines. – DD. Apr 05 '13 at 07:06
  • DD, "this might happen, sure", because onDataUpdate can be called before the onPositionUpdate. Isn't that obvious? – ArtemGr Apr 05 '13 at 16:02
  • If you read my question more carefully I asked if the position callback can run after updateTable finishes but before isLoaded is set to true. This is impossible. Once updateTable runs isLoaded is guaranteed to run. So your answer is incorrect. – DD. Apr 05 '13 at 17:09