google-code-export / esprima

Automatically exported from code.google.com/p/esprima
BSD 2-Clause "Simplified" License
1 stars 0 forks source link

Debouncing Errors for Esprima Demos #592

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The "Changed" handlers for parse, validate, and highlight demos are attached 
incorrectly for 
the demos.

The general layout of these handlers is 
var handler = function(delay) {
    // ...
    if (timeoutID) {
        clearTimeout(timeoutID);
    }
    timeoutID = setTimeout(function(){
    }, delay || 811);
    timeoutID = undefined;
    // ...
};

The event handlers are being attached like

addEventListener(handler),

but should be attached 
like so 

addEventListener(function(event){ handler(); });

The respective handlers parse, validate, and triggerParse expect a delay value,
but are currently being invoked with an event object passed from the orion 
editor.

As  a result, when setTimeout(s) is invoked, their timer/callback functions
are also invoked  almost immediately because the delay value is parsed 
incorrectly (delay value should
be integer instead of event object). If you set a breakpoint on clearTimeout,
you'll notice that it's almost never hit.

I believe the way the code was written the intention was to have the delay value
default to the original value (811) if not provided and use JavaScript 
debouncing
(http://unscriptable.com/2009/03/20/debouncing-javascript-methods/),
so that only one signal is sent for an event that may be happening hundred of 
times within a certain period.

What this means, is that when a user types, the setTimeout callbacks should  be 
 invoked
only after a certain period of idle activity.
Currently the timer functions are invoked after every change, which is very 
taxing on the CPU.

Original issue reported on code.google.com by anthon...@radialglo.com on 18 Sep 2014 at 11:28

GoogleCodeExporter commented 9 years ago
ref: https://github.com/ariya/esprima/pull/284

Original comment by anthon...@radialglo.com on 18 Sep 2014 at 11:43

GoogleCodeExporter commented 9 years ago
* Small correction for issue reported *
The general layout of these handlers is 
var handler = function(delay) {
    // ...
    if (timeoutID) {
        clearTimeout(timeoutID);
    }
    timeoutID = setTimeout(function(){
          timeoutID = undefined;
    }, delay || 811);

    // ...
};

Original comment by anthon...@radialglo.com on 18 Sep 2014 at 11:49

GoogleCodeExporter commented 9 years ago
Issue 592: Debouncing Errors for Esprima Demos
https://github.com/ariya/esprima/commit/1cf6132cdf

Original comment by ariya.hi...@gmail.com on 27 Jan 2015 at 4:12