Skip to content

Commit

Permalink
Update dstore/Cache::put (SitePen#154)
Browse files Browse the repository at this point in the history
Previous behaviour was to remove the item while waiting for return
from the master store.  Now Cache will presume the update will
complete and only remove the item if the master store rejects the
`put`.

Fixes SitePen#117
  • Loading branch information
kitsonk authored May 24, 2017
1 parent ffb8550 commit a0e5169
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,20 @@ define([
});
},
put: function (object, directives) {
// first remove from the cache, so it is empty until we get a response from the master store
// first update the cache, we are going to assume the update is valid while we wait to
// hear from the master store
var cachingStore = this.cachingStore;
cachingStore.remove((directives && directives.id) || this.getIdentity(object));
return when(this.inherited(arguments), function (result) {
cachingStore.put(object, directives);
return when(this.inherited(arguments)).then(function (result) {
// now put result in cache
var cachedPutResult =
cachingStore.put(object && typeof result === 'object' ? result : object, directives);
// the result from the put should be dictated by the master store and be unaffected by the cachingStore,
// unless the master store doesn't implement put
return result || cachedPutResult;
}, function () {
// Assuming a rejection of a promise invalidates the local cache
cachingStore.remove((directives && directives.id) || this.getIdentity(object));
});
},
remove: function (id, directives) {
Expand Down

0 comments on commit a0e5169

Please sign in to comment.