Chromium Code Reviews| Index: chrome/browser/resources/settings/search_settings.js |
| diff --git a/chrome/browser/resources/settings/search_settings.js b/chrome/browser/resources/settings/search_settings.js |
| index 78960bbf4448944de9d990a8bc82a03f3ce7f95a..195155da8d98c4b5b63d59fbf5c9666991fedfea 100644 |
| --- a/chrome/browser/resources/settings/search_settings.js |
| +++ b/chrome/browser/resources/settings/search_settings.js |
| @@ -125,8 +125,7 @@ cr.define('settings', function() { |
| function doSearch(node) { |
| if (node.nodeName == 'TEMPLATE' && node.hasAttribute('route-path') && |
| !node.if && !node.hasAttribute(SKIP_SEARCH_CSS_ATTRIBUTE)) { |
| - getSearchManager().queue_.addRenderTask( |
| - new RenderTask(request, node)); |
| + request.queue_.addRenderTask(new RenderTask(request, node)); |
| return; |
| } |
| @@ -304,7 +303,7 @@ cr.define('settings', function() { |
| parent.querySelector('[route-path="' + routePath + '"]'); |
| // Register a SearchAndHighlightTask for the part of the DOM that was |
| // just rendered. |
| - getSearchManager().queue_.addSearchAndHighlightTask( |
| + this.request.queue_.addSearchAndHighlightTask( |
|
Dan Beam
2017/03/14 00:44:27
should this be this.request_?
dpapad
2017/03/14 03:28:11
This is a protected member variable from the super
|
| new SearchAndHighlightTask(this.request, assert(renderedNode))); |
| resolve(); |
| }.bind(this)); |
| @@ -372,8 +371,12 @@ cr.define('settings', function() { |
| /** |
| * @constructor |
| + * @param {!settings.SearchRequest} request |
| */ |
| - function TaskQueue() { |
| + function TaskQueue(request) { |
| + /** @private {!settings.SearchRequest} */ |
| + this.request_ = request; |
| + |
| /** |
| * @private {{ |
| * high: !Array<!Task>, |
| @@ -452,18 +455,14 @@ cr.define('settings', function() { |
| this.running_ = true; |
| window.requestIdleCallback(function() { |
| - function startNextTask() { |
| - this.running_ = false; |
| - this.consumePending_(); |
| - } |
| - if (task.request.id == |
| - getSearchManager().activeRequest_.id) { |
| - task.exec().then(startNextTask.bind(this)); |
| - } else { |
| - // Dropping this task without ever executing it, since a new search |
| - // has been issued since this task was queued. |
| - startNextTask.call(this); |
| + if (!this.request_.canceled) { |
| + task.exec().then(function() { |
| + this.running_ = false; |
| + this.consumePending_(); |
| + }.bind(this)); |
| } |
| + // Nothing to do otherwise. Since the request corresponding to this |
| + // queue was canceled, the queue is disposed along with the request. |
| }.bind(this)); |
| return; |
| } |
| @@ -472,38 +471,52 @@ cr.define('settings', function() { |
| /** |
| * @constructor |
| + * |
| + * @param {string} rawQuery |
| + * @param {!HTMLElement} root |
| */ |
| - var SearchRequest = function(rawQuery) { |
| - /** @type {number} */ |
| - this.id = SearchRequest.nextId_++; |
| - |
| + var SearchRequest = function(rawQuery, root) { |
| /** @private {string} */ |
| this.rawQuery_ = rawQuery; |
| + /** @private {!HTMLElement} */ |
| + this.root = root; |
|
Dan Beam
2017/03/14 00:44:27
if this is private, root_
dpapad
2017/03/14 03:28:11
Done.
|
| + |
| /** @type {?RegExp} */ |
| this.regExp = this.generateRegExp_(); |
| /** |
| - * Whether this request was fully processed. |
| + * Whether this request was canceled before completing. |
| * @type {boolean} |
| */ |
| - this.finished = false; |
| + this.canceled = false; |
| /** @private {boolean} */ |
| this.foundMatches_ = false; |
| /** @type {!PromiseResolver} */ |
| this.resolver = new PromiseResolver(); |
| - }; |
| - /** @private {number} */ |
| - SearchRequest.nextId_ = 0; |
| + /** @private {!TaskQueue} */ |
| + this.queue_ = new TaskQueue(this); |
| + this.queue_.onEmpty(function() { |
| + this.resolver.resolve(this); |
| + }.bind(this)); |
| + }; |
| /** @private {!RegExp} */ |
| SearchRequest.SANITIZE_REGEX_ = /[-[\]{}()*+?.,\\^$|#\s]/g; |
| SearchRequest.prototype = { |
| /** |
| + * Fires this search request. |
| + */ |
| + start: function() { |
| + this.queue_.addTopLevelSearchTask( |
| + new TopLevelSearchTask(this, this.root)); |
| + }, |
| + |
| + /** |
| * @return {?RegExp} |
| * @private |
| */ |
| @@ -561,37 +574,36 @@ cr.define('settings', function() { |
| * @implements {SearchManager} |
| */ |
| var SearchManagerImpl = function() { |
| - /** @private {?settings.SearchRequest} */ |
| - this.activeRequest_ = null; |
| + /** @private {!Set<!settings.SearchRequest>} */ |
| + this.activeRequests_ = new Set(); |
| - /** @private {!TaskQueue} */ |
| - this.queue_ = new TaskQueue(); |
| - this.queue_.onEmpty(function() { |
| - this.activeRequest_.finished = true; |
| - this.activeRequest_.resolver.resolve(this.activeRequest_); |
| - this.activeRequest_ = null; |
| - }.bind(this)); |
| + /** @private {?string} */ |
| + this.lastSearchedText_ = null; |
| }; |
| cr.addSingletonGetter(SearchManagerImpl); |
| SearchManagerImpl.prototype = { |
| /** @override */ |
| search: function(text, page) { |
| - // Creating a new request only if the |text| changed. |
| - if (!this.activeRequest_ || !this.activeRequest_.isSame(text)) { |
| - // Resolving previous search request without marking it as |
| - // 'finished', if any, and dropping all pending tasks. |
| - this.queue_.reset(); |
| - if (this.activeRequest_) |
| - this.activeRequest_.resolver.resolve(this.activeRequest_); |
| - |
| - this.activeRequest_ = new SearchRequest(text); |
| + // Cancel any pending requests if a request with different text is |
| + // submitted. |
| + if (text != this.lastSearchedText_) { |
| + this.activeRequests_.forEach(function(request) { |
| + request.canceled = true; |
| + request.resolver.resolve(request); |
| + }); |
| + this.activeRequests_.clear(); |
| } |
| - this.queue_.addTopLevelSearchTask( |
| - new TopLevelSearchTask(this.activeRequest_, page)); |
| - |
| - return this.activeRequest_.resolver.promise; |
| + this.lastSearchedText_ = text; |
| + var request = new SearchRequest(text, page); |
| + this.activeRequests_.add(request); |
| + request.start(); |
| + return request.resolver.promise.then(function() { |
| + // Stop tracking requests that finished. |
| + this.activeRequests_.delete(request); |
| + return request; |
| + }.bind(this)); |
| }, |
| }; |