Commit 6e537c98 authored by tetsui's avatar tetsui Committed by Commit bot

Reuse FileTasks when entries are not changed.

fileManagerPrivate.getFileTasks is slow.
However, every time a file is copied during multi file copy, the method
was called via CHANGE_THROTTLED event listener.
This CL stops creating FileTasks again when file entries for the
previous FileTasks are same as the current ones.
Other possible solution to this problem is to change C++ interface of
fileManagerPrivate.getFileTasks to support incremental update of
FileTasks.

BUG=462989,712121
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2833413003
Cr-Commit-Position: refs/heads/master@{#467264}
parent ae42f270
......@@ -682,6 +682,28 @@ util.isSameEntry = function(entry1, entry2) {
return entry1.toURL() === entry2.toURL();
};
/**
* Compares two entry arrays.
* @param {Array<!Entry>} entries1 The entry array to be compared.
* @param {Array<!Entry>} entries2 The entry array to be compared.
* @return {boolean} True if the both arrays contain same files or directories
* in the same order. Returns true if both arrays are null.
*/
util.isSameEntries = function(entries1, entries2) {
if (!entries1 && !entries2)
return true;
if (!entries1 || !entries2)
return false;
if (entries1.length !== entries2.length)
return false;
for (var i = 0; i < entries1.length; i++) {
if (!util.isSameEntry(entries1[i], entries2[i])) {
return false;
}
}
return true;
};
/**
* Compares two file systems.
* @param {FileSystem} fileSystem1 The file system to be compared.
......
......@@ -68,6 +68,15 @@ function FileTasks(volumeManager, metadataModel, directoryModel, ui, entries,
this.defaultTask_ = defaultTask;
};
FileTasks.prototype = {
/**
* @return {!Array<!Entry>}
*/
get entries() {
return this.entries_;
}
};
/**
* The app ID of the video player app.
* @const
......
......@@ -262,7 +262,6 @@ TaskController.prototype.getMimeType_ = function(entry) {
* @private
*/
TaskController.prototype.onSelectionChanged_ = function() {
this.tasks_ = null;
var selection = this.selectionHandler_.selection;
// Caller of update context menu task items.
// FileSelectionHandler.EventType.CHANGE
......@@ -286,15 +285,36 @@ TaskController.prototype.updateTasks_ = function() {
var selection = this.selectionHandler_.selection;
if (this.dialogType_ === DialogType.FULL_PAGE &&
(selection.directoryCount > 0 || selection.fileCount > 0)) {
this.getFileTasks()
.then(function(tasks) {
tasks.display(this.ui_.taskMenuButton);
this.updateContextMenuTaskItems_(tasks.getTaskItems());
}.bind(this))
.catch(function(error) {
if (error)
console.error(error.stack || error);
});
var previousFileTasksInvalidatePromise = new Promise(function(resolve) {
if (this.tasks_) {
this.getFileTasks()
.then(function(tasks) {
if (!util.isSameEntries(tasks.entries, selection.entries)) {
this.tasks_ = null;
}
resolve();
}.bind(this))
.catch(function(error) {
if (error)
console.log(error.stack || error);
this.tasks_ = null;
resolve();
}.bind(this));
} else {
resolve();
}
}.bind(this));
previousFileTasksInvalidatePromise.then(function() {
this.getFileTasks()
.then(function(tasks) {
tasks.display(this.ui_.taskMenuButton);
this.updateContextMenuTaskItems_(tasks.getTaskItems());
}.bind(this))
.catch(function(error) {
if (error)
console.error(error.stack || error);
});
}.bind(this));
} else {
this.ui_.taskMenuButton.hidden = true;
}
......@@ -309,19 +329,21 @@ TaskController.prototype.getFileTasks = function() {
return this.tasks_;
var selection = this.selectionHandler_.selection;
return selection.computeAdditional(this.metadataModel_).then(
function() {
if (this.selectionHandler_.selection !== selection)
return Promise.reject();
return FileTasks.create(
this.volumeManager_, this.metadataModel_, this.directoryModel_,
this.ui_, selection.entries, assert(selection.mimeTypes)).
then(function(tasks) {
if (this.selectionHandler_.selection !== selection)
return Promise.reject();
return tasks;
}.bind(this));
}.bind(this));
return this.tasks_ =
selection.computeAdditional(this.metadataModel_).then(function() {
if (this.selectionHandler_.selection !== selection)
return Promise.reject();
return FileTasks
.create(
this.volumeManager_, this.metadataModel_,
this.directoryModel_, this.ui_, selection.entries,
assert(selection.mimeTypes))
.then(function(tasks) {
if (this.selectionHandler_.selection !== selection)
return Promise.reject();
return tasks;
}.bind(this));
}.bind(this));
};
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment