[Search] Break up item indexing

Use timeouts to make the generic search's item
indexing not block up the page.
Updated tests accordingly.
This commit is contained in:
slhale 2015-08-12 10:06:27 -07:00
parent 9939c809be
commit 9dd520f17b
3 changed files with 30 additions and 16 deletions

View File

@ -39,7 +39,7 @@
"provides": "searchService", "provides": "searchService",
"type": "provider", "type": "provider",
"implementation": "services/GenericSearchProvider.js", "implementation": "services/GenericSearchProvider.js",
"depends": [ "$q", "objectService", "workerService", "GENERIC_SEARCH_ROOTS" ] "depends": [ "$q", "$timeout", "objectService", "workerService", "GENERIC_SEARCH_ROOTS" ]
}, },
{ {
"provides": "searchService", "provides": "searchService",

View File

@ -39,6 +39,7 @@ define(
* *
* @constructor * @constructor
* @param $q Angular's $q, for promise consolidation. * @param $q Angular's $q, for promise consolidation.
* @param $timeout Angular's $timeout, for delayed funtion execution.
* @param {ObjectService} objectService The service from which * @param {ObjectService} objectService The service from which
* domain objects can be gotten. * domain objects can be gotten.
* @param {WorkerService} workerService The service which allows * @param {WorkerService} workerService The service which allows
@ -46,7 +47,7 @@ define(
* @param {GENERIC_SEARCH_ROOTS} ROOTS An array of the root * @param {GENERIC_SEARCH_ROOTS} ROOTS An array of the root
* domain objects' IDs. * domain objects' IDs.
*/ */
function GenericSearchProvider($q, objectService, workerService, ROOTS) { function GenericSearchProvider($q, $timeout, objectService, workerService, ROOTS) {
var worker = workerService.run('genericSearchWorker'), var worker = workerService.run('genericSearchWorker'),
indexed = {}, indexed = {},
pendingQueries = {}; pendingQueries = {};
@ -132,16 +133,23 @@ define(
indexItem(node); indexItem(node);
indexed[id] = true; indexed[id] = true;
if (node.hasCapability && node.hasCapability('composition')) { if (node && node.hasCapability && node.hasCapability('composition')) {
// This node has children // This node has children
node.getCapability('composition').invoke().then(function (children) {
// Index the children // Make sure that this is async, so doesn't block up page
if (children.constructor === Array) { $timeout(function () {
indexItems(children); // Get the children...
} else { node.useCapability('composition').then(function (children) {
indexItems([children]); $timeout(function () {
} // ... then index the children
}); if (children.constructor === Array) {
indexItems(children);
} else {
indexItems([children]);
}
}, 100);
});
}, 100);
} }
} }

View File

@ -31,6 +31,7 @@ define(
describe("The generic search provider ", function () { describe("The generic search provider ", function () {
var mockQ, var mockQ,
mockTimeout,
mockDeferred, mockDeferred,
mockObjectService, mockObjectService,
mockObjectPromise, mockObjectPromise,
@ -57,6 +58,8 @@ define(
mockDeferred.promise = "mock promise"; mockDeferred.promise = "mock promise";
mockQ.defer.andReturn(mockDeferred); mockQ.defer.andReturn(mockDeferred);
mockTimeout = jasmine.createSpy("$timeout");
mockObjectService = jasmine.createSpyObj( mockObjectService = jasmine.createSpyObj(
"objectService", "objectService",
[ "getObjects" ] [ "getObjects" ]
@ -83,7 +86,7 @@ define(
mockDomainObjects[i] = ( mockDomainObjects[i] = (
jasmine.createSpyObj( jasmine.createSpyObj(
"domainObject", "domainObject",
[ "getId", "getModel", "hasCapability", "getCapability" ] [ "getId", "getModel", "hasCapability", "getCapability", "useCapability" ]
) )
); );
mockDomainObjects[i].getId.andReturn(i); mockDomainObjects[i].getId.andReturn(i);
@ -102,14 +105,17 @@ define(
mockCapability.invoke.andReturn(mockCapabilityPromise); mockCapability.invoke.andReturn(mockCapabilityPromise);
mockDomainObjects[0].getCapability.andReturn(mockCapability); mockDomainObjects[0].getCapability.andReturn(mockCapability);
provider = new GenericSearchProvider(mockQ, mockObjectService, mockWorkerService, mockRoots); provider = new GenericSearchProvider(mockQ, mockTimeout, mockObjectService, mockWorkerService, mockRoots);
}); });
it("indexes tree on initialization", function () { it("indexes tree on initialization", function () {
mockObjectPromise.then.mostRecentCall.args[0](mockDomainObjects);
mockCapabilityPromise.then.mostRecentCall.args[0](mockDomainObjects[1]);
expect(mockObjectService.getObjects).toHaveBeenCalled(); expect(mockObjectService.getObjects).toHaveBeenCalled();
expect(mockObjectPromise.then).toHaveBeenCalled();
mockObjectPromise.then.mostRecentCall.args[0](mockDomainObjects);
//mockCapabilityPromise.then.mostRecentCall.args[0](mockDomainObjects[1]);
expect(mockWorker.postMessage).toHaveBeenCalled(); expect(mockWorker.postMessage).toHaveBeenCalled();
}); });