[Search] Cleaning up

Cleaning up code in generic serach provider. Removed
unnecissary return statements. Moved indexing via web
worker from getItems() loop to within itemsHelper().
Commented out timeout, becuase seems unnecissary.
Changed all date.getTime() to Date.now(). Indexing
has undefined checking.
This commit is contained in:
slhale 2015-07-29 10:59:58 -07:00
parent 639cf1cf59
commit f2cd55d6cd

View File

@ -51,14 +51,19 @@ define(
// Tell the web worker to add a new item's model to its list of items. // Tell the web worker to add a new item's model to its list of items.
function indexItem(domainObject) { function indexItem(domainObject) {
var message = { var message;
request: 'index',
model: domainObject.getModel(), // undefined check
id: domainObject.getId() if (domainObject && domainObject.getModel) {
}; // Using model instead of whole domain object because
// Note that getModel() by definition returns a JavaScript object // it's a JSON object.
// that can be losslesly converted to a JSON object. message = {
worker.postMessage(message); request: 'index',
model: domainObject.getModel(),
id: domainObject.getId()
};
worker.postMessage(message);
}
} }
// Tell the worker to search for items it has that match this searchInput. // Tell the worker to search for items it has that match this searchInput.
@ -104,11 +109,16 @@ define(
// Recursive helper function for getItems() // Recursive helper function for getItems()
function itemsHelper(children, i) { function itemsHelper(children, i) {
var date = new Date(); // Index the current node
if (stopTime && date.getTime() >= stopTime) { indexItem(children[i]);
/*
if (stopTime && Date.now() >= stopTime) {
// This indexing of items has timed out // This indexing of items has timed out
return children; return children;
} else if (i >= children.length) { } else
*/
if (i >= children.length) {
// Done! // Done!
return children; return children;
} else if (children[i].hasCapability('composition')) { } else if (children[i].hasCapability('composition')) {
@ -128,43 +138,32 @@ define(
function getItems(timeout) { function getItems(timeout) {
var rootIds = [], var rootIds = [],
i; i;
for (i = 0; i < roots.length; i += 1) { for (i = 0; i < roots.length; i += 1) {
rootIds.push(roots[i].id); rootIds.push(roots[i].id);
} }
// Aquire root objects // Aquire root objects
return objectService.getObjects(rootIds).then(function (objectsById) { objectService.getObjects(rootIds).then(function (objectsById) {
var objects = [], var objects = [],
date,
id; id;
/*
// TODO: Is this timeout necissary?
if (timeout) { if (timeout) {
// Set a timeout for itemsHelper // Set a timeout for itemsHelper
date = new Date(); stopTime = Date.now() + timeout;
stopTime = date.getTime() + timeout;
} }
// If there was no timeout provided, leave undefined // If there was no timeout provided, leave undefined
// itemsHelper should just treat this as having no timeout // itemsHelper should just treat this as having no timeout
*/
// Convert to the format itemsHelper takes // Get each of the objects in objectsById
for (id in objectsById) { for (id in objectsById) {
objects.push(objectsById[id]); objects.push(objectsById[id]);
} }
// Get all of its descendents // Index all of the roots' descendents
return itemsHelper(objects, 0).then(function (items) { itemsHelper(objects, 0);
var i;
// Add each item that itemsHelper found to the web worker index
// TODO: Try to do this within itemsHelper. Perhaps just
// need to add this to the last two if statements?
for (i = 0; i < items.length; i += 1) {
indexItem(items[i]);
}
return; // We don't need to return anything anymore
// TODO: Fix return statements. Do we need them still?
});
}); });
} }
@ -194,7 +193,6 @@ define(
// just send the query to the worker. // just send the query to the worker.
workerSearch(input, maxResults, timestamp); workerSearch(input, maxResults, timestamp);
return;
} }
// Index the tree's contents once at the beginning // Index the tree's contents once at the beginning