Move mutation listening out of cache service

This commit is contained in:
Pete Richards 2016-11-08 14:14:56 -08:00
parent 9a7f69a614
commit d74eba1922
3 changed files with 20 additions and 24 deletions

View File

@ -373,9 +373,6 @@ define([
{ {
"key": "cacheService", "key": "cacheService",
"implementation": ModelCacheService, "implementation": ModelCacheService,
"depends": [
"topic"
]
}, },
{ {
"key": "now", "key": "now",
@ -415,7 +412,7 @@ define([
"runs": [ "runs": [
{ {
"implementation": TransactingMutationListener, "implementation": TransactingMutationListener,
"depends": ["topic", "transactionService"] "depends": ["topic", "transactionService", "cacheService"]
} }
], ],
"constants": [ "constants": [

View File

@ -28,13 +28,8 @@ define([], function () {
* @constructor * @constructor
* @memberof platform/core * @memberof platform/core
*/ */
function ModelCacheService(topic) { function ModelCacheService() {
this.cache = {}; this.cache = {};
topic('mutation').listen(function (domainObject) {
if (this.has(domainObject.getId())) {
this.put(domainObject.getId(), domainObject.getModel());
}
}.bind(this));
} }
/** /**

View File

@ -30,7 +30,11 @@ define([], function () {
* @param {Topic} topic the `topic` service; used to listen for mutation * @param {Topic} topic the `topic` service; used to listen for mutation
* @memberof platform/core * @memberof platform/core
*/ */
function TransactingMutationListener(topic, transactionService) { function TransactingMutationListener(
topic,
transactionService,
cacheService
) {
var mutationTopic = topic('mutation'); var mutationTopic = topic('mutation');
mutationTopic.listen(function (domainObject) { mutationTopic.listen(function (domainObject) {
var persistence = domainObject.getCapability('persistence'); var persistence = domainObject.getCapability('persistence');
@ -39,15 +43,6 @@ define([], function () {
if (!wasActive) { if (!wasActive) {
transactionService.startTransaction(); 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)) { if (!MUTATION_TRACKER.has(domainObject)) {
MUTATION_TRACKER.set(domainObject, domainObject MUTATION_TRACKER.set(domainObject, domainObject
@ -56,12 +51,21 @@ define([], function () {
); );
} }
// add model to cache and keep cache up to date with listener cacheService.put(domainObject.getId(), domainObject.getModel());
// remove listener and remove from cache on commit & on cancel.
function unlistenAndCall(f) {
return function () {
if (MUTATION_TRACKER.has(domainObject)) {
MUTATION_TRACKER.get(domainObject)();
MUTATION_TRACKER.delete(domainObject);
}
return f();
}
}
transactionService.addToTransaction( transactionService.addToTransaction(
wrap(persistence.persist.bind(persistence)), unlistenAndCall(persistence.persist.bind(persistence)),
wrap(persistence.refresh.bind(persistence)) unlistenAndCall(persistence.refresh.bind(persistence))
); );
if (!wasActive) { if (!wasActive) {