diff --git a/platform/core/src/capabilities/MutationCapability.js b/platform/core/src/capabilities/MutationCapability.js index b9f49ca969..8746b9af64 100644 --- a/platform/core/src/capabilities/MutationCapability.js +++ b/platform/core/src/capabilities/MutationCapability.js @@ -29,7 +29,8 @@ define( function () { "use strict"; - var TOPIC_PREFIX = "mutation:"; + var GENERAL_TOPIC = "mutation", + TOPIC_PREFIX = "mutation:"; // Utility function to overwrite a destination object // with the contents of a source object. @@ -78,7 +79,11 @@ define( * @implements {Capability} */ 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.domainObject = domainObject; } @@ -115,11 +120,19 @@ define( // mutator function has a temporary copy to work with. var domainObject = this.domainObject, now = this.now, - t = this.mutationTopic, + generalTopic = this.generalMutationTopic, + specificTopic = this.specificMutationTopic, model = domainObject.getModel(), clone = JSON.parse(JSON.stringify(model)), 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 handleMutation(mutationResult) { // If mutation result was undefined, just use @@ -136,7 +149,7 @@ define( copyValues(model, result); } model.modified = useTimestamp ? timestamp : now(); - t.notify(model); + notifyListeners(model); } // Report the result of the mutation diff --git a/platform/search/bundle.json b/platform/search/bundle.json index 7ea1536556..4602227c27 100644 --- a/platform/search/bundle.json +++ b/platform/search/bundle.json @@ -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 @@ } ] } -} \ No newline at end of file +} diff --git a/platform/search/src/services/GenericSearchProvider.js b/platform/search/src/services/GenericSearchProvider.js index 8688fae68a..eba2b7f725 100644 --- a/platform/search/src/services/GenericSearchProvider.js +++ b/platform/search/src/services/GenericSearchProvider.js @@ -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]); + }); } /**