[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

@ -29,7 +29,8 @@ define(
function () { function () {
"use strict"; "use strict";
var TOPIC_PREFIX = "mutation:"; var GENERAL_TOPIC = "mutation",
TOPIC_PREFIX = "mutation:";
// Utility function to overwrite a destination object // Utility function to overwrite a destination object
// with the contents of a source object. // with the contents of a source object.
@ -78,7 +79,11 @@ define(
* @implements {Capability} * @implements {Capability}
*/ */
function MutationCapability(topic, now, domainObject) { function MutationCapability(topic, now, domainObject) {
this.mutationTopic = topic(TOPIC_PREFIX + domainObject.getId()); this.generalMutationTopic =
topic(GENERAL_TOPIC);
this.specificMutationTopic =
topic(TOPIC_PREFIX + domainObject.getId());
this.now = now; this.now = now;
this.domainObject = domainObject; this.domainObject = domainObject;
} }
@ -115,11 +120,19 @@ define(
// mutator function has a temporary copy to work with. // mutator function has a temporary copy to work with.
var domainObject = this.domainObject, var domainObject = this.domainObject,
now = this.now, now = this.now,
t = this.mutationTopic, generalTopic = this.generalMutationTopic,
specificTopic = this.specificMutationTopic,
model = domainObject.getModel(), model = domainObject.getModel(),
clone = JSON.parse(JSON.stringify(model)), clone = JSON.parse(JSON.stringify(model)),
useTimestamp = arguments.length > 1; useTimestamp = arguments.length > 1;
function notifyListeners(model) {
// Broadcast a general event...
generalTopic.notify(domainObject);
// ...and also notify listeners watching this specific object.
specificTopic.notify(model);
}
// Function to handle copying values to the actual // Function to handle copying values to the actual
function handleMutation(mutationResult) { function handleMutation(mutationResult) {
// If mutation result was undefined, just use // If mutation result was undefined, just use
@ -136,7 +149,7 @@ define(
copyValues(model, result); copyValues(model, result);
} }
model.modified = useTimestamp ? timestamp : now(); model.modified = useTimestamp ? timestamp : now();
t.notify(model); notifyListeners(model);
} }
// Report the result of the mutation // Report the result of the mutation

View File

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

View File

@ -47,10 +47,11 @@ 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, $timeout, objectService, workerService, ROOTS) { function GenericSearchProvider($q, $timeout, objectService, workerService, topic, ROOTS) {
var indexed = {}, var indexed = {},
pendingQueries = {}, pendingQueries = {},
worker = workerService.run('genericSearchWorker'); worker = workerService.run('genericSearchWorker'),
mutationTopic = topic("mutation");
this.worker = worker; this.worker = worker;
this.pendingQueries = pendingQueries; this.pendingQueries = pendingQueries;
@ -113,23 +114,6 @@ 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();
@ -160,11 +144,6 @@ 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(handleMutation);
}
}); });
} }
@ -189,6 +168,11 @@ define(
// Index the tree's contents once at the beginning // Index the tree's contents once at the beginning
getItems(); getItems();
// Re-index items when they are mutated
mutationTopic.listen(function (domainObject) {
indexItems([domainObject]);
});
} }
/** /**