From ce5fc67e7883f80c2da05d86003ff9d7f56b2717 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Tue, 1 Sep 2015 17:23:03 +0100 Subject: [PATCH] Update `dstore/Cache::put` 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`. --- Cache.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Cache.js b/Cache.js index b0ea0b3..76d79f6 100644 --- a/Cache.js +++ b/Cache.js @@ -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) {