[Core] Update capability interfaces

Update capability interfaces for persistence and mutation to
track timestamps of both changes and persistence calls.
Helps distinguish when refreshes should be allowed, which in
turn will be used to support Overwrite behavior when
Save conflicts are detected. WTD-1033.
This commit is contained in:
Victor Woeltjen 2015-03-20 15:20:27 -07:00
parent 1583c871fd
commit 66fd899650
3 changed files with 34 additions and 25 deletions

View File

@ -162,7 +162,8 @@
},
{
"key": "mutation",
"implementation": "capabilities/MutationCapability.js"
"implementation": "capabilities/MutationCapability.js",
"depends": [ "now" ]
},
{
"key": "delegation",

View File

@ -50,7 +50,7 @@ define(
* which will expose this capability
* @constructor
*/
function MutationCapability(domainObject) {
function MutationCapability(now, domainObject) {
function mutate(mutator, timestamp) {
// Get the object's model and clone it, so the
@ -74,7 +74,7 @@ define(
copyValues(model, result);
}
model.modified = (typeof timestamp === 'number') ?
timestamp : Date.now();
timestamp : now();
}
// Report the result of the mutation

View File

@ -27,19 +27,29 @@ define(
// Update a domain object's model upon refresh
function updateModel(model) {
modified = model.modified;
var modified = model.modified;
return domainObject.useCapability("mutation", function () {
return model;
});
}, modified);
}
// For refresh; update a domain object model, only if there
// are no unsaved changes.
function maybeUpdateModel(model) {
// Only update the model if there are no pending changes
if (domainObject.getModel().modified === modified) {
updateModel(model);
}
function updatePersistenceTimestamp() {
var modified = domainObject.getModel().modified;
domainObject.useCapability("mutation", function (model) {
model.persisted = modified;
}, modified);
}
// Utility function for creating promise-like objects which
// resolve synchronously when possible
function fastPromise(value) {
return (value || {}).then ? value : {
then: function (callback) {
return fastPromise(callback(value));
}
};
}
return {
@ -50,18 +60,13 @@ define(
* if persistence is successful, and rejected
* if not.
*/
persist: function (hard) {
persist: function () {
updatePersistenceTimestamp();
return persistenceService.updateObject(
SPACE,
domainObject.getId(),
domainObject.getModel(),
{ check: !hard }
).then(function (value) {
if (value) {
modified = domainObject.getModel().modified;
}
return value;
});
domainObject.getModel()
);
},
/**
* Update this domain object to match the latest from
@ -69,12 +74,15 @@ define(
* @returns {Promise} a promise which will be resolved
* when the update is complete
*/
refresh: function (hard) {
return persistenceService.readObject(
SPACE,
domainObject.getId(),
{ cache: false } // Disallow cached reads
).then(hard ? updateModel : maybeUpdateModel);
refresh: function () {
var model = domainObject.getModel();
// Only update if we don't have unsaved changes
return (model.modified === model.persisted) ?
persistenceService.readObject(
SPACE,
domainObject.getId()
).then(updateModel) :
fastPromise(false);
},
/**
* Get the space in which this domain object is persisted;