1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | require("./fixPolyfills");
|
5 | var apollo_cache_1 = require("apollo-cache");
|
6 | var apollo_utilities_1 = require("apollo-utilities");
|
7 | var optimism_1 = require("optimism");
|
8 | var ts_invariant_1 = require("ts-invariant");
|
9 | var fragmentMatcher_1 = require("./fragmentMatcher");
|
10 | var readFromStore_1 = require("./readFromStore");
|
11 | var writeToStore_1 = require("./writeToStore");
|
12 | var depTrackingCache_1 = require("./depTrackingCache");
|
13 | var optimism_2 = require("optimism");
|
14 | var objectCache_1 = require("./objectCache");
|
15 | var defaultConfig = {
|
16 | fragmentMatcher: new fragmentMatcher_1.HeuristicFragmentMatcher(),
|
17 | dataIdFromObject: defaultDataIdFromObject,
|
18 | addTypename: true,
|
19 | resultCaching: true,
|
20 | freezeResults: false,
|
21 | };
|
22 | function defaultDataIdFromObject(result) {
|
23 | if (result.__typename) {
|
24 | if (result.id !== undefined) {
|
25 | return result.__typename + ":" + result.id;
|
26 | }
|
27 | if (result._id !== undefined) {
|
28 | return result.__typename + ":" + result._id;
|
29 | }
|
30 | }
|
31 | return null;
|
32 | }
|
33 | exports.defaultDataIdFromObject = defaultDataIdFromObject;
|
34 | var hasOwn = Object.prototype.hasOwnProperty;
|
35 | var OptimisticCacheLayer = (function (_super) {
|
36 | tslib_1.__extends(OptimisticCacheLayer, _super);
|
37 | function OptimisticCacheLayer(optimisticId, parent, transaction) {
|
38 | var _this = _super.call(this, Object.create(null)) || this;
|
39 | _this.optimisticId = optimisticId;
|
40 | _this.parent = parent;
|
41 | _this.transaction = transaction;
|
42 | return _this;
|
43 | }
|
44 | OptimisticCacheLayer.prototype.toObject = function () {
|
45 | return tslib_1.__assign(tslib_1.__assign({}, this.parent.toObject()), this.data);
|
46 | };
|
47 | OptimisticCacheLayer.prototype.get = function (dataId) {
|
48 | return hasOwn.call(this.data, dataId)
|
49 | ? this.data[dataId]
|
50 | : this.parent.get(dataId);
|
51 | };
|
52 | return OptimisticCacheLayer;
|
53 | }(objectCache_1.ObjectCache));
|
54 | exports.OptimisticCacheLayer = OptimisticCacheLayer;
|
55 | var InMemoryCache = (function (_super) {
|
56 | tslib_1.__extends(InMemoryCache, _super);
|
57 | function InMemoryCache(config) {
|
58 | if (config === void 0) { config = {}; }
|
59 | var _this = _super.call(this) || this;
|
60 | _this.watches = new Set();
|
61 | _this.typenameDocumentCache = new Map();
|
62 | _this.cacheKeyRoot = new optimism_2.KeyTrie(apollo_utilities_1.canUseWeakMap);
|
63 | _this.silenceBroadcast = false;
|
64 | _this.config = tslib_1.__assign(tslib_1.__assign({}, defaultConfig), config);
|
65 | if (_this.config.customResolvers) {
|
66 | ts_invariant_1.invariant.warn('customResolvers have been renamed to cacheRedirects. Please update your config as we will be deprecating customResolvers in the next major version.');
|
67 | _this.config.cacheRedirects = _this.config.customResolvers;
|
68 | }
|
69 | if (_this.config.cacheResolvers) {
|
70 | ts_invariant_1.invariant.warn('cacheResolvers have been renamed to cacheRedirects. Please update your config as we will be deprecating cacheResolvers in the next major version.');
|
71 | _this.config.cacheRedirects = _this.config.cacheResolvers;
|
72 | }
|
73 | _this.addTypename = !!_this.config.addTypename;
|
74 | _this.data = _this.config.resultCaching
|
75 | ? new depTrackingCache_1.DepTrackingCache()
|
76 | : new objectCache_1.ObjectCache();
|
77 | _this.optimisticData = _this.data;
|
78 | _this.storeWriter = new writeToStore_1.StoreWriter();
|
79 | _this.storeReader = new readFromStore_1.StoreReader({
|
80 | cacheKeyRoot: _this.cacheKeyRoot,
|
81 | freezeResults: config.freezeResults,
|
82 | });
|
83 | var cache = _this;
|
84 | var maybeBroadcastWatch = cache.maybeBroadcastWatch;
|
85 | _this.maybeBroadcastWatch = optimism_1.wrap(function (c) {
|
86 | return maybeBroadcastWatch.call(_this, c);
|
87 | }, {
|
88 | makeCacheKey: function (c) {
|
89 | if (c.optimistic) {
|
90 | return;
|
91 | }
|
92 | if (c.previousResult) {
|
93 | return;
|
94 | }
|
95 | if (cache.data instanceof depTrackingCache_1.DepTrackingCache) {
|
96 | return cache.cacheKeyRoot.lookup(c.query, JSON.stringify(c.variables));
|
97 | }
|
98 | }
|
99 | });
|
100 | return _this;
|
101 | }
|
102 | InMemoryCache.prototype.restore = function (data) {
|
103 | if (data)
|
104 | this.data.replace(data);
|
105 | return this;
|
106 | };
|
107 | InMemoryCache.prototype.extract = function (optimistic) {
|
108 | if (optimistic === void 0) { optimistic = false; }
|
109 | return (optimistic ? this.optimisticData : this.data).toObject();
|
110 | };
|
111 | InMemoryCache.prototype.read = function (options) {
|
112 | if (typeof options.rootId === 'string' &&
|
113 | typeof this.data.get(options.rootId) === 'undefined') {
|
114 | return null;
|
115 | }
|
116 | var fragmentMatcher = this.config.fragmentMatcher;
|
117 | var fragmentMatcherFunction = fragmentMatcher && fragmentMatcher.match;
|
118 | return this.storeReader.readQueryFromStore({
|
119 | store: options.optimistic ? this.optimisticData : this.data,
|
120 | query: this.transformDocument(options.query),
|
121 | variables: options.variables,
|
122 | rootId: options.rootId,
|
123 | fragmentMatcherFunction: fragmentMatcherFunction,
|
124 | previousResult: options.previousResult,
|
125 | config: this.config,
|
126 | }) || null;
|
127 | };
|
128 | InMemoryCache.prototype.write = function (write) {
|
129 | var fragmentMatcher = this.config.fragmentMatcher;
|
130 | var fragmentMatcherFunction = fragmentMatcher && fragmentMatcher.match;
|
131 | this.storeWriter.writeResultToStore({
|
132 | dataId: write.dataId,
|
133 | result: write.result,
|
134 | variables: write.variables,
|
135 | document: this.transformDocument(write.query),
|
136 | store: this.data,
|
137 | dataIdFromObject: this.config.dataIdFromObject,
|
138 | fragmentMatcherFunction: fragmentMatcherFunction,
|
139 | });
|
140 | this.broadcastWatches();
|
141 | };
|
142 | InMemoryCache.prototype.diff = function (query) {
|
143 | var fragmentMatcher = this.config.fragmentMatcher;
|
144 | var fragmentMatcherFunction = fragmentMatcher && fragmentMatcher.match;
|
145 | return this.storeReader.diffQueryAgainstStore({
|
146 | store: query.optimistic ? this.optimisticData : this.data,
|
147 | query: this.transformDocument(query.query),
|
148 | variables: query.variables,
|
149 | returnPartialData: query.returnPartialData,
|
150 | previousResult: query.previousResult,
|
151 | fragmentMatcherFunction: fragmentMatcherFunction,
|
152 | config: this.config,
|
153 | });
|
154 | };
|
155 | InMemoryCache.prototype.watch = function (watch) {
|
156 | var _this = this;
|
157 | this.watches.add(watch);
|
158 | return function () {
|
159 | _this.watches.delete(watch);
|
160 | };
|
161 | };
|
162 | InMemoryCache.prototype.evict = function (query) {
|
163 | throw new ts_invariant_1.InvariantError("eviction is not implemented on InMemory Cache");
|
164 | };
|
165 | InMemoryCache.prototype.reset = function () {
|
166 | this.data.clear();
|
167 | this.broadcastWatches();
|
168 | return Promise.resolve();
|
169 | };
|
170 | InMemoryCache.prototype.removeOptimistic = function (idToRemove) {
|
171 | var toReapply = [];
|
172 | var removedCount = 0;
|
173 | var layer = this.optimisticData;
|
174 | while (layer instanceof OptimisticCacheLayer) {
|
175 | if (layer.optimisticId === idToRemove) {
|
176 | ++removedCount;
|
177 | }
|
178 | else {
|
179 | toReapply.push(layer);
|
180 | }
|
181 | layer = layer.parent;
|
182 | }
|
183 | if (removedCount > 0) {
|
184 | this.optimisticData = layer;
|
185 | while (toReapply.length > 0) {
|
186 | var layer_1 = toReapply.pop();
|
187 | this.performTransaction(layer_1.transaction, layer_1.optimisticId);
|
188 | }
|
189 | this.broadcastWatches();
|
190 | }
|
191 | };
|
192 | InMemoryCache.prototype.performTransaction = function (transaction, optimisticId) {
|
193 | var _a = this, data = _a.data, silenceBroadcast = _a.silenceBroadcast;
|
194 | this.silenceBroadcast = true;
|
195 | if (typeof optimisticId === 'string') {
|
196 | this.data = this.optimisticData = new OptimisticCacheLayer(optimisticId, this.optimisticData, transaction);
|
197 | }
|
198 | try {
|
199 | transaction(this);
|
200 | }
|
201 | finally {
|
202 | this.silenceBroadcast = silenceBroadcast;
|
203 | this.data = data;
|
204 | }
|
205 | this.broadcastWatches();
|
206 | };
|
207 | InMemoryCache.prototype.recordOptimisticTransaction = function (transaction, id) {
|
208 | return this.performTransaction(transaction, id);
|
209 | };
|
210 | InMemoryCache.prototype.transformDocument = function (document) {
|
211 | if (this.addTypename) {
|
212 | var result = this.typenameDocumentCache.get(document);
|
213 | if (!result) {
|
214 | result = apollo_utilities_1.addTypenameToDocument(document);
|
215 | this.typenameDocumentCache.set(document, result);
|
216 | this.typenameDocumentCache.set(result, result);
|
217 | }
|
218 | return result;
|
219 | }
|
220 | return document;
|
221 | };
|
222 | InMemoryCache.prototype.broadcastWatches = function () {
|
223 | var _this = this;
|
224 | if (!this.silenceBroadcast) {
|
225 | this.watches.forEach(function (c) { return _this.maybeBroadcastWatch(c); });
|
226 | }
|
227 | };
|
228 | InMemoryCache.prototype.maybeBroadcastWatch = function (c) {
|
229 | c.callback(this.diff({
|
230 | query: c.query,
|
231 | variables: c.variables,
|
232 | previousResult: c.previousResult && c.previousResult(),
|
233 | optimistic: c.optimistic,
|
234 | }));
|
235 | };
|
236 | return InMemoryCache;
|
237 | }(apollo_cache_1.ApolloCache));
|
238 | exports.InMemoryCache = InMemoryCache;
|
239 |
|
\ | No newline at end of file |