[Search] Don't block UI between requests

Timeout subsequent calls to keepIndexing at the end of a
indexRequest, so that UI operations are not blocked.
This commit is contained in:
Pete Richards 2015-10-21 07:39:59 -07:00
parent 9a63e99710
commit 833f57e284
2 changed files with 35 additions and 19 deletions

View File

@ -19,7 +19,7 @@
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define*/
/*global define,setTimeout*/
/**
* Module defining GenericSearchProvider. Created by shale on 07/16/2015.
@ -62,6 +62,8 @@ define([
ROOTS.forEach(function indexRoot(rootId) {
provider.scheduleForIndexing(rootId);
});
}
/**
@ -148,8 +150,10 @@ define([
* @private
*/
GenericSearchProvider.prototype.keepIndexing = function () {
if (this.pendingRequests < this.MAX_CONCURRENT_REQUESTS) {
this.beginIndexRequest();
while (this.pendingRequests < this.MAX_CONCURRENT_REQUESTS &&
this.idsToIndex.length
) {
this.beginIndexRequest();
}
};
@ -188,10 +192,6 @@ define([
var idToIndex = this.idsToIndex.shift(),
provider = this;
if (!idToIndex) {
return;
}
this.pendingRequests += 1;
this.modelService
.getModels([idToIndex])
@ -206,8 +206,10 @@ define([
.warn('Failed to index domain object ' + idToIndex);
})
.then(function () {
provider.pendingRequests -= 1;
provider.keepIndexing();
setTimeout(function () {
provider.pendingRequests -= 1;
provider.keepIndexing();
}, 0);
});
};
@ -235,7 +237,6 @@ define([
};
});
pendingQuery.resolve(modelResults);
delete this.pendingQueries[event.data.queryId];
};

View File

@ -143,17 +143,38 @@ define([
});
describe('keepIndexing', function () {
it('kicks off an index request when not at maximum', function () {
spyOn(provider, 'beginIndexRequest');
provider.pendingRequests = 0;
it('calls beginIndexRequest until at maximum', function () {
spyOn(provider, 'beginIndexRequest').andCallThrough();
provider.pendingRequests = 9;
provider.idsToIndex = ['a', 'b', 'c'];
provider.MAX_CONCURRENT_REQUESTS = 10;
provider.keepIndexing();
expect(provider.beginIndexRequest).toHaveBeenCalled();
expect(provider.beginIndexRequest.calls.length).toBe(1);
});
it('calls beginIndexRequest for all ids to index', function () {
spyOn(provider, 'beginIndexRequest').andCallThrough();
provider.pendingRequests = 0;
provider.idsToIndex = ['a', 'b', 'c'];
provider.MAX_CONCURRENT_REQUESTS = 10;
provider.keepIndexing();
expect(provider.beginIndexRequest).toHaveBeenCalled();
expect(provider.beginIndexRequest.calls.length).toBe(3);
});
it('does not index when at capacity', function () {
spyOn(provider, 'beginIndexRequest');
provider.pendingRequests = 10;
provider.idsToIndex.push('a');
provider.MAX_CONCURRENT_REQUESTS = 10;
provider.keepIndexing();
expect(provider.beginIndexRequest).not.toHaveBeenCalled();
});
it('does not index when no ids to index', function () {
spyOn(provider, 'beginIndexRequest');
provider.pendingRequests = 0;
provider.MAX_CONCURRENT_REQUESTS = 10;
provider.keepIndexing();
expect(provider.beginIndexRequest).not.toHaveBeenCalled();
@ -222,12 +243,6 @@ define([
});
});
it('does not error if no objects queued', function () {
provider.idsToIndex = [];
expect(function () {
provider.beginIndexRequest();
}).not.toThrow();
});
});