[Persistence] Share transaction clearing functions

This commit is contained in:
Victor Woeltjen 2016-07-14 12:53:58 -07:00
parent 00fff52529
commit c7529dd56b

View File

@ -24,7 +24,7 @@
define(
[],
function () {
var PERSIST_PENDING_SET = {};
var TRANSACTION_SET = {};
/**
* Wraps persistence capability to enable transactions. Transactions
@ -49,15 +49,14 @@ define(
this.persistenceCapability = persistenceCapability;
this.domainObject = domainObject;
this.$q = $q;
this.removeFromTransaction = undefined;
}
TransactionalPersistenceCapability.prototype.persistPending = function (state) {
TransactionalPersistenceCapability.prototype.transactionClearer = function (state) {
var id = this.domainObject.getId();
if (arguments.length > 0) {
PERSIST_PENDING_SET[id] = state;
TRANSACTION_SET[id] = state;
}
return PERSIST_PENDING_SET[id];
return TRANSACTION_SET[id];
};
/**
@ -69,11 +68,11 @@ define(
var self = this;
function onCommit() {
if (!self.persistPending()) {
if (!self.transactionClearer()) {
return Promise.resolve(true);
}
return self.persistenceCapability.persist().then(function (result) {
self.persistPending(false);
self.transactionClearer(undefined);
return result;
});
}
@ -82,11 +81,11 @@ define(
if (self.domainObject.getModel().persisted !== undefined) {
//Fetch clean model from persistence
return self.persistenceCapability.refresh().then(function (result) {
self.persistPending(false);
self.transactionClearer(undefined);
return result;
});
} else {
self.persistPending(false);
self.transactionClearer(undefined);
self.removeFromTransaction = undefined;
//Model is undefined in persistence, so return undefined.
return self.$q.when(undefined);
@ -94,10 +93,9 @@ define(
}
if (this.transactionService.isActive()) {
if (!self.persistPending()) {
this.removeFromTransaction = this.transactionService
.addToTransaction(onCommit, onCancel);
this.persistPending(true);
if (!self.transactionClearer()) {
this.transactionClearer(this.transactionService
.addToTransaction(onCommit, onCancel));
}
//Need to return a promise from this function
return this.$q.when(true);
@ -107,10 +105,9 @@ define(
};
TransactionalPersistenceCapability.prototype.refresh = function () {
if (this.persistPending()) {
this.persistPending(false);
this.removeFromTransaction();
this.removeFromTransaction = undefined;
if (this.transactionClearer()) {
this.transactionClearer()();
this.transactionClearer(undefined);
}
return this.persistenceCapability.refresh();
};