From c4aed5716521ab199003a1b31fe1604f6c2f3c28 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 17 Sep 2015 13:51:47 -0700 Subject: [PATCH] [Entanglement] Refactor LocatingObjectDecorator Rearrange code in LocatingObjectDecorator to make it easier to follow; add comments. --- .../src/services/LocatingObjectDecorator.js | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/platform/entanglement/src/services/LocatingObjectDecorator.js b/platform/entanglement/src/services/LocatingObjectDecorator.js index 6fde642ee0..f4be5f1900 100644 --- a/platform/entanglement/src/services/LocatingObjectDecorator.js +++ b/platform/entanglement/src/services/LocatingObjectDecorator.js @@ -47,28 +47,19 @@ define( objectService = this.objectService, result = {}; + // Load a single object using location to establish a context function loadObjectInContext(id, exclude) { - function attachContextById(domainObject, locationId) { - return loadObjectInContext(locationId, exclude) - .then(function (parent) { - return contextualize(domainObject, parent); - }); - } - - function attachContextForLocation(domainObject) { - var model = domainObject && domainObject.getModel(), + function attachContext(objects) { + var domainObject = (objects || {})[id], + model = domainObject && domainObject.getModel(), location = (model || {}).location; - // Don't pursue a context if we encounter this - // object again during this sequence of invocations. - exclude[id] = true; + // If no location is defined, we can't look up a context. + if (!location) { + return domainObject; + } - return location ? - attachContextById(domainObject, location) : - domainObject; - } - - return objectService.getObjects([id]).then(function (objects) { + // Avoid looping indefinitely on cyclical locations if (exclude[id]) { $log.warn([ "LocatingObjectDecorator detected a cycle", @@ -77,11 +68,21 @@ define( "no context will be added and unexpected behavior", "may follow." ].join(" ")); - return objects[id]; + return domainObject; } - return attachContextForLocation(objects[id]); - }); + // Record that we've visited this ID to detect cycles. + exclude[id] = true; + + // Do the recursive step to get the parent... + return loadObjectInContext(location, exclude) + .then(function (parent) { + // ...and then contextualize with it! + return contextualize(domainObject, parent); + }); + } + + return objectService.getObjects([id]).then(attachContext); } ids.forEach(function (id) {