[Search] Listen on a global mutation topic

Listen on a global mutation topic to remove the need to retain
listeners per domain object.
This commit is contained in:
Victor Woeltjen
2015-09-29 11:47:02 -07:00
parent ad7d3d642e
commit 866c8882ca
3 changed files with 34 additions and 30 deletions

View File

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

View File

@@ -47,10 +47,11 @@ define(
* @param {GENERIC_SEARCH_ROOTS} ROOTS An array of the root
* domain objects' IDs.
*/
function GenericSearchProvider($q, $timeout, objectService, workerService, ROOTS) {
function GenericSearchProvider($q, $timeout, objectService, workerService, topic, ROOTS) {
var indexed = {},
pendingQueries = {},
worker = workerService.run('genericSearchWorker');
worker = workerService.run('genericSearchWorker'),
mutationTopic = topic("mutation");
this.worker = worker;
this.pendingQueries = pendingQueries;
@@ -113,23 +114,6 @@ define(
// Helper function for getItems(). Indexes the tree.
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) {
var id = node && node.getId && node.getId();
@@ -160,11 +144,6 @@ define(
});
}, 0);
}
// Watch for changes to this item, in case it gets new children
if (node && node.hasCapability && node.hasCapability('mutation')) {
node.getCapability('mutation').listen(handleMutation);
}
});
}
@@ -189,6 +168,11 @@ define(
// Index the tree's contents once at the beginning
getItems();
// Re-index items when they are mutated
mutationTopic.listen(function (domainObject) {
indexItems([domainObject]);
});
}
/**