From 947b54555a07ed69cdb4e6b7a458485cbdcfa6a1 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Fri, 7 Oct 2016 11:35:35 -0700 Subject: [PATCH] [Edit Mode] Simplify transaction stack https://github.com/nasa/openmct/pull/874#r76593588 --- .../edit/src/services/TransactionService.js | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/platform/commonUI/edit/src/services/TransactionService.js b/platform/commonUI/edit/src/services/TransactionService.js index 7697eed436..00aa91e6e4 100644 --- a/platform/commonUI/edit/src/services/TransactionService.js +++ b/platform/commonUI/edit/src/services/TransactionService.js @@ -37,8 +37,7 @@ define( function TransactionService($q, $log) { this.$q = $q; this.$log = $log; - this.transaction = undefined; - this.transactionStack = []; + this.transactions = []; } /** @@ -48,19 +47,18 @@ define( * #cancel} are called */ TransactionService.prototype.startTransaction = function () { - if (this.transaction) { - this.transactionStack.push(this.transaction); - this.transaction = new NestedTransaction(this.transaction); - } else { - this.transaction = new Transaction(this.$log); - } + var transaction = this.isActive() ? + new NestedTransaction(this.transactions[0]) : + new Transaction(this.$log); + + this.transactions.push(transaction); }; /** * @returns {boolean} If true, indicates that a transaction is in progress */ 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 */ TransactionService.prototype.addToTransaction = function (onCommit, onCancel) { - if (this.transaction) { - return this.transaction.add(onCommit, onCancel); + if (this.isActive()) { + return this.activeTransaction().add(onCommit, onCancel); } else { //Log error because this is a programming error if it occurs. 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 * will be committed. @@ -87,8 +93,7 @@ define( * completed. Will reject if any commit operations fail */ TransactionService.prototype.commit = function () { - var transaction = this.transaction; - this.transaction = this.transactionStack.pop(); + var transaction = this.transactions.pop(); return transaction ? transaction.commit() : Promise.reject(); }; @@ -101,13 +106,17 @@ define( * @returns {*} */ TransactionService.prototype.cancel = function () { - var transaction = this.transaction; - this.transaction = this.transactionStack.pop(); + var transaction = this.transactions.pop(); 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 () { - return this.transaction ? this.transaction.size() : 0; + return this.isActive() ? this.activeTransaction.size() : 0; }; return TransactionService;