Compare commits
1 Commits
remove-unu
...
refresh-ap
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6526f3d685 |
@@ -129,9 +129,7 @@ class MutableDomainObject {
|
|||||||
|
|
||||||
mutable.$observe('$_synchronize_model', (updatedObject) => {
|
mutable.$observe('$_synchronize_model', (updatedObject) => {
|
||||||
let clone = JSON.parse(JSON.stringify(updatedObject));
|
let clone = JSON.parse(JSON.stringify(updatedObject));
|
||||||
let deleted = _.difference(Object.keys(mutable), Object.keys(updatedObject));
|
utils.refresh(mutable, clone);
|
||||||
deleted.forEach((propertyName) => delete mutable[propertyName]);
|
|
||||||
Object.assign(mutable, clone);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return mutable;
|
return mutable;
|
||||||
|
|||||||
@@ -389,6 +389,23 @@ ObjectAPI.prototype.mutate = function (domainObject, path, value) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates a domain object based on its latest persisted state. Note that this will mutate the provided object.
|
||||||
|
* @param {module:openmct.DomainObject} domainObject an object to refresh from its persistence store
|
||||||
|
* @returns {Promise} the provided object, updated to reflect the latest persisted state of the object.
|
||||||
|
*/
|
||||||
|
ObjectAPI.prototype.refresh = async function (domainObject) {
|
||||||
|
const refreshedObject = await this.get(domainObject.identifier);
|
||||||
|
|
||||||
|
if (domainObject.isMutable) {
|
||||||
|
domainObject.$refresh(refreshedObject);
|
||||||
|
} else {
|
||||||
|
utils.refresh(domainObject, refreshedObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
return domainObject;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -223,6 +223,28 @@ describe("The Object API", () => {
|
|||||||
expect(testObject.name).toBe(MUTATED_NAME);
|
expect(testObject.name).toBe(MUTATED_NAME);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Provides a way of refreshing an object from the persistence store', () => {
|
||||||
|
const modifiedTestObject = JSON.parse(JSON.stringify(testObject));
|
||||||
|
const OTHER_ATTRIBUTE_VALUE = 'Modified value';
|
||||||
|
const NEW_ATTRIBUTE_VALUE = 'A new attribute';
|
||||||
|
modifiedTestObject.otherAttribute = OTHER_ATTRIBUTE_VALUE;
|
||||||
|
modifiedTestObject.newAttribute = NEW_ATTRIBUTE_VALUE;
|
||||||
|
delete modifiedTestObject.objectAttribute;
|
||||||
|
|
||||||
|
spyOn(objectAPI, 'get');
|
||||||
|
objectAPI.get.and.returnValue(Promise.resolve(modifiedTestObject));
|
||||||
|
|
||||||
|
expect(objectAPI.get).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
return objectAPI.refresh(testObject).then(() => {
|
||||||
|
expect(objectAPI.get).toHaveBeenCalledWith(testObject.identifier);
|
||||||
|
|
||||||
|
expect(testObject.otherAttribute).toEqual(OTHER_ATTRIBUTE_VALUE);
|
||||||
|
expect(testObject.newAttribute).toEqual(NEW_ATTRIBUTE_VALUE);
|
||||||
|
expect(testObject.objectAttribute).not.toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe ('uses a MutableDomainObject', () => {
|
describe ('uses a MutableDomainObject', () => {
|
||||||
it('and retains properties of original object ', function () {
|
it('and retains properties of original object ', function () {
|
||||||
expect(hasOwnProperty(mutable, 'identifier')).toBe(true);
|
expect(hasOwnProperty(mutable, 'identifier')).toBe(true);
|
||||||
|
|||||||
@@ -165,12 +165,19 @@ define([
|
|||||||
return identifierEquals(a.identifier, b.identifier);
|
return identifierEquals(a.identifier, b.identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function refresh(oldObject, newObject) {
|
||||||
|
let deleted = _.difference(Object.keys(oldObject), Object.keys(newObject));
|
||||||
|
deleted.forEach((propertyName) => delete oldObject[propertyName]);
|
||||||
|
Object.assign(oldObject, newObject);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
toOldFormat: toOldFormat,
|
toOldFormat: toOldFormat,
|
||||||
toNewFormat: toNewFormat,
|
toNewFormat: toNewFormat,
|
||||||
makeKeyString: makeKeyString,
|
makeKeyString: makeKeyString,
|
||||||
parseKeyString: parseKeyString,
|
parseKeyString: parseKeyString,
|
||||||
equals: objectEquals,
|
equals: objectEquals,
|
||||||
identifierEquals: identifierEquals
|
identifierEquals: identifierEquals,
|
||||||
|
refresh: refresh
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user