Index: chrome/browser/resources/file_manager/js/file_manager.js |
diff --git a/chrome/browser/resources/file_manager/js/file_manager.js b/chrome/browser/resources/file_manager/js/file_manager.js |
index e4e044ed8d5a468c8935a5d7f47988e9ea5969f6..3f514e55f7f3ff8381d7aa6b037154b557f0a3b1 100644 |
--- a/chrome/browser/resources/file_manager/js/file_manager.js |
+++ b/chrome/browser/resources/file_manager/js/file_manager.js |
@@ -1794,7 +1794,7 @@ FileManager.prototype = { |
if (event.status == 'success' || |
event.status == 'error_unknown_filesystem' || |
event.status == 'error_unsuported_filesystem') |
- self.rescanDirectory_(); |
+ self.rescanDirectory_(undefined, 300); |
rginda
2011/08/25 00:29:09
In js, "undefined" just happens to be a variable t
sidor
2011/08/25 01:09:58
Done.
|
}); |
}; |
@@ -2522,12 +2522,59 @@ FileManager.prototype = { |
}; |
/** |
- * Rescan the current directory, refreshing the list. |
+ * Rescans the current directory, refreshing the list. It decreases the |
+ * probability that two such calls are pending simultaneously. |
* |
* @param {function()} opt_callback Optional function to invoke when the |
* rescan is complete. |
+ * @param {string} delayMS Delay during which next rescanDirectory calls |
+ * can happen. |
*/ |
- FileManager.prototype.rescanDirectory_ = function(opt_callback) { |
+ |
+ FileManager.prototype.rescanDirectory_ = function(opt_callback, delayMS) { |
+ function done(count) { |
+ // Variable count is introduced because we only want to do callbacks, that |
+ // were in the queue at the moment of rescanDirectoryNow invocation. |
+ while (count--) { |
+ var callback = this.rescanDirectory_.callbacks.shift(); |
rginda
2011/08/25 00:29:09
s/shift/pop/
push/pop work from the end of the ar
sidor
2011/08/25 01:09:58
Hmm.. Nope. I did it on purpose and I really think
|
+ callback(); |
+ } |
+ } |
+ |
+ if (!this.rescanDirectory_) |
+ this.rescanDirectory_ = []; |
rginda
2011/08/25 00:29:09
this.rescanDirectory_ is the function we're in. T
sidor
2011/08/25 01:09:58
Done.
|
+ |
+ if (!this.rescanDirectory_.callbacks) |
+ this.rescanDirectory_.callbacks = []; |
+ |
+ if (opt_callback) |
+ this.rescanDirectory_.callbacks.push(opt_callback); |
+ |
+ if (!delayMS) |
+ var delayMS = 100; |
rginda
2011/08/25 00:29:09
No need for var here. delayMS is already declared
sidor
2011/08/25 01:09:58
That's kind of cool idiom :)
|
+ |
+ // Assumes rescanDirectoryNow_ takes less than 100 ms. If not there is |
+ // a possible overlap between two calls. |
rginda
2011/08/25 00:29:09
I don't think this comment (or the minimum 100ms p
sidor
2011/08/25 01:09:58
No, no this is not what I mean there. Here's what
|
+ if (delayMS < 100) |
+ delayMS = 100; |
+ |
+ if (this.rescanDirectory_.handle) |
+ clearTimeout(this.rescanDirectory_.handle); |
+ var self = this; |
+ this.rescanDirectory_.handle = setTimeout(function () { |
+ self.rescanDirectoryNow_(done.bind(self, |
rginda
2011/08/25 00:29:09
done already has access to self, there is no need
sidor
2011/08/25 01:09:58
OK, but I still need to pass and argument to the f
|
+ self.rescanDirectory_.callbacks.length)); |
+ }, delayMS); |
+ }; |
+ |
+ /** |
+ * Rescans the current directory immediately, refreshing the list. Should NOT |
+ * be used in most cases. Instead use rescanDirectory_. |
+ * |
+ * @param {function()} opt_callback Optional function to invoke when the |
+ * rescan is complete. |
+ */ |
+ FileManager.prototype.rescanDirectoryNow_ = function(opt_callback) { |
var self = this; |
var reader; |