[Persistence] Use ids from TransactionManager API

Recommended during code review,
https://github.com/nasa/openmct/pull/1084#discussion_r71021889
This commit is contained in:
Victor Woeltjen 2016-07-15 12:23:33 -07:00
parent 550e60455b
commit eb6ddb5e45
4 changed files with 31 additions and 33 deletions

View File

@ -60,7 +60,7 @@ define(
if (this.transactionManager.isActive()) { if (this.transactionManager.isActive()) {
this.transactionManager.addToTransaction( this.transactionManager.addToTransaction(
this.domainObject, this.domainObject.getId(),
wrappedPersistence.persist.bind(wrappedPersistence), wrappedPersistence.persist.bind(wrappedPersistence),
wrappedPersistence.refresh.bind(wrappedPersistence) wrappedPersistence.refresh.bind(wrappedPersistence)
); );
@ -72,7 +72,8 @@ define(
}; };
TransactionalPersistenceCapability.prototype.refresh = function () { TransactionalPersistenceCapability.prototype.refresh = function () {
this.transactionManager.clearTransactionsFor(this.domainObject); this.transactionManager
.clearTransactionsFor(this.domainObject.getId());
return this.persistenceCapability.refresh(); return this.persistenceCapability.refresh();
}; };

View File

@ -46,11 +46,11 @@ define([], function () {
* Check if callbacks associated with this domain object have already * Check if callbacks associated with this domain object have already
* been added to the active transaction. * been added to the active transaction.
* @private * @private
* @param {DomainObject} domainObject the object to check * @param {string} id the identifier of the domain object to check
* @returns {boolean} true if callbacks have been added * @returns {boolean} true if callbacks have been added
*/ */
TransactionManager.prototype.isScheduled = function (domainObject) { TransactionManager.prototype.isScheduled = function (id) {
return !!this.clearTransactionFns[domainObject.getId()]; return !!this.clearTransactionFns[id];
}; };
/** /**
@ -61,16 +61,16 @@ define([], function () {
* If callbacks associated with this domain object have already been * If callbacks associated with this domain object have already been
* added to the active transaction, this call will be ignored. * added to the active transaction, this call will be ignored.
* *
* @param {DomainObject} domainObject the associated domain object * @param {string} id the identifier of the associated domain object
* @param {Function} onCommit behavior to invoke when committing transaction * @param {Function} onCommit behavior to invoke when committing transaction
* @param {Function} onCancel behavior to invoke when cancelling transaction * @param {Function} onCancel behavior to invoke when cancelling transaction
*/ */
TransactionManager.prototype.addToTransaction = function ( TransactionManager.prototype.addToTransaction = function (
domainObject, id,
onCommit, onCommit,
onCancel onCancel
) { ) {
var release = this.releaseClearFn.bind(this, domainObject); var release = this.releaseClearFn.bind(this, id);
function chain(promiseFn, nextFn) { function chain(promiseFn, nextFn) {
return function () { return function () {
@ -78,8 +78,8 @@ define([], function () {
}; };
} }
if (!this.isScheduled(domainObject)) { if (!this.isScheduled(id)) {
this.clearTransactionFns[domainObject.getId()] = this.clearTransactionFns[id] =
this.transactionService.addToTransaction( this.transactionService.addToTransaction(
chain(onCommit, release), chain(onCommit, release),
chain(onCancel, release) chain(onCancel, release)
@ -90,23 +90,23 @@ define([], function () {
/** /**
* Remove any callbacks associated with this domain object from the * Remove any callbacks associated with this domain object from the
* active transaction. * active transaction.
* @param {DomainObject} domainObject the domain object * @param {string} id the identifier for the domain object
*/ */
TransactionManager.prototype.clearTransactionsFor = function (domainObject) { TransactionManager.prototype.clearTransactionsFor = function (id) {
if (this.isScheduled(domainObject)) { if (this.isScheduled(id)) {
this.clearTransactionFns[domainObject.getId()](); this.clearTransactionFns[id]();
this.releaseClearFn(domainObject); this.releaseClearFn(id);
} }
}; };
/** /**
* Release the cached "remove from transaction" function that has been * Release the cached "remove from transaction" function that has been
* stored in association with this domain object. * stored in association with this domain object.
* @param {DomainObject} domainObject the domain object * @param {string} id the identifier for the domain object
* @private * @private
*/ */
TransactionManager.prototype.releaseClearFn = function (domainObject) { TransactionManager.prototype.releaseClearFn = function (id) {
delete this.clearTransactionFns[domainObject.getId()]; delete this.clearTransactionFns[id];
}; };
return TransactionManager; return TransactionManager;

View File

@ -40,9 +40,12 @@ define(
mockTransactionManager, mockTransactionManager,
mockPersistence, mockPersistence,
mockDomainObject, mockDomainObject,
testId,
capability; capability;
beforeEach(function () { beforeEach(function () {
testId = "test-id";
mockQ = jasmine.createSpyObj("$q", ["when"]); mockQ = jasmine.createSpyObj("$q", ["when"]);
mockQ.when.andCallFake(function (val) { mockQ.when.andCallFake(function (val) {
return fastPromise(val); return fastPromise(val);
@ -60,11 +63,10 @@ define(
mockDomainObject = jasmine.createSpyObj( mockDomainObject = jasmine.createSpyObj(
"domainObject", "domainObject",
[ ["getModel", "getId"]
"getModel"
]
); );
mockDomainObject.getModel.andReturn({persisted: 1}); mockDomainObject.getModel.andReturn({persisted: 1});
mockDomainObject.getId.andReturn(testId);
capability = new TransactionalPersistenceCapability( capability = new TransactionalPersistenceCapability(
mockQ, mockQ,
@ -100,7 +102,7 @@ define(
it("clears transactions and delegates refresh calls", function () { it("clears transactions and delegates refresh calls", function () {
capability.refresh(); capability.refresh();
expect(mockTransactionManager.clearTransactionsFor) expect(mockTransactionManager.clearTransactionsFor)
.toHaveBeenCalledWith(mockDomainObject); .toHaveBeenCalledWith(testId);
expect(mockPersistence.refresh) expect(mockPersistence.refresh)
.toHaveBeenCalled(); .toHaveBeenCalled();
}); });

View File

@ -26,7 +26,7 @@ define(
function (TransactionManager) { function (TransactionManager) {
describe("TransactionManager", function () { describe("TransactionManager", function () {
var mockTransactionService, var mockTransactionService,
mockDomainObject, testId,
mockOnCommit, mockOnCommit,
mockOnCancel, mockOnCancel,
mockRemoves, mockRemoves,
@ -41,11 +41,7 @@ define(
); );
mockOnCommit = jasmine.createSpy('commit'); mockOnCommit = jasmine.createSpy('commit');
mockOnCancel = jasmine.createSpy('cancel'); mockOnCancel = jasmine.createSpy('cancel');
mockDomainObject = jasmine.createSpyObj( testId = 'test-id';
'domainObject',
['getId', 'getModel', 'getCapability']
);
mockDomainObject.getId.andReturn('testId');
mockPromise = jasmine.createSpyObj('promise', ['then']); mockPromise = jasmine.createSpyObj('promise', ['then']);
mockOnCommit.andReturn(mockPromise); mockOnCommit.andReturn(mockPromise);
@ -71,7 +67,7 @@ define(
describe("when addToTransaction is called", function () { describe("when addToTransaction is called", function () {
beforeEach(function () { beforeEach(function () {
manager.addToTransaction( manager.addToTransaction(
mockDomainObject, testId,
mockOnCommit, mockOnCommit,
mockOnCancel mockOnCancel
); );
@ -99,7 +95,7 @@ define(
it("ignores subsequent calls for the same object", function () { it("ignores subsequent calls for the same object", function () {
manager.addToTransaction( manager.addToTransaction(
mockDomainObject, testId,
jasmine.createSpy(), jasmine.createSpy(),
jasmine.createSpy() jasmine.createSpy()
); );
@ -108,9 +104,8 @@ define(
}); });
it("accepts subsequent calls for other objects", function () { it("accepts subsequent calls for other objects", function () {
mockDomainObject.getId.andReturn('otherId');
manager.addToTransaction( manager.addToTransaction(
mockDomainObject, 'other-id',
jasmine.createSpy(), jasmine.createSpy(),
jasmine.createSpy() jasmine.createSpy()
); );
@ -124,7 +119,7 @@ define(
describe("and clearTransactionsFor is subsequently called", function () { describe("and clearTransactionsFor is subsequently called", function () {
beforeEach(function () { beforeEach(function () {
manager.clearTransactionsFor(mockDomainObject); manager.clearTransactionsFor(testId);
}); });
it("removes callbacks from the transaction", function () { it("removes callbacks from the transaction", function () {