apollo-cache-inmemory
Advanced tools
Comparing version 0.2.0-rc.2 to 0.2.0-rc.3
@@ -5,2 +5,3 @@ # Change log | ||
- Don't broadcast query watchers during a transaction (for example, while mutation results are being processed) [Issue #2221](https://github.com/apollographql/apollo-client/issues/2221) [PR #2358](https://github.com/apollographql/apollo-client/pull/2358) | ||
- `readQuery` and `readFragment` return now the result instead of `Cache.DiffResult` [PR #2320](https://github.com/apollographql/apollo-client/pull/2320) | ||
@@ -7,0 +8,0 @@ |
@@ -11,2 +11,3 @@ import { DocumentNode } from 'graphql'; | ||
private addTypename; | ||
private silenceBroadcast; | ||
constructor(config?: ApolloReducerConfig); | ||
@@ -13,0 +14,0 @@ restore(data: NormalizedCache): ApolloCache<NormalizedCache>; |
@@ -51,2 +51,3 @@ "use strict"; | ||
_this.watches = []; | ||
_this.silenceBroadcast = false; | ||
_this.config = __assign({}, defaultConfig, config); | ||
@@ -133,9 +134,16 @@ if (_this.config.customResolvers) | ||
InMemoryCache.prototype.performTransaction = function (transaction) { | ||
var alreadySilenced = this.silenceBroadcast; | ||
this.silenceBroadcast = true; | ||
transaction(this); | ||
if (!alreadySilenced) { | ||
this.silenceBroadcast = false; | ||
} | ||
this.broadcastWatches(); | ||
}; | ||
InMemoryCache.prototype.recordOptimisticTransaction = function (transaction, id) { | ||
this.silenceBroadcast = true; | ||
var before = this.extract(true); | ||
var orig = this.data; | ||
this.data = __assign({}, before); | ||
transaction(this); | ||
this.performTransaction(transaction); | ||
var after = this.data; | ||
@@ -154,2 +162,3 @@ this.data = orig; | ||
}); | ||
this.silenceBroadcast = false; | ||
this.broadcastWatches(); | ||
@@ -197,2 +206,4 @@ }; | ||
var _this = this; | ||
if (this.silenceBroadcast) | ||
return; | ||
this.watches.forEach(function (c) { | ||
@@ -202,3 +213,3 @@ var newData = _this.diff({ | ||
variables: c.variables, | ||
previousResult: c.previousResult(), | ||
previousResult: c.previousResult && c.previousResult(), | ||
optimistic: c.optimistic, | ||
@@ -205,0 +216,0 @@ }); |
@@ -118,6 +118,5 @@ "use strict"; | ||
var sameAsPreviousResult = Object.keys(idValue.previousResult).reduce(function (sameKeys, key) { return sameKeys && currentResultKeys_1.indexOf(key) > -1; }, true) && | ||
currentResultKeys_1.reduce(function (same, key) { | ||
return same && | ||
areNestedArrayItemsStrictlyEqual(resultFields[key], idValue.previousResult[key]); | ||
}, true); | ||
currentResultKeys_1.every(function (key) { | ||
return areNestedArrayItemsStrictlyEqual(resultFields[key], idValue.previousResult[key]); | ||
}); | ||
if (sameAsPreviousResult) { | ||
@@ -142,4 +141,4 @@ return idValue.previousResult; | ||
} | ||
return a.reduce(function (same, item, i) { return same && areNestedArrayItemsStrictlyEqual(item, b[i]); }, true); | ||
return a.every(function (item, i) { return areNestedArrayItemsStrictlyEqual(item, b[i]); }); | ||
} | ||
//# sourceMappingURL=readFromStore.js.map |
{ | ||
"name": "apollo-cache-inmemory", | ||
"version": "0.2.0-rc.2", | ||
"version": "0.2.0-rc.3", | ||
"description": "Core abstract of Caching layer for Apollo Client", | ||
@@ -38,5 +38,5 @@ "author": "James Baxley <james@meteor.com>", | ||
"dependencies": { | ||
"apollo-cache": "^0.2.0-rc.1", | ||
"apollo-utilities": "^0.2.0-rc.1", | ||
"graphql-anywhere": "^4.0.0-rc.1" | ||
"apollo-cache": "^0.2.0-rc.2", | ||
"apollo-utilities": "^0.2.0-rc.2", | ||
"graphql-anywhere": "^4.0.0-rc.2" | ||
}, | ||
@@ -46,4 +46,4 @@ "devDependencies": { | ||
"@types/jest": "21.1.2", | ||
"@types/lodash": "4.14.77", | ||
"browserify": "14.4.0", | ||
"@types/lodash": "4.14.78", | ||
"browserify": "14.5.0", | ||
"graphql": "0.11.7", | ||
@@ -50,0 +50,0 @@ "graphql-tag": "2.4.2", |
@@ -1,5 +0,5 @@ | ||
import { DataProxy } from 'apollo-cache'; | ||
import { ApolloCache } from 'apollo-cache'; | ||
import gql, { disableFragmentWarnings } from 'graphql-tag'; | ||
import { InMemoryCache, ApolloReducerConfig } from '..'; | ||
import { InMemoryCache, ApolloReducerConfig, NormalizedCache } from '..'; | ||
@@ -17,3 +17,3 @@ disableFragmentWarnings(); | ||
} = {}, | ||
): DataProxy { | ||
): ApolloCache<NormalizedCache> { | ||
return new InMemoryCache( | ||
@@ -1107,2 +1107,98 @@ config || { addTypename: false }, | ||
}); | ||
describe('performTransaction', () => { | ||
it('will not broadcast mid-transaction', () => { | ||
const cache = createCache(); | ||
let numBroadcasts = 0; | ||
const query = gql` | ||
{ | ||
a | ||
} | ||
`; | ||
cache.watch({ | ||
query, | ||
optimistic: false, | ||
callback: () => { | ||
numBroadcasts++; | ||
}, | ||
}); | ||
expect(numBroadcasts).toEqual(0); | ||
cache.performTransaction(proxy => { | ||
proxy.writeQuery({ | ||
data: { a: 1 }, | ||
query, | ||
}); | ||
expect(numBroadcasts).toEqual(0); | ||
proxy.writeQuery({ | ||
data: { a: 4, b: 5, c: 6 }, | ||
query: gql` | ||
{ | ||
a | ||
b | ||
c | ||
} | ||
`, | ||
}); | ||
expect(numBroadcasts).toEqual(0); | ||
}); | ||
expect(numBroadcasts).toEqual(1); | ||
}); | ||
}); | ||
describe('performOptimisticTransaction', () => { | ||
it('will only broadcast once', () => { | ||
const cache = createCache(); | ||
let numBroadcasts = 0; | ||
const query = gql` | ||
{ | ||
a | ||
} | ||
`; | ||
cache.watch({ | ||
query, | ||
optimistic: true, | ||
callback: () => { | ||
numBroadcasts++; | ||
}, | ||
}); | ||
expect(numBroadcasts).toEqual(0); | ||
cache.recordOptimisticTransaction(proxy => { | ||
proxy.writeQuery({ | ||
data: { a: 1 }, | ||
query, | ||
}); | ||
expect(numBroadcasts).toEqual(0); | ||
proxy.writeQuery({ | ||
data: { a: 4, b: 5, c: 6 }, | ||
query: gql` | ||
{ | ||
a | ||
b | ||
c | ||
} | ||
`, | ||
}); | ||
expect(numBroadcasts).toEqual(0); | ||
}, 1); | ||
expect(numBroadcasts).toEqual(1); | ||
}); | ||
}); | ||
}); |
@@ -15,4 +15,4 @@ import { cloneDeep, assign, omit } from 'lodash'; | ||
IdValue, | ||
addTypenameToDocument | ||
} from "apollo-utilities"; | ||
addTypenameToDocument, | ||
} from 'apollo-utilities'; | ||
@@ -1563,4 +1563,4 @@ import { | ||
name: 'Todo 1', | ||
description: 'Description 1' | ||
} | ||
description: 'Description 1', | ||
}, | ||
], | ||
@@ -1575,3 +1575,3 @@ }; | ||
dataIdFromObject: getIdField, | ||
fragmentMatcherFunction | ||
fragmentMatcherFunction, | ||
}); | ||
@@ -1578,0 +1578,0 @@ |
@@ -44,2 +44,6 @@ import { DocumentNode } from 'graphql'; | ||
// Set this while in a transaction to prevent broadcasts... | ||
// don't forget to turn it back on! | ||
private silenceBroadcast: boolean = false; | ||
constructor(config: ApolloReducerConfig = {}) { | ||
@@ -145,3 +149,15 @@ super(); | ||
// TODO: does this need to be different, or is this okay for an in-memory cache? | ||
let alreadySilenced = this.silenceBroadcast; | ||
this.silenceBroadcast = true; | ||
transaction(this); | ||
if (!alreadySilenced) { | ||
// Don't un-silence since this is a nested transaction | ||
// (for example, a transaction inside an optimistic record) | ||
this.silenceBroadcast = false; | ||
} | ||
this.broadcastWatches(); | ||
} | ||
@@ -153,2 +169,4 @@ | ||
) { | ||
this.silenceBroadcast = true; | ||
const before = this.extract(true); | ||
@@ -158,3 +176,3 @@ | ||
this.data = { ...before }; | ||
transaction(this); | ||
this.performTransaction(transaction); | ||
const after = this.data; | ||
@@ -177,2 +195,4 @@ this.data = orig; | ||
this.silenceBroadcast = false; | ||
this.broadcastWatches(); | ||
@@ -232,8 +252,14 @@ } | ||
private broadcastWatches() { | ||
// Skip this when silenced (like inside a transaction) | ||
if (this.silenceBroadcast) return; | ||
// right now, we invalidate all queries whenever anything changes | ||
this.watches.forEach(c => { | ||
this.watches.forEach((c: Cache.WatchOptions) => { | ||
const newData = this.diff({ | ||
query: c.query, | ||
variables: c.variables, | ||
previousResult: c.previousResult(), | ||
// TODO: previousResult isn't in the types - this will only work | ||
// with ObservableQuery which is in a different package | ||
previousResult: (c as any).previousResult && c.previousResult(), | ||
optimistic: c.optimistic, | ||
@@ -240,0 +266,0 @@ }); |
@@ -284,10 +284,7 @@ import graphqlAnywhere, { Resolver, ExecInfo } from 'graphql-anywhere'; | ||
// While we do a shallow comparison of objects, but we do a deep comparison of arrays. | ||
currentResultKeys.reduce( | ||
(same, key) => | ||
same && | ||
areNestedArrayItemsStrictlyEqual( | ||
resultFields[key], | ||
idValue.previousResult[key], | ||
), | ||
true, | ||
currentResultKeys.every(key => | ||
areNestedArrayItemsStrictlyEqual( | ||
resultFields[key], | ||
idValue.previousResult[key], | ||
), | ||
); | ||
@@ -335,6 +332,3 @@ | ||
// if they are equal. | ||
return a.reduce( | ||
(same, item, i) => same && areNestedArrayItemsStrictlyEqual(item, b[i]), | ||
true, | ||
); | ||
return a.every((item, i) => areNestedArrayItemsStrictlyEqual(item, b[i])); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
246453
7135
Updatedapollo-cache@^0.2.0-rc.2
Updatedapollo-utilities@^0.2.0-rc.2
Updatedgraphql-anywhere@^4.0.0-rc.2