Check that mutation happens only if model has changed (#3751)

* When a mutation is requested, the LegacyObjectAPIInterceptor triggers a second mutatation request - ensure that the model for this 2nd request has some diff from the current model before saving the object.

Co-authored-by: Andrew Henry <akhenry@gmail.com>
This commit is contained in:
Shefali Joshi 2021-05-20 10:01:14 -07:00 committed by GitHub
parent 605eeff9d7
commit aebb5df611
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View File

@ -141,11 +141,17 @@ define(
if (mutationResult !== false) {
// Copy values if result was a different object
// (either our clone or some other new thing)
if (model !== result) {
let modelHasChanged = _.isEqual(model, result) === false;
if (modelHasChanged) {
copyValues(model, result);
}
model.modified = useTimestamp ? timestamp : now();
if (modelHasChanged
|| (useTimestamp !== undefined)
|| (model.modified === undefined)) {
model.modified = useTimestamp ? timestamp : now();
}
notifyListeners(model);
}

View File

@ -91,7 +91,7 @@ export default class CouchObjectProvider {
* persist any queued objects
* @private
*/
checkResponse(response, intermediateResponse) {
checkResponse(response, intermediateResponse, key) {
let requestSuccess = false;
const id = response ? response.id : undefined;
let rev;
@ -113,6 +113,8 @@ export default class CouchObjectProvider {
if (this.objectQueue[id].hasNext()) {
this.updateQueued(id);
}
} else {
this.objectQueue[key].pending = false;
}
}
@ -132,8 +134,7 @@ export default class CouchObjectProvider {
}
//Sometimes CouchDB returns the old rev which fetching the object if there is a document update in progress
//Only update the rev if it's the first time we're getting the object from CouchDB. Subsequent revs should only be updated by updates.
if (!this.objectQueue[key].pending && !this.objectQueue[key].rev) {
if (!this.objectQueue[key].pending) {
this.objectQueue[key].updateRevision(response[REV]);
}
@ -458,7 +459,7 @@ export default class CouchObjectProvider {
const queued = this.objectQueue[key].dequeue();
let document = new CouchDocument(key, queued.model);
this.request(key, "PUT", document).then((response) => {
this.checkResponse(response, queued.intermediateResponse);
this.checkResponse(response, queued.intermediateResponse, key);
});
return intermediateResponse.promise;
@ -473,7 +474,7 @@ export default class CouchObjectProvider {
const queued = this.objectQueue[key].dequeue();
let document = new CouchDocument(key, queued.model, this.objectQueue[key].rev);
this.request(key, "PUT", document).then((response) => {
this.checkResponse(response, queued.intermediateResponse);
this.checkResponse(response, queued.intermediateResponse, key);
});
}
}