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",
"implementation": ModelCacheService,
"depends": [
"topic"
]
},
{
"key": "now",
@ -415,7 +412,7 @@ define([
"runs": [
{
"implementation": TransactingMutationListener,
"depends": ["topic", "transactionService"]
"depends": ["topic", "transactionService", "cacheService"]
}
],
"constants": [

View File

@ -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));
}
/**

View File

@ -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) {