[Edit Mode] Simplify transaction stack

https://github.com/nasa/openmct/pull/874#r76593588
This commit is contained in:
Victor Woeltjen 2016-10-07 11:35:35 -07:00
parent 1a88c9a651
commit 947b54555a

View File

@ -37,8 +37,7 @@ define(
function TransactionService($q, $log) { function TransactionService($q, $log) {
this.$q = $q; this.$q = $q;
this.$log = $log; this.$log = $log;
this.transaction = undefined; this.transactions = [];
this.transactionStack = [];
} }
/** /**
@ -48,19 +47,18 @@ define(
* #cancel} are called * #cancel} are called
*/ */
TransactionService.prototype.startTransaction = function () { TransactionService.prototype.startTransaction = function () {
if (this.transaction) { var transaction = this.isActive() ?
this.transactionStack.push(this.transaction); new NestedTransaction(this.transactions[0]) :
this.transaction = new NestedTransaction(this.transaction); new Transaction(this.$log);
} else {
this.transaction = new Transaction(this.$log); this.transactions.push(transaction);
}
}; };
/** /**
* @returns {boolean} If true, indicates that a transaction is in progress * @returns {boolean} If true, indicates that a transaction is in progress
*/ */
TransactionService.prototype.isActive = function () { TransactionService.prototype.isActive = function () {
return !!this.transaction; return this.transactions.length > 0;
}; };
/** /**
@ -71,14 +69,22 @@ define(
* @param onCancel A function to call on cancel * @param onCancel A function to call on cancel
*/ */
TransactionService.prototype.addToTransaction = function (onCommit, onCancel) { TransactionService.prototype.addToTransaction = function (onCommit, onCancel) {
if (this.transaction) { if (this.isActive()) {
return this.transaction.add(onCommit, onCancel); return this.activeTransaction().add(onCommit, onCancel);
} else { } else {
//Log error because this is a programming error if it occurs. //Log error because this is a programming error if it occurs.
this.$log.error("No transaction in progress"); this.$log.error("No transaction in progress");
} }
}; };
/**
* Get the transaction at the top of the stack.
* @private
*/
TransactionService.prototype.activeTransaction = function () {
return this.transactions[this.transactions.length - 1];
};
/** /**
* All persist calls deferred since the beginning of the transaction * All persist calls deferred since the beginning of the transaction
* will be committed. * will be committed.
@ -87,8 +93,7 @@ define(
* completed. Will reject if any commit operations fail * completed. Will reject if any commit operations fail
*/ */
TransactionService.prototype.commit = function () { TransactionService.prototype.commit = function () {
var transaction = this.transaction; var transaction = this.transactions.pop();
this.transaction = this.transactionStack.pop();
return transaction ? transaction.commit() : Promise.reject(); return transaction ? transaction.commit() : Promise.reject();
}; };
@ -101,13 +106,17 @@ define(
* @returns {*} * @returns {*}
*/ */
TransactionService.prototype.cancel = function () { TransactionService.prototype.cancel = function () {
var transaction = this.transaction; var transaction = this.transactions.pop();
this.transaction = this.transactionStack.pop();
return transaction ? transaction.cancel() : Promise.reject(); return transaction ? transaction.cancel() : Promise.reject();
}; };
/**
* Get the size (the number of commit/cancel callbacks) of
* the active transaction.
* @returns {number} size of the active transaction
*/
TransactionService.prototype.size = function () { TransactionService.prototype.size = function () {
return this.transaction ? this.transaction.size() : 0; return this.isActive() ? this.activeTransaction.size() : 0;
}; };
return TransactionService; return TransactionService;