UNPKG

12.3 kBJavaScriptView Raw
1import { __assign, __extends } from "tslib";
2import { invariant } from "../../utilities/globals/index.js";
3import "./fixPolyfills.js";
4import { wrap } from 'optimism';
5import { equal } from '@wry/equality';
6import { ApolloCache } from "../core/cache.js";
7import { MissingFieldError } from "../core/types/common.js";
8import { addTypenameToDocument, isReference, } from "../../utilities/index.js";
9import { StoreReader } from "./readFromStore.js";
10import { StoreWriter } from "./writeToStore.js";
11import { EntityStore, supportsResultCaching } from "./entityStore.js";
12import { makeVar, forgetCache, recallCache } from "./reactiveVars.js";
13import { Policies } from "./policies.js";
14import { hasOwn, normalizeConfig, shouldCanonizeResults } from "./helpers.js";
15import { canonicalStringify } from "./object-canon.js";
16var InMemoryCache = (function (_super) {
17 __extends(InMemoryCache, _super);
18 function InMemoryCache(config) {
19 if (config === void 0) { config = {}; }
20 var _this = _super.call(this) || this;
21 _this.watches = new Set();
22 _this.typenameDocumentCache = new Map();
23 _this.makeVar = makeVar;
24 _this.txCount = 0;
25 _this.config = normalizeConfig(config);
26 _this.addTypename = !!_this.config.addTypename;
27 _this.policies = new Policies({
28 cache: _this,
29 dataIdFromObject: _this.config.dataIdFromObject,
30 possibleTypes: _this.config.possibleTypes,
31 typePolicies: _this.config.typePolicies,
32 });
33 _this.init();
34 return _this;
35 }
36 InMemoryCache.prototype.init = function () {
37 var rootStore = this.data = new EntityStore.Root({
38 policies: this.policies,
39 resultCaching: this.config.resultCaching,
40 });
41 this.optimisticData = rootStore.stump;
42 this.resetResultCache();
43 };
44 InMemoryCache.prototype.resetResultCache = function (resetResultIdentities) {
45 var _this = this;
46 var previousReader = this.storeReader;
47 var fragments = this.config.fragments;
48 this.storeWriter = new StoreWriter(this, this.storeReader = new StoreReader({
49 cache: this,
50 addTypename: this.addTypename,
51 resultCacheMaxSize: this.config.resultCacheMaxSize,
52 canonizeResults: shouldCanonizeResults(this.config),
53 canon: resetResultIdentities
54 ? void 0
55 : previousReader && previousReader.canon,
56 fragments: fragments,
57 }), fragments);
58 this.maybeBroadcastWatch = wrap(function (c, options) {
59 return _this.broadcastWatch(c, options);
60 }, {
61 max: this.config.resultCacheMaxSize,
62 makeCacheKey: function (c) {
63 var store = c.optimistic ? _this.optimisticData : _this.data;
64 if (supportsResultCaching(store)) {
65 var optimistic = c.optimistic, id = c.id, variables = c.variables;
66 return store.makeCacheKey(c.query, c.callback, canonicalStringify({ optimistic: optimistic, id: id, variables: variables }));
67 }
68 }
69 });
70 new Set([
71 this.data.group,
72 this.optimisticData.group,
73 ]).forEach(function (group) { return group.resetCaching(); });
74 };
75 InMemoryCache.prototype.restore = function (data) {
76 this.init();
77 if (data)
78 this.data.replace(data);
79 return this;
80 };
81 InMemoryCache.prototype.extract = function (optimistic) {
82 if (optimistic === void 0) { optimistic = false; }
83 return (optimistic ? this.optimisticData : this.data).extract();
84 };
85 InMemoryCache.prototype.read = function (options) {
86 var _a = options.returnPartialData, returnPartialData = _a === void 0 ? false : _a;
87 try {
88 return this.storeReader.diffQueryAgainstStore(__assign(__assign({}, options), { store: options.optimistic ? this.optimisticData : this.data, config: this.config, returnPartialData: returnPartialData })).result || null;
89 }
90 catch (e) {
91 if (e instanceof MissingFieldError) {
92 return null;
93 }
94 throw e;
95 }
96 };
97 InMemoryCache.prototype.write = function (options) {
98 try {
99 ++this.txCount;
100 return this.storeWriter.writeToStore(this.data, options);
101 }
102 finally {
103 if (!--this.txCount && options.broadcast !== false) {
104 this.broadcastWatches();
105 }
106 }
107 };
108 InMemoryCache.prototype.modify = function (options) {
109 if (hasOwn.call(options, "id") && !options.id) {
110 return false;
111 }
112 var store = options.optimistic
113 ? this.optimisticData
114 : this.data;
115 try {
116 ++this.txCount;
117 return store.modify(options.id || "ROOT_QUERY", options.fields);
118 }
119 finally {
120 if (!--this.txCount && options.broadcast !== false) {
121 this.broadcastWatches();
122 }
123 }
124 };
125 InMemoryCache.prototype.diff = function (options) {
126 return this.storeReader.diffQueryAgainstStore(__assign(__assign({}, options), { store: options.optimistic ? this.optimisticData : this.data, rootId: options.id || "ROOT_QUERY", config: this.config }));
127 };
128 InMemoryCache.prototype.watch = function (watch) {
129 var _this = this;
130 if (!this.watches.size) {
131 recallCache(this);
132 }
133 this.watches.add(watch);
134 if (watch.immediate) {
135 this.maybeBroadcastWatch(watch);
136 }
137 return function () {
138 if (_this.watches.delete(watch) && !_this.watches.size) {
139 forgetCache(_this);
140 }
141 _this.maybeBroadcastWatch.forget(watch);
142 };
143 };
144 InMemoryCache.prototype.gc = function (options) {
145 canonicalStringify.reset();
146 var ids = this.optimisticData.gc();
147 if (options && !this.txCount) {
148 if (options.resetResultCache) {
149 this.resetResultCache(options.resetResultIdentities);
150 }
151 else if (options.resetResultIdentities) {
152 this.storeReader.resetCanon();
153 }
154 }
155 return ids;
156 };
157 InMemoryCache.prototype.retain = function (rootId, optimistic) {
158 return (optimistic ? this.optimisticData : this.data).retain(rootId);
159 };
160 InMemoryCache.prototype.release = function (rootId, optimistic) {
161 return (optimistic ? this.optimisticData : this.data).release(rootId);
162 };
163 InMemoryCache.prototype.identify = function (object) {
164 if (isReference(object))
165 return object.__ref;
166 try {
167 return this.policies.identify(object)[0];
168 }
169 catch (e) {
170 __DEV__ && invariant.warn(e);
171 }
172 };
173 InMemoryCache.prototype.evict = function (options) {
174 if (!options.id) {
175 if (hasOwn.call(options, "id")) {
176 return false;
177 }
178 options = __assign(__assign({}, options), { id: "ROOT_QUERY" });
179 }
180 try {
181 ++this.txCount;
182 return this.optimisticData.evict(options, this.data);
183 }
184 finally {
185 if (!--this.txCount && options.broadcast !== false) {
186 this.broadcastWatches();
187 }
188 }
189 };
190 InMemoryCache.prototype.reset = function (options) {
191 var _this = this;
192 this.init();
193 canonicalStringify.reset();
194 if (options && options.discardWatches) {
195 this.watches.forEach(function (watch) { return _this.maybeBroadcastWatch.forget(watch); });
196 this.watches.clear();
197 forgetCache(this);
198 }
199 else {
200 this.broadcastWatches();
201 }
202 return Promise.resolve();
203 };
204 InMemoryCache.prototype.removeOptimistic = function (idToRemove) {
205 var newOptimisticData = this.optimisticData.removeLayer(idToRemove);
206 if (newOptimisticData !== this.optimisticData) {
207 this.optimisticData = newOptimisticData;
208 this.broadcastWatches();
209 }
210 };
211 InMemoryCache.prototype.batch = function (options) {
212 var _this = this;
213 var update = options.update, _a = options.optimistic, optimistic = _a === void 0 ? true : _a, removeOptimistic = options.removeOptimistic, onWatchUpdated = options.onWatchUpdated;
214 var updateResult;
215 var perform = function (layer) {
216 var _a = _this, data = _a.data, optimisticData = _a.optimisticData;
217 ++_this.txCount;
218 if (layer) {
219 _this.data = _this.optimisticData = layer;
220 }
221 try {
222 return updateResult = update(_this);
223 }
224 finally {
225 --_this.txCount;
226 _this.data = data;
227 _this.optimisticData = optimisticData;
228 }
229 };
230 var alreadyDirty = new Set();
231 if (onWatchUpdated && !this.txCount) {
232 this.broadcastWatches(__assign(__assign({}, options), { onWatchUpdated: function (watch) {
233 alreadyDirty.add(watch);
234 return false;
235 } }));
236 }
237 if (typeof optimistic === 'string') {
238 this.optimisticData = this.optimisticData.addLayer(optimistic, perform);
239 }
240 else if (optimistic === false) {
241 perform(this.data);
242 }
243 else {
244 perform();
245 }
246 if (typeof removeOptimistic === "string") {
247 this.optimisticData = this.optimisticData.removeLayer(removeOptimistic);
248 }
249 if (onWatchUpdated && alreadyDirty.size) {
250 this.broadcastWatches(__assign(__assign({}, options), { onWatchUpdated: function (watch, diff) {
251 var result = onWatchUpdated.call(this, watch, diff);
252 if (result !== false) {
253 alreadyDirty.delete(watch);
254 }
255 return result;
256 } }));
257 if (alreadyDirty.size) {
258 alreadyDirty.forEach(function (watch) { return _this.maybeBroadcastWatch.dirty(watch); });
259 }
260 }
261 else {
262 this.broadcastWatches(options);
263 }
264 return updateResult;
265 };
266 InMemoryCache.prototype.performTransaction = function (update, optimisticId) {
267 return this.batch({
268 update: update,
269 optimistic: optimisticId || (optimisticId !== null),
270 });
271 };
272 InMemoryCache.prototype.transformDocument = function (document) {
273 if (this.addTypename) {
274 var result = this.typenameDocumentCache.get(document);
275 if (!result) {
276 result = addTypenameToDocument(document);
277 this.typenameDocumentCache.set(document, result);
278 this.typenameDocumentCache.set(result, result);
279 }
280 return result;
281 }
282 return document;
283 };
284 InMemoryCache.prototype.transformForLink = function (document) {
285 var fragments = this.config.fragments;
286 return fragments
287 ? fragments.transform(document)
288 : document;
289 };
290 InMemoryCache.prototype.broadcastWatches = function (options) {
291 var _this = this;
292 if (!this.txCount) {
293 this.watches.forEach(function (c) { return _this.maybeBroadcastWatch(c, options); });
294 }
295 };
296 InMemoryCache.prototype.broadcastWatch = function (c, options) {
297 var lastDiff = c.lastDiff;
298 var diff = this.diff(c);
299 if (options) {
300 if (c.optimistic &&
301 typeof options.optimistic === "string") {
302 diff.fromOptimisticTransaction = true;
303 }
304 if (options.onWatchUpdated &&
305 options.onWatchUpdated.call(this, c, diff, lastDiff) === false) {
306 return;
307 }
308 }
309 if (!lastDiff || !equal(lastDiff.result, diff.result)) {
310 c.callback(c.lastDiff = diff, lastDiff);
311 }
312 };
313 return InMemoryCache;
314}(ApolloCache));
315export { InMemoryCache };
316//# sourceMappingURL=inMemoryCache.js.map
\No newline at end of file