apollo-cache-inmemory
Advanced tools
Comparing version 1.3.0-beta.10 to 1.3.0-beta.11
@@ -856,3 +856,3 @@ (function (global, factory) { | ||
_this.optimistic = []; | ||
_this.watches = []; | ||
_this.watches = new Set(); | ||
_this.typenameDocumentCache = new WeakMap(); | ||
@@ -872,2 +872,19 @@ _this.silenceBroadcast = false; | ||
_this.storeReader = new StoreReader(); | ||
var cache = _this; | ||
var maybeBroadcastWatch = cache.maybeBroadcastWatch; | ||
_this.maybeBroadcastWatch = wrap(function (c) { | ||
return maybeBroadcastWatch.call(_this, c); | ||
}, { | ||
makeCacheKey: function (c) { | ||
if (c.optimistic && cache.optimistic.length > 0) { | ||
return; | ||
} | ||
if (c.previousResult) { | ||
return; | ||
} | ||
if (cache.data instanceof DepTrackingCache) { | ||
return defaultMakeCacheKey(c.query, JSON.stringify(c.variables)); | ||
} | ||
} | ||
}); | ||
return _this; | ||
@@ -933,5 +950,5 @@ } | ||
var _this = this; | ||
this.watches.push(watch); | ||
this.watches.add(watch); | ||
return function () { | ||
_this.watches = _this.watches.filter(function (c) { return c !== watch; }); | ||
_this.watches.delete(watch); | ||
}; | ||
@@ -1026,14 +1043,19 @@ }; | ||
InMemoryCache.prototype.broadcastWatches = function () { | ||
var _this = this; | ||
if (this.silenceBroadcast) | ||
if (!this.silenceBroadcast) { | ||
this.watches.forEach(this.maybeBroadcastWatch, this); | ||
} | ||
}; | ||
InMemoryCache.prototype.maybeBroadcastWatch = function (c) { | ||
var previousResult = c.previousResult && c.previousResult(); | ||
var newData = this.diff({ | ||
query: c.query, | ||
variables: c.variables, | ||
previousResult: previousResult, | ||
optimistic: c.optimistic, | ||
}); | ||
if (previousResult && | ||
previousResult === newData.result) { | ||
return; | ||
this.watches.forEach(function (c) { | ||
var newData = _this.diff({ | ||
query: c.query, | ||
variables: c.variables, | ||
previousResult: c.previousResult && c.previousResult(), | ||
optimistic: c.optimistic, | ||
}); | ||
c.callback(newData); | ||
}); | ||
} | ||
c.callback(newData); | ||
}; | ||
@@ -1040,0 +1062,0 @@ return InMemoryCache; |
@@ -32,3 +32,4 @@ import { DocumentNode } from 'graphql'; | ||
protected broadcastWatches(): void; | ||
private maybeBroadcastWatch; | ||
} | ||
//# sourceMappingURL=inMemoryCache.d.ts.map |
@@ -30,3 +30,4 @@ var __extends = (this && this.__extends) || (function () { | ||
import { StoreReader } from './readFromStore'; | ||
import { defaultNormalizedCacheFactory } from './depTrackingCache'; | ||
import { defaultNormalizedCacheFactory, DepTrackingCache } from './depTrackingCache'; | ||
import { wrap, defaultMakeCacheKey } from "./optimism"; | ||
import { record } from './recordingCache'; | ||
@@ -55,3 +56,3 @@ var defaultConfig = { | ||
_this.optimistic = []; | ||
_this.watches = []; | ||
_this.watches = new Set(); | ||
_this.typenameDocumentCache = new WeakMap(); | ||
@@ -71,2 +72,19 @@ _this.silenceBroadcast = false; | ||
_this.storeReader = new StoreReader(); | ||
var cache = _this; | ||
var maybeBroadcastWatch = cache.maybeBroadcastWatch; | ||
_this.maybeBroadcastWatch = wrap(function (c) { | ||
return maybeBroadcastWatch.call(_this, c); | ||
}, { | ||
makeCacheKey: function (c) { | ||
if (c.optimistic && cache.optimistic.length > 0) { | ||
return; | ||
} | ||
if (c.previousResult) { | ||
return; | ||
} | ||
if (cache.data instanceof DepTrackingCache) { | ||
return defaultMakeCacheKey(c.query, JSON.stringify(c.variables)); | ||
} | ||
} | ||
}); | ||
return _this; | ||
@@ -132,5 +150,5 @@ } | ||
var _this = this; | ||
this.watches.push(watch); | ||
this.watches.add(watch); | ||
return function () { | ||
_this.watches = _this.watches.filter(function (c) { return c !== watch; }); | ||
_this.watches.delete(watch); | ||
}; | ||
@@ -225,14 +243,19 @@ }; | ||
InMemoryCache.prototype.broadcastWatches = function () { | ||
var _this = this; | ||
if (this.silenceBroadcast) | ||
if (!this.silenceBroadcast) { | ||
this.watches.forEach(this.maybeBroadcastWatch, this); | ||
} | ||
}; | ||
InMemoryCache.prototype.maybeBroadcastWatch = function (c) { | ||
var previousResult = c.previousResult && c.previousResult(); | ||
var newData = this.diff({ | ||
query: c.query, | ||
variables: c.variables, | ||
previousResult: previousResult, | ||
optimistic: c.optimistic, | ||
}); | ||
if (previousResult && | ||
previousResult === newData.result) { | ||
return; | ||
this.watches.forEach(function (c) { | ||
var newData = _this.diff({ | ||
query: c.query, | ||
variables: c.variables, | ||
previousResult: c.previousResult && c.previousResult(), | ||
optimistic: c.optimistic, | ||
}); | ||
c.callback(newData); | ||
}); | ||
} | ||
c.callback(newData); | ||
}; | ||
@@ -239,0 +262,0 @@ return InMemoryCache; |
{ | ||
"name": "apollo-cache-inmemory", | ||
"version": "1.3.0-beta.10", | ||
"version": "1.3.0-beta.11", | ||
"description": "Core abstract of Caching layer for Apollo Client", | ||
@@ -5,0 +5,0 @@ "author": "James Baxley <james@meteor.com>", |
@@ -18,4 +18,7 @@ import { DocumentNode } from 'graphql'; | ||
import { writeResultToStore } from './writeToStore'; | ||
import { StoreReader } from './readFromStore'; | ||
import { defaultNormalizedCacheFactory } from './depTrackingCache'; | ||
import { defaultNormalizedCacheFactory, DepTrackingCache } from './depTrackingCache'; | ||
import { wrap, defaultMakeCacheKey } from "./optimism"; | ||
import { record } from './recordingCache'; | ||
@@ -44,3 +47,3 @@ const defaultConfig: ApolloReducerConfig = { | ||
protected optimistic: OptimisticStoreItem[] = []; | ||
private watches: Cache.WatchOptions[] = []; | ||
private watches = new Set<Cache.WatchOptions>(); | ||
private addTypename: boolean; | ||
@@ -77,2 +80,33 @@ private typenameDocumentCache = new WeakMap<DocumentNode, DocumentNode>(); | ||
this.storeReader = new StoreReader(); | ||
const cache = this; | ||
const { maybeBroadcastWatch } = cache; | ||
this.maybeBroadcastWatch = wrap((c: Cache.WatchOptions) => { | ||
return maybeBroadcastWatch.call(this, c); | ||
}, { | ||
makeCacheKey(c: Cache.WatchOptions) { | ||
if (c.optimistic && cache.optimistic.length > 0) { | ||
// If we're reading optimistic data, it doesn't matter if this.data | ||
// is a DepTrackingCache, since it will be ignored. | ||
return; | ||
} | ||
if (c.previousResult) { | ||
// If a previousResult was provided, assume the caller would prefer | ||
// to compare the previous data to the new data to determine whether | ||
// to broadcast, so we should disable caching by returning here, to | ||
// give maybeBroadcastWatch a chance to do that comparison. | ||
return; | ||
} | ||
if (cache.data instanceof DepTrackingCache) { | ||
// Return a cache key (thus enabling caching) only if we're currently | ||
// using a data store that can track cache dependencies. | ||
return defaultMakeCacheKey( | ||
c.query, | ||
JSON.stringify(c.variables), | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -145,6 +179,6 @@ | ||
public watch(watch: Cache.WatchOptions): () => void { | ||
this.watches.push(watch); | ||
this.watches.add(watch); | ||
return () => { | ||
this.watches = this.watches.filter(c => c !== watch); | ||
this.watches.delete(watch); | ||
}; | ||
@@ -285,20 +319,26 @@ } | ||
protected broadcastWatches() { | ||
// Skip this when silenced (like inside a transaction) | ||
if (this.silenceBroadcast) return; | ||
if (!this.silenceBroadcast) { | ||
this.watches.forEach(this.maybeBroadcastWatch, this); | ||
} | ||
} | ||
// right now, we invalidate all queries whenever anything changes | ||
this.watches.forEach((c: Cache.WatchOptions) => { | ||
const newData = this.diff({ | ||
query: c.query, | ||
variables: c.variables, | ||
// This method is wrapped in the constructor so that it will be called only | ||
// if the data that would be broadcast has changed. | ||
private maybeBroadcastWatch(c: Cache.WatchOptions) { | ||
const previousResult = c.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, | ||
}); | ||
const newData = this.diff({ | ||
query: c.query, | ||
variables: c.variables, | ||
previousResult, | ||
optimistic: c.optimistic, | ||
}); | ||
c.callback(newData); | ||
}); | ||
if (previousResult && | ||
previousResult === newData.result) { | ||
return; | ||
} | ||
c.callback(newData); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
456588
9618