diff --git a/platform/core/bundle.js b/platform/core/bundle.js index 294d339692..1d630d96cd 100644 --- a/platform/core/bundle.js +++ b/platform/core/bundle.js @@ -373,9 +373,6 @@ define([ { "key": "cacheService", "implementation": ModelCacheService, - "depends": [ - "topic" - ] }, { "key": "now", @@ -415,7 +412,7 @@ define([ "runs": [ { "implementation": TransactingMutationListener, - "depends": ["topic", "transactionService"] + "depends": ["topic", "transactionService", "cacheService"] } ], "constants": [ diff --git a/platform/core/src/models/ModelCacheService.js b/platform/core/src/models/ModelCacheService.js index 0ddb279251..0e3daf2121 100644 --- a/platform/core/src/models/ModelCacheService.js +++ b/platform/core/src/models/ModelCacheService.js @@ -28,13 +28,8 @@ define([], function () { * @constructor * @memberof platform/core */ - function ModelCacheService(topic) { + function ModelCacheService() { this.cache = {}; - topic('mutation').listen(function (domainObject) { - if (this.has(domainObject.getId())) { - this.put(domainObject.getId(), domainObject.getModel()); - } - }.bind(this)); } /** diff --git a/platform/core/src/runs/TransactingMutationListener.js b/platform/core/src/runs/TransactingMutationListener.js index 101798bd67..01d765f1bb 100644 --- a/platform/core/src/runs/TransactingMutationListener.js +++ b/platform/core/src/runs/TransactingMutationListener.js @@ -30,7 +30,11 @@ define([], function () { * @param {Topic} topic the `topic` service; used to listen for mutation * @memberof platform/core */ - function TransactingMutationListener(topic, transactionService) { + function TransactingMutationListener( + topic, + transactionService, + cacheService + ) { var mutationTopic = topic('mutation'); mutationTopic.listen(function (domainObject) { var persistence = domainObject.getCapability('persistence'); @@ -39,15 +43,6 @@ define([], function () { if (!wasActive) { transactionService.startTransaction(); } - var wrap = function(f) { - return function () { - if (MUTATION_TRACKER.has(domainObject)) { - MUTATION_TRACKER.get(domainObject)(); - MUTATION_TRACKER.delete(domainObject); - } - return f(); - } - } if (!MUTATION_TRACKER.has(domainObject)) { MUTATION_TRACKER.set(domainObject, domainObject @@ -56,12 +51,21 @@ define([], function () { ); } - // add model to cache and keep cache up to date with listener - // remove listener and remove from cache on commit & on cancel. + cacheService.put(domainObject.getId(), domainObject.getModel()); + + function unlistenAndCall(f) { + return function () { + if (MUTATION_TRACKER.has(domainObject)) { + MUTATION_TRACKER.get(domainObject)(); + MUTATION_TRACKER.delete(domainObject); + } + return f(); + } + } transactionService.addToTransaction( - wrap(persistence.persist.bind(persistence)), - wrap(persistence.refresh.bind(persistence)) + unlistenAndCall(persistence.persist.bind(persistence)), + unlistenAndCall(persistence.refresh.bind(persistence)) ); if (!wasActive) {