Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

asyncCallback sporadically fails to report events #129

Open
hiattp opened this issue Jun 7, 2014 · 2 comments
Open

asyncCallback sporadically fails to report events #129

hiattp opened this issue Jun 7, 2014 · 2 comments

Comments

@hiattp
Copy link

hiattp commented Jun 7, 2014

I haven't delved into this too deeply beyond observing the effect, but the timeout strategy behind the event callback is dropping events (maybe 1 out of 10 times or so). Given the nature of the issue it is relatively hard to reproduce, but try to observe "start" events (e.g. "drag start") across a series of drags and every once in a while it won't appear when you start dragging.

My guess is that events are stacking up and clearing previous events' timeouts (e.g. an "apply" coming on the back of a "drag start" will clear the "drag start" timeout so that it never calls the callback).

@jwarren1980
Copy link

Your guess is correct, and I had to fight over a similar issue before.

The current code for FreeTransform will call your callback event and send status of any recent events that occurred. But if two events occur close to one another, the second event will cancel out the call of the first event before it has had a chance to be fired. The reason is because each event triggers a call to your callback function through a setTimeout method, but each event will first clear any existing timeouts triggered from before. As browsers and CPUs get faster and support more multi-threading, it is possible to have two events triggered within 1ms of each other which makes the current code fail on tracking all events in order.

The solution I used, which works for handling all events as they are called, in order, is to replace the function asyncCallback with the one below:

/**
 * Call callback asynchronously for better performance
 */
function asyncCallback(e)
{
     if (ft.callback)
     {
          // Remove empty values
          var events = [];

          e.map(function (e, i) { if (e) { events.push(e); } });

          //clearTimeout(timeout);

          //timeout = setTimeout(function () { if (ft.callback) { ft.callback(ft, events); } }, 1);

          if (ft.callback) { ft.callback(ft, events); }
     }
}

@hiattp
Copy link
Author

hiattp commented Aug 13, 2014

Yeah that looks like it would work, thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants