Skip to content

Commit

Permalink
Cache: Ensure add/put return a promise for read-only master stores
Browse files Browse the repository at this point in the history
Note that for simplicity's sake, this fix presumes that the master store
adheres to the documented behavior of the add and put APIs, i.e. that it
returns an object; if it returns a falsy value, this fix will end up
returning the result of add/put on the caching store in that case too.

This commit also updates RequestMemory tests to remove when calls and test
Cache APIs for promises.  (Without the fixes in Cache.js, the add/put tests
will fail.)

Conflicts:
	tests/RequestMemory.js
  • Loading branch information
kfranqueiro authored and kriszyp committed Mar 19, 2015
1 parent 9cc63ae commit 9a4a777
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
16 changes: 10 additions & 6 deletions Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ define([
return when(this.inherited(arguments), function (result) {
// now put result in cache (note we don't do add, because add may have
// called put() and already added it)
cachingStore.put(object && typeof result === 'object' ? result : object, directives);
// the result from the add should be dictated by the master store and be unaffected by the cachingStore
return result;
var cachedPutResult =
cachingStore.put(object && typeof result === 'object' ? result : object, directives);
// the result from the add should be dictated by the master store and be unaffected by the cachingStore,
// unless the master store doesn't implement add
return result || cachedPutResult;
});
},
put: function (object, directives) {
Expand All @@ -138,9 +140,11 @@ define([
cachingStore.remove((directives && directives.id) || this.getIdentity(object));
return when(this.inherited(arguments), function (result) {
// now put result in cache
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
return result;
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;
});
},
remove: function (id, directives) {
Expand Down
46 changes: 30 additions & 16 deletions tests/RequestMemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ define([
'intern!object',
'intern/chai!assert',
'require',
'dojo/when',
'dojo/_base/array',
'dojo/_base/declare',
'../RequestMemory',
'../Trackable'
], function (registerSuite, assert, require, when, arrayUtil, declare, RequestMemory, Trackable) {
], function (registerSuite, assert, require, arrayUtil, declare, RequestMemory, Trackable) {

var store;
function mapResultIds(results) {
Expand All @@ -25,7 +24,7 @@ define([
},

'.get': function () {
return when(store.get('node2'), function (item) {
return store.get('node2').then(function (item) {
assert.strictEqual(
JSON.stringify(item),
JSON.stringify({ 'id': 'node2', 'name':'node2', 'someProperty':'somePropertyB' })
Expand All @@ -35,11 +34,17 @@ define([

'.put': function () {
var updatedItem;
return when(store.get('node5')).then(function (item) {
var updateEventFired;
store.on('update', function () {
updateEventFired = true;
});
return store.get('node5').then(function (item) {
item.changed = true;
updatedItem = item;

return store.put(updatedItem);
var putResult = store.put(updatedItem);
assert.isDefined(putResult && putResult.then, 'put should return a promise');
return putResult;
}).then(function () {
return store.get('node5');
}).then(function (item) {
Expand All @@ -49,17 +54,27 @@ define([

'.add': function () {
var newItem = { 'id': 'node6', 'name':'node5', 'someProperty':'somePropertyB' };
return when(store.add(newItem), function () {
return when(store.get('node6'), function (item) {
assert.strictEqual(JSON.stringify(item), JSON.stringify(newItem));
});

var addEventFired;
store.on('add', function () {
addEventFired = true;
});
var addResult = store.add(newItem);
assert.isDefined(addResult && addResult.then, 'add should return a promise');
return addResult.then(function () {
return store.get('node6');
}).then(function (item) {
assert.strictEqual(JSON.stringify(item), JSON.stringify(newItem));
assert.isTrue(addEventFired);
});
},

'.remove': function () {
return when(store.get('node3')).then(function (item) {
return store.get('node3').then(function (item) {
assert.ok(item);
return store.remove('node3');
var removeResult = store.remove('node3');
assert.isDefined(removeResult && removeResult.then, 'remove should return a promise');
return removeResult;
}).then(function () {
return store.get('node3');
}).then(function (item) {
Expand All @@ -70,7 +85,7 @@ define([

'filter': function () {
var results = store.filter({ someProperty: 'somePropertyB' }).fetch().then(mapResultIds);
return when(results, function (data) {
return results.then(function (data) {
assert.deepEqual(data.slice(), [ 'node2', 'node5' ]);
});
},
Expand All @@ -80,14 +95,14 @@ define([
{ property: 'someProperty', descending: true },
{ property: 'name', descending: false }
]).fetch().then(mapResultIds);
return when(results, function (data) {
return results.then(function (data) {
assert.deepEqual(data.slice(), [ 'node3', 'node2', 'node5', 'node1', 'node4' ]);
});
},

'.fetchRange': function () {
var results = store.fetchRange({start: 1, end: 4}).then(mapResultIds);
return when(results, function (data) {
return results.then(function (data) {
assert.deepEqual(data.slice(), [ 'node2', 'node3', 'node4' ]);
});
},
Expand All @@ -99,10 +114,9 @@ define([
})
.sort('name', true)
.fetchRange({start: 1, end: 3}).then(mapResultIds);
return when(results, function (data) {
return results.then(function (data) {
assert.deepEqual(data.slice(), [ 'node3', 'node1' ]);
});
}
});
});

0 comments on commit 9a4a777

Please sign in to comment.