Skip to content

Commit

Permalink
improve the dragging performance of the Simile timeline by using thro…
Browse files Browse the repository at this point in the history
…ttling
  • Loading branch information
Bart van den Eijnden committed Aug 3, 2012
1 parent ab45c75 commit e70a221
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
64 changes: 64 additions & 0 deletions src/script/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,70 @@ gxp.util = {
return a.href;
},

/** api: function[throttle]
* :arg func: ``Function``
* :arg wait: ``Integer``
* :return: ``Function``
*
* Returns a function, that, when invoked, will only be triggered at
* most once during a given window of time.
*/
throttle: (function() {

This comment has been minimized.

Copy link
@mpriour

mpriour Aug 3, 2012

Contributor

throttling is already implemented in the ExtJs library via the Ext.util.DelayedTask class.
You could replace this with:
throttle: function(func, wait){
var task = new Ext.util.DelayedTask(func);
var f = function(){
task.delay(wait);
};
return f;
}

This comment has been minimized.

Copy link
@bartvde

bartvde via email Aug 3, 2012

Contributor

This comment has been minimized.

Copy link
@bartvde

bartvde Aug 6, 2012

Contributor

@mpriour I was surprised to see that Ext 4 has created a createThrottled function which does not use Ext.util.DelayedTask:

http://all-docs.info/extjs4/docs/source/Function.html

// Underscore.js 1.3.3
// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
// Underscore is freely distributable under the MIT license.
// Portions of Underscore are inspired or borrowed from Prototype,
// Oliver Steele's Functional, and John Resig's Micro-Templating.
// For all details and documentation:
// http://documentcloud.github.com/underscore

// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
var debounce = function(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
if (immediate && !timeout) func.apply(context, args);
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
};

// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time.
var throttle = function(func, wait) {
var context, args, timeout, throttling, more, result;
var whenDone = debounce(function(){ more = throttling = false; }, wait);
return function() {
context = this; args = arguments;
var later = function() {
timeout = null;
if (more) func.apply(context, args);
whenDone();
};
if (!timeout) timeout = setTimeout(later, wait);
if (throttling) {
more = true;
} else {
result = func.apply(context, args);
}
whenDone();
throttling = true;
return result;
};
};
return function(func, wait) {
return throttle(func, wait);
};
})(),

/** api: function[md5]
* :arg data: ``String``
* :returns: ``String`` md5 hash
Expand Down
8 changes: 7 additions & 1 deletion src/script/widgets/TimelinePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ window.Timeline && window.SimileAjax && (function() {
*/
gxp.TimelinePanel = Ext.extend(Ext.Panel, {

/** api: config[scrollInterval]
* ``Integer`` The Simile scroll event listener will only be handled
* upon every scrollInterval milliseconds. Defaults to 500.
*/
scrollInterval: 500,

/** api: config[featureEditor]
* ``gxp.plugins.FeatureEditor``
*/
Expand Down Expand Up @@ -806,7 +812,7 @@ gxp.TimelinePanel = Ext.extend(Ext.Panel, {
// since the bands are linked we need to listen to one band only
this._silent = true;
this.timeline.getBand(0).addOnScrollListener(
this.setPlaybackCenter.createDelegate(this)
gxp.util.throttle(this.setPlaybackCenter.createDelegate(this), this.scrollInterval)
);

},
Expand Down

1 comment on commit e70a221

@mpriour
Copy link
Contributor

@mpriour mpriour commented on e70a221 Aug 3, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExtJS already has a 'throttle' concept built into its event listeners and the Ext.util.DelayedTask class. You could rewrite the throttle function as shown above as a convenience function or you could add a 'timelinescroll' event to the timeline panel and just fire that event as a result of the Simile timeline scroll. Then after the ...addOnScrollListener call, you could add setPlaybackCenter as the listener for the 'timelinescroll' event with the buffer option set to this.scrollInterval

Please sign in to comment.