[Search] Move reindex function
Move function used to listen for mutation changes (to trigger reindexing) up in scope, to avoid retaining references to domain objects via closure. nasa/openmctweb#141 Also, includes misc. whitespace normalization (provided by code editor.)
This commit is contained in:
@@ -28,13 +28,13 @@ define(
|
|||||||
[],
|
[],
|
||||||
function () {
|
function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var DEFAULT_MAX_RESULTS = 100,
|
var DEFAULT_MAX_RESULTS = 100,
|
||||||
DEFAULT_TIMEOUT = 1000,
|
DEFAULT_TIMEOUT = 1000,
|
||||||
stopTime;
|
stopTime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A search service which searches through domain objects in
|
* A search service which searches through domain objects in
|
||||||
* the filetree without using external search implementations.
|
* the filetree without using external search implementations.
|
||||||
*
|
*
|
||||||
* @constructor
|
* @constructor
|
||||||
@@ -44,7 +44,7 @@ define(
|
|||||||
* domain objects can be gotten.
|
* domain objects can be gotten.
|
||||||
* @param {WorkerService} workerService The service which allows
|
* @param {WorkerService} workerService The service which allows
|
||||||
* more easy creation of web workers.
|
* more easy creation of web workers.
|
||||||
* @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, $timeout, objectService, workerService, ROOTS) {
|
function GenericSearchProvider($q, $timeout, objectService, workerService, ROOTS) {
|
||||||
@@ -57,11 +57,11 @@ define(
|
|||||||
this.$q = $q;
|
this.$q = $q;
|
||||||
// pendingQueries is a dictionary with the key value pairs st
|
// pendingQueries is a dictionary with the key value pairs st
|
||||||
// the key is the timestamp and the value is the promise
|
// the key is the timestamp and the value is the promise
|
||||||
|
|
||||||
// Tell the web worker to add a domain object's model to its list of items.
|
// Tell the web worker to add a domain object's model to its list of items.
|
||||||
function indexItem(domainObject) {
|
function indexItem(domainObject) {
|
||||||
var message;
|
var message;
|
||||||
|
|
||||||
// undefined check
|
// undefined check
|
||||||
if (domainObject && domainObject.getModel) {
|
if (domainObject && domainObject.getModel) {
|
||||||
// Using model instead of whole domain object because
|
// Using model instead of whole domain object because
|
||||||
@@ -74,15 +74,15 @@ define(
|
|||||||
worker.postMessage(message);
|
worker.postMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Handles responses from the web worker. Namely, the results of
|
|
||||||
// a search request.
|
// Handles responses from the web worker. Namely, the results of
|
||||||
|
// a search request.
|
||||||
function handleResponse(event) {
|
function handleResponse(event) {
|
||||||
var ids = [],
|
var ids = [],
|
||||||
id;
|
id;
|
||||||
|
|
||||||
// If we have the results from a search
|
// If we have the results from a search
|
||||||
if (event.data.request === 'search') {
|
if (event.data.request === 'search') {
|
||||||
// Convert the ids given from the web worker into domain objects
|
// Convert the ids given from the web worker into domain objects
|
||||||
for (id in event.data.results) {
|
for (id in event.data.results) {
|
||||||
@@ -91,7 +91,7 @@ define(
|
|||||||
objectService.getObjects(ids).then(function (objects) {
|
objectService.getObjects(ids).then(function (objects) {
|
||||||
var searchResults = [],
|
var searchResults = [],
|
||||||
id;
|
id;
|
||||||
|
|
||||||
// Create searchResult objects
|
// Create searchResult objects
|
||||||
for (id in objects) {
|
for (id in objects) {
|
||||||
searchResults.push({
|
searchResults.push({
|
||||||
@@ -100,8 +100,8 @@ define(
|
|||||||
score: event.data.results[id]
|
score: event.data.results[id]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resove the promise corresponding to this
|
// Resove the promise corresponding to this
|
||||||
pendingQueries[event.data.timestamp].resolve({
|
pendingQueries[event.data.timestamp].resolve({
|
||||||
hits: searchResults,
|
hits: searchResults,
|
||||||
total: event.data.total,
|
total: event.data.total,
|
||||||
@@ -110,27 +110,44 @@ define(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function for getItems(). Indexes the tree.
|
// Helper function for getItems(). Indexes the tree.
|
||||||
function indexItems(nodes) {
|
function indexItems(nodes) {
|
||||||
|
function handleMutation(model) {
|
||||||
|
if (model && model.composition) {
|
||||||
|
// If the node was mutated to have children, get the child domain objects
|
||||||
|
objectService.getObjects(listener.composition).then(function (objectsById) {
|
||||||
|
var objects = [],
|
||||||
|
id;
|
||||||
|
|
||||||
|
// Get each of the domain objects in objectsById
|
||||||
|
for (id in objectsById) {
|
||||||
|
objects.push(objectsById[id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
indexItems(objects);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
nodes.forEach(function (node) {
|
nodes.forEach(function (node) {
|
||||||
var id = node && node.getId && node.getId();
|
var id = node && node.getId && node.getId();
|
||||||
|
|
||||||
// If we have already indexed this item, stop here
|
// If we have already indexed this item, stop here
|
||||||
if (indexed[id]) {
|
if (indexed[id]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index each item with the web worker
|
// Index each item with the web worker
|
||||||
indexItem(node);
|
indexItem(node);
|
||||||
indexed[id] = true;
|
indexed[id] = true;
|
||||||
|
|
||||||
|
|
||||||
// If this node has children, index those
|
// If this node has children, index those
|
||||||
if (node && node.hasCapability && node.hasCapability('composition')) {
|
if (node && node.hasCapability && node.hasCapability('composition')) {
|
||||||
// Make sure that this is async, so doesn't block up page
|
// Make sure that this is async, so doesn't block up page
|
||||||
$timeout(function () {
|
$timeout(function () {
|
||||||
// Get the children...
|
// Get the children...
|
||||||
node.useCapability('composition').then(function (children) {
|
node.useCapability('composition').then(function (children) {
|
||||||
$timeout(function () {
|
$timeout(function () {
|
||||||
// ... then index the children
|
// ... then index the children
|
||||||
@@ -143,41 +160,26 @@ define(
|
|||||||
});
|
});
|
||||||
}, 0);
|
}, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Watch for changes to this item, in case it gets new children
|
|
||||||
if (node && node.hasCapability && node.hasCapability('mutation')) {
|
|
||||||
node.getCapability('mutation').listen(function (listener) {
|
|
||||||
if (listener && listener.composition) {
|
|
||||||
// If the node was mutated to have children, get the child domain objects
|
|
||||||
objectService.getObjects(listener.composition).then(function (objectsById) {
|
|
||||||
var objects = [],
|
|
||||||
id;
|
|
||||||
|
|
||||||
// Get each of the domain objects in objectsById
|
// Watch for changes to this item, in case it gets new children
|
||||||
for (id in objectsById) {
|
if (node && node.hasCapability && node.hasCapability('mutation')) {
|
||||||
objects.push(objectsById[id]);
|
node.getCapability('mutation').listen(handleMutation);
|
||||||
}
|
|
||||||
|
|
||||||
indexItems(objects);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts the filetree into a list
|
// Converts the filetree into a list
|
||||||
function getItems() {
|
function getItems() {
|
||||||
// Aquire root objects
|
// Aquire root objects
|
||||||
objectService.getObjects(ROOTS).then(function (objectsById) {
|
objectService.getObjects(ROOTS).then(function (objectsById) {
|
||||||
var objects = [],
|
var objects = [],
|
||||||
id;
|
id;
|
||||||
|
|
||||||
// Get each of the domain objects in objectsById
|
// Get each of the domain objects in objectsById
|
||||||
for (id in objectsById) {
|
for (id in objectsById) {
|
||||||
objects.push(objectsById[id]);
|
objects.push(objectsById[id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index all of the roots' descendents
|
// Index all of the roots' descendents
|
||||||
indexItems(objects);
|
indexItems(objects);
|
||||||
});
|
});
|
||||||
@@ -185,7 +187,7 @@ define(
|
|||||||
|
|
||||||
worker.onmessage = handleResponse;
|
worker.onmessage = handleResponse;
|
||||||
|
|
||||||
// Index the tree's contents once at the beginning
|
// Index the tree's contents once at the beginning
|
||||||
getItems();
|
getItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,4 +268,4 @@ define(
|
|||||||
|
|
||||||
return GenericSearchProvider;
|
return GenericSearchProvider;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user