Skip to content

Commit

Permalink
fix: update default willCommit semantics to make race conditions slig…
Browse files Browse the repository at this point in the history
…htly safer
  • Loading branch information
runspired committed Oct 18, 2023
1 parent 34c0df0 commit 46c36a1
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/json-api/src/-private/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,40 @@ export default class JSONAPICache implements Cache {
*/
willCommit(identifier: StableRecordIdentifier): void {
const cached = this.__peek(identifier, false);

/*
if we have multiple saves in flight at once then
we have information loss no matter what. This
attempts to lose the least information.
If we were to clear inflightAttrs, previous requests
would not be able to use it during their didCommit.
If we upsert inflightattrs, previous requests incorrectly
see more recent inflight changes as part of their own and
will incorrectly mark the new state as the correct remote state.
We choose this latter behavior to avoid accidentally removing
earlier changes.
If apps do not want this behavior they can either
- chain save requests serially vs allowing concurrent saves
- move to using a request handler that caches the inflight state
on a per-request basis
- change their save requests to only send a "PATCH" instead of a "PUT"
so that only latest changes are involved in each request, and then also
ensure that the API or their handler reflects only those changes back
for upsert into the cache.
*/
if (cached.inflightAttrs) {
if (!cached.localAttrs) {
return;
}
Object.assign(cached.inflightAttrs, cached.localAttrs);
cached.localAttrs = null;
return;
}
cached.inflightAttrs = cached.localAttrs;
cached.localAttrs = null;
}
Expand Down

0 comments on commit 46c36a1

Please sign in to comment.