|
| 1 | +/***************************************************************************** |
| 2 | + * Open MCT, Copyright (c) 2014-2016, United States Government |
| 3 | + * as represented by the Administrator of the National Aeronautics and Space |
| 4 | + * Administration. All rights reserved. |
| 5 | + * |
| 6 | + * Open MCT is licensed under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + * |
| 17 | + * Open MCT includes source code licensed under additional open source |
| 18 | + * licenses. See the Open Source Licenses file (LICENSES.md) included with |
| 19 | + * this source code distribution or the Licensing information page available |
| 20 | + * at runtime from the About dialog for additional information. |
| 21 | + *****************************************************************************/ |
| 22 | + |
| 23 | +define([], function () { |
| 24 | + /** |
| 25 | + * Manages transactions to support the TransactionalPersistenceCapability. |
| 26 | + * This assumes that all commit/cancel callbacks for a given domain |
| 27 | + * object are equivalent, and only need to be added once to any active |
| 28 | + * transaction. Violating this assumption may cause unexpected behavior. |
| 29 | + * @constructor |
| 30 | + * @memberof platform/commonUI/edit |
| 31 | + */ |
| 32 | + function TransactionManager(transactionService) { |
| 33 | + this.transactionService = transactionService; |
| 34 | + this.clearTransactionFns = {}; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Check if a transaction is currently active. |
| 39 | + * @returns {boolean} true if there is a transaction active |
| 40 | + */ |
| 41 | + TransactionManager.prototype.isActive = function () { |
| 42 | + return this.transactionService.isActive(); |
| 43 | + }; |
| 44 | + |
| 45 | + /** |
| 46 | + * Check if callbacks associated with this domain object have already |
| 47 | + * been added to the active transaction. |
| 48 | + * @private |
| 49 | + * @param {string} id the identifier of the domain object to check |
| 50 | + * @returns {boolean} true if callbacks have been added |
| 51 | + */ |
| 52 | + TransactionManager.prototype.isScheduled = function (id) { |
| 53 | + return !!this.clearTransactionFns[id]; |
| 54 | + }; |
| 55 | + |
| 56 | + /** |
| 57 | + * Add callbacks associated with this domain object to the active |
| 58 | + * transaction. Both callbacks are expected to return promises that |
| 59 | + * resolve when their associated behavior is complete. |
| 60 | + * |
| 61 | + * If callbacks associated with this domain object have already been |
| 62 | + * added to the active transaction, this call will be ignored. |
| 63 | + * |
| 64 | + * @param {string} id the identifier of the associated domain object |
| 65 | + * @param {Function} onCommit behavior to invoke when committing transaction |
| 66 | + * @param {Function} onCancel behavior to invoke when cancelling transaction |
| 67 | + */ |
| 68 | + TransactionManager.prototype.addToTransaction = function ( |
| 69 | + id, |
| 70 | + onCommit, |
| 71 | + onCancel |
| 72 | + ) { |
| 73 | + var release = this.releaseClearFn.bind(this, id); |
| 74 | + |
| 75 | + function chain(promiseFn, nextFn) { |
| 76 | + return function () { |
| 77 | + return promiseFn().then(nextFn); |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + if (!this.isScheduled(id)) { |
| 82 | + this.clearTransactionFns[id] = |
| 83 | + this.transactionService.addToTransaction( |
| 84 | + chain(onCommit, release), |
| 85 | + chain(onCancel, release) |
| 86 | + ); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + /** |
| 91 | + * Remove any callbacks associated with this domain object from the |
| 92 | + * active transaction. |
| 93 | + * @param {string} id the identifier for the domain object |
| 94 | + */ |
| 95 | + TransactionManager.prototype.clearTransactionsFor = function (id) { |
| 96 | + if (this.isScheduled(id)) { |
| 97 | + this.clearTransactionFns[id](); |
| 98 | + this.releaseClearFn(id); |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + /** |
| 103 | + * Release the cached "remove from transaction" function that has been |
| 104 | + * stored in association with this domain object. |
| 105 | + * @param {string} id the identifier for the domain object |
| 106 | + * @private |
| 107 | + */ |
| 108 | + TransactionManager.prototype.releaseClearFn = function (id) { |
| 109 | + delete this.clearTransactionFns[id]; |
| 110 | + }; |
| 111 | + |
| 112 | + return TransactionManager; |
| 113 | +}); |
0 commit comments