Repository navigation
Expand file tree
/
Copy pathinMemoryCache.ts
More file actions
355 lines (300 loc) · 11.3 KB
/
Copy pathinMemoryCache.ts
File metadata and controls
355 lines (300 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Make builtins like Map and Set safe to use with non-extensible objects.
import './fixPolyfills';
import { DocumentNode } from 'graphql';
import { Cache, ApolloCache, Transaction } from 'apollo-cache';
import { addTypenameToDocument, canUseWeakMap } from 'apollo-utilities';
import { wrap } from 'optimism';
import { invariant, InvariantError } from 'ts-invariant';
import { HeuristicFragmentMatcher } from './fragmentMatcher';
import {
ApolloReducerConfig,
NormalizedCache,
NormalizedCacheObject,
} from './types';
import { StoreReader } from './readFromStore';
import { StoreWriter } from './writeToStore';
import { DepTrackingCache } from './depTrackingCache';
import { KeyTrie } from 'optimism';
import { ObjectCache } from './objectCache';
export interface InMemoryCacheConfig extends ApolloReducerConfig {
resultCaching?: boolean;
freezeResults?: boolean;
}
const defaultConfig: InMemoryCacheConfig = {
fragmentMatcher: new HeuristicFragmentMatcher(),
dataIdFromObject: defaultDataIdFromObject,
addTypename: true,
resultCaching: true,
freezeResults: false,
};
export function defaultDataIdFromObject(result: any): string | null {
if (result.__typename) {
if (result.id !== undefined) {
return `${result.__typename}:${result.id}`;
}
if (result._id !== undefined) {
return `${result.__typename}:${result._id}`;
}
}
return null;
}
const hasOwn = Object.prototype.hasOwnProperty;
export class OptimisticCacheLayer extends ObjectCache {
constructor(
public readonly optimisticId: string,
// OptimisticCacheLayer objects always wrap some other parent cache, so
// this.parent should never be null.
public readonly parent: NormalizedCache,
public readonly transaction: Transaction<NormalizedCacheObject>,
) {
super(Object.create(null));
}
public toObject(): NormalizedCacheObject {
return {
...this.parent.toObject(),
...this.data,
};
}
// All the other accessor methods of ObjectCache work without knowing about
// this.parent, but the get method needs to be overridden to implement the
// fallback this.parent.get(dataId) behavior.
public get(dataId: string) {
return hasOwn.call(this.data, dataId)
? this.data[dataId]
: this.parent.get(dataId);
}
}
export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
private data: NormalizedCache;
private optimisticData: NormalizedCache;
protected config: InMemoryCacheConfig;
private watches = new Set<Cache.WatchOptions>();
private addTypename: boolean;
private typenameDocumentCache = new Map<DocumentNode, DocumentNode>();
private storeReader: StoreReader;
private storeWriter: StoreWriter;
private cacheKeyRoot = new KeyTrie<object>(canUseWeakMap);
// Set this while in a transaction to prevent broadcasts...
// don't forget to turn it back on!
private silenceBroadcast: boolean = false;
constructor(config: InMemoryCacheConfig = {}) {
super();
this.config = { ...defaultConfig, ...config };
// backwards compat
if ((this.config as any).customResolvers) {
invariant.warn(
'customResolvers have been renamed to cacheRedirects. Please update your config as we will be deprecating customResolvers in the next major version.',
);
this.config.cacheRedirects = (this.config as any).customResolvers;
}
if ((this.config as any).cacheResolvers) {
invariant.warn(
'cacheResolvers have been renamed to cacheRedirects. Please update your config as we will be deprecating cacheResolvers in the next major version.',
);
this.config.cacheRedirects = (this.config as any).cacheResolvers;
}
this.addTypename = !!this.config.addTypename;
// Passing { resultCaching: false } in the InMemoryCache constructor options
// will completely disable dependency tracking, which will improve memory
// usage but worsen the performance of repeated reads.
this.data = this.config.resultCaching
? new DepTrackingCache()
: new ObjectCache();
// When no optimistic writes are currently active, cache.optimisticData ===
// cache.data, so there are no additional layers on top of the actual data.
// When an optimistic update happens, this.optimisticData will become a
// linked list of OptimisticCacheLayer objects that terminates with the
// original this.data cache object.
this.optimisticData = this.data;
this.storeWriter = new StoreWriter();
this.storeReader = new StoreReader({
cacheKeyRoot: this.cacheKeyRoot,
freezeResults: config.freezeResults,
});
const cache = this;
const { maybeBroadcastWatch } = cache;
this.maybeBroadcastWatch = wrap((c: Cache.WatchOptions) => {
return maybeBroadcastWatch.call(this, c);
}, {
makeCacheKey(c: Cache.WatchOptions) {
if (c.optimistic) {
// 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 cache.cacheKeyRoot.lookup(
c.query,
JSON.stringify(c.variables),
);
}
}
});
}
public restore(data: NormalizedCacheObject): this {
if (data) this.data.replace(data);
return this;
}
public extract(optimistic: boolean = false): NormalizedCacheObject {
return (optimistic ? this.optimisticData : this.data).toObject();
}
public read<T>(options: Cache.ReadOptions): T | null {
if (typeof options.rootId === 'string' &&
typeof this.data.get(options.rootId) === 'undefined') {
return null;
}
const { fragmentMatcher } = this.config;
const fragmentMatcherFunction = fragmentMatcher && fragmentMatcher.match;
return this.storeReader.readQueryFromStore({
store: options.optimistic ? this.optimisticData : this.data,
query: this.transformDocument(options.query),
variables: options.variables,
rootId: options.rootId,
fragmentMatcherFunction,
previousResult: options.previousResult,
config: this.config,
}) || null;
}
public write(write: Cache.WriteOptions): void {
const { fragmentMatcher } = this.config;
const fragmentMatcherFunction = fragmentMatcher && fragmentMatcher.match;
this.storeWriter.writeResultToStore({
dataId: write.dataId,
result: write.result,
variables: write.variables,
document: this.transformDocument(write.query),
store: this.data,
dataIdFromObject: this.config.dataIdFromObject,
fragmentMatcherFunction,
});
this.broadcastWatches();
}
public diff<T>(query: Cache.DiffOptions): Cache.DiffResult<T> {
const { fragmentMatcher } = this.config;
const fragmentMatcherFunction = fragmentMatcher && fragmentMatcher.match;
return this.storeReader.diffQueryAgainstStore({
store: query.optimistic ? this.optimisticData : this.data,
query: this.transformDocument(query.query),
variables: query.variables,
returnPartialData: query.returnPartialData,
previousResult: query.previousResult,
fragmentMatcherFunction,
config: this.config,
});
}
public watch(watch: Cache.WatchOptions): () => void {
this.watches.add(watch);
return () => {
this.watches.delete(watch);
};
}
public evict(query: Cache.EvictOptions): Cache.EvictionResult {
throw new InvariantError(`eviction is not implemented on InMemory Cache`);
}
public reset(): Promise<void> {
this.data.clear();
this.broadcastWatches();
return Promise.resolve();
}
public removeOptimistic(idToRemove: string) {
const toReapply: OptimisticCacheLayer[] = [];
let removedCount = 0;
let layer = this.optimisticData;
while (layer instanceof OptimisticCacheLayer) {
if (layer.optimisticId === idToRemove) {
++removedCount;
} else {
toReapply.push(layer);
}
layer = layer.parent;
}
if (removedCount > 0) {
// Reset this.optimisticData to the first non-OptimisticCacheLayer object,
// which is almost certainly this.data.
this.optimisticData = layer;
// Reapply the layers whose optimistic IDs do not match the removed ID.
while (toReapply.length > 0) {
const layer = toReapply.pop()!;
this.performTransaction(layer.transaction, layer.optimisticId);
}
this.broadcastWatches();
}
}
public performTransaction(
transaction: Transaction<NormalizedCacheObject>,
// This parameter is not part of the performTransaction signature inherited
// from the ApolloCache abstract class, but it's useful because it saves us
// from duplicating this implementation in recordOptimisticTransaction.
optimisticId?: string,
) {
const { data, silenceBroadcast } = this;
this.silenceBroadcast = true;
if (typeof optimisticId === 'string') {
// Add a new optimistic layer and temporarily make this.data refer to
// that layer for the duration of the transaction.
this.data = this.optimisticData = new OptimisticCacheLayer(
// Note that there can be multiple layers with the same optimisticId.
// When removeOptimistic(id) is called for that id, all matching layers
// will be removed, and the remaining layers will be reapplied.
optimisticId,
this.optimisticData,
transaction,
);
}
try {
transaction(this);
} finally {
this.silenceBroadcast = silenceBroadcast;
this.data = data;
}
// This broadcast does nothing if this.silenceBroadcast is true.
this.broadcastWatches();
}
public recordOptimisticTransaction(
transaction: Transaction<NormalizedCacheObject>,
id: string,
) {
return this.performTransaction(transaction, id);
}
public transformDocument(document: DocumentNode): DocumentNode {
if (this.addTypename) {
let result = this.typenameDocumentCache.get(document);
if (!result) {
result = addTypenameToDocument(document);
this.typenameDocumentCache.set(document, result);
// If someone calls transformDocument and then mistakenly passes the
// result back into an API that also calls transformDocument, make sure
// we don't keep creating new query documents.
this.typenameDocumentCache.set(result, result);
}
return result;
}
return document;
}
protected broadcastWatches() {
if (!this.silenceBroadcast) {
this.watches.forEach(c => this.maybeBroadcastWatch(c));
}
}
// 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) {
c.callback(
this.diff({
query: c.query,
variables: c.variables,
previousResult: c.previousResult && c.previousResult(),
optimistic: c.optimistic,
}),
);
}
}