Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Unified Diff: chrome/browser/resources/file_manager/js/async_util.js

Issue 15809008: Make resizing in Files.app more responsive. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/file_manager/js/file_grid.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/file_manager/js/async_util.js
diff --git a/chrome/browser/resources/file_manager/js/async_util.js b/chrome/browser/resources/file_manager/js/async_util.js
index 47bc684f878e82d1348bba8ea70c90ce205c49ce..59199eb86c8089e6c7aa0a594b145817943d5816 100644
--- a/chrome/browser/resources/file_manager/js/async_util.js
+++ b/chrome/browser/resources/file_manager/js/async_util.js
@@ -137,3 +137,79 @@ AsyncUtil.Group.prototype.finish_ = function(task) {
this.finishedTasks_[task.name] = task;
this.continue_();
};
+
+/**
+ * Aggregates consecutive calls and executes the closure only once instead of
+ * several times. The first call is always called immediately, and the next
+ * consecutive ones are aggregated and the closure is called only once once
+ * |delay| amount of time passes after the last call to run().
+ *
+ * @param {function()} closure Closure to be aggregated.
+ * @param {number=} opt_delay Minimum aggregation time in milliseconds. Default
+ * is 50 milliseconds.
+ * @constructor
+ */
+AsyncUtil.Aggregation = function(closure, opt_delay) {
+ /**
+ * @type {number}
+ * @private
+ */
+ this.delay_ = opt_delay || 50;
+
+ /**
+ * @type {function()}
+ * @private
+ */
+ this.closure_ = closure;
+
+ /**
+ * @type {number?}
+ * @private
+ */
+ this.scheduledRunsTimer_ = null;
+
+ /**
+ * @type {number}
+ * @private
+ */
+ this.lastRunTime_ = 0;
+};
+
+/**
+ * Runs a closure. Skips consecutive calls. The first call is called
+ * immediately.
+ */
+AsyncUtil.Aggregation.prototype.run = function() {
+ // If recently called, then schedule the consecutive call with a delay.
+ if (Date.now() - this.lastRunTime_ < this.delay_) {
+ this.cancelScheduledRuns_();
+ this.scheduledRunsTimer_ = setTimeout(this.runImmediately_.bind(this),
+ this.delay_ + 1);
+ this.lastRunTime_ = Date.now();
+ return;
+ }
+
+ // Otherwise, run immediately.
+ this.runImmediately_();
+};
+
+/**
+ * Calls the schedule immediately and cancels any scheduled calls.
+ * @private
+ */
+AsyncUtil.Aggregation.prototype.runImmediately_ = function() {
+ this.cancelScheduledRuns_();
+ this.closure_();
+ this.lastRunTime_ = Date.now();
+};
+
+/**
+ * Cancels all scheduled runs (if any).
+ * @private
+ */
+AsyncUtil.Aggregation.prototype.cancelScheduledRuns_ = function() {
+ if (this.scheduledRunsTimer_) {
+ clearTimeout(this.scheduledRunsTimer_);
+ this.scheduledRunsTimer_ = null;
+ }
+};
« no previous file with comments | « no previous file | chrome/browser/resources/file_manager/js/file_grid.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698