UNPKG

11.1 kBJavaScriptView Raw
1import { __assign } from "tslib";
2import { equal } from "@wry/equality";
3import { DeepMerger } from "../utilities/index.js";
4import { mergeIncrementalData } from "../utilities/common/incrementalResult.js";
5import { reobserveCacheFirst } from "./ObservableQuery.js";
6import { isNonEmptyArray, graphQLResultHasError, canUseWeakMap, } from "../utilities/index.js";
7import { NetworkStatus, isNetworkRequestInFlight, } from "./networkStatus.js";
8;
9var destructiveMethodCounts = new (canUseWeakMap ? WeakMap : Map)();
10function wrapDestructiveCacheMethod(cache, methodName) {
11 var original = cache[methodName];
12 if (typeof original === "function") {
13 cache[methodName] = function () {
14 destructiveMethodCounts.set(cache, (destructiveMethodCounts.get(cache) + 1) % 1e15);
15 return original.apply(this, arguments);
16 };
17 }
18}
19function cancelNotifyTimeout(info) {
20 if (info["notifyTimeout"]) {
21 clearTimeout(info["notifyTimeout"]);
22 info["notifyTimeout"] = void 0;
23 }
24}
25var QueryInfo = (function () {
26 function QueryInfo(queryManager, queryId) {
27 if (queryId === void 0) { queryId = queryManager.generateQueryId(); }
28 this.queryId = queryId;
29 this.listeners = new Set();
30 this.document = null;
31 this.lastRequestId = 1;
32 this.subscriptions = new Set();
33 this.stopped = false;
34 this.dirty = false;
35 this.observableQuery = null;
36 var cache = this.cache = queryManager.cache;
37 if (!destructiveMethodCounts.has(cache)) {
38 destructiveMethodCounts.set(cache, 0);
39 wrapDestructiveCacheMethod(cache, "evict");
40 wrapDestructiveCacheMethod(cache, "modify");
41 wrapDestructiveCacheMethod(cache, "reset");
42 }
43 }
44 QueryInfo.prototype.init = function (query) {
45 var networkStatus = query.networkStatus || NetworkStatus.loading;
46 if (this.variables &&
47 this.networkStatus !== NetworkStatus.loading &&
48 !equal(this.variables, query.variables)) {
49 networkStatus = NetworkStatus.setVariables;
50 }
51 if (!equal(query.variables, this.variables)) {
52 this.lastDiff = void 0;
53 }
54 Object.assign(this, {
55 document: query.document,
56 variables: query.variables,
57 networkError: null,
58 graphQLErrors: this.graphQLErrors || [],
59 networkStatus: networkStatus,
60 });
61 if (query.observableQuery) {
62 this.setObservableQuery(query.observableQuery);
63 }
64 if (query.lastRequestId) {
65 this.lastRequestId = query.lastRequestId;
66 }
67 return this;
68 };
69 QueryInfo.prototype.reset = function () {
70 cancelNotifyTimeout(this);
71 this.dirty = false;
72 };
73 QueryInfo.prototype.getDiff = function (variables) {
74 if (variables === void 0) { variables = this.variables; }
75 var options = this.getDiffOptions(variables);
76 if (this.lastDiff && equal(options, this.lastDiff.options)) {
77 return this.lastDiff.diff;
78 }
79 this.updateWatch(this.variables = variables);
80 var oq = this.observableQuery;
81 if (oq && oq.options.fetchPolicy === "no-cache") {
82 return { complete: false };
83 }
84 var diff = this.cache.diff(options);
85 this.updateLastDiff(diff, options);
86 return diff;
87 };
88 QueryInfo.prototype.updateLastDiff = function (diff, options) {
89 this.lastDiff = diff ? {
90 diff: diff,
91 options: options || this.getDiffOptions(),
92 } : void 0;
93 };
94 QueryInfo.prototype.getDiffOptions = function (variables) {
95 var _a;
96 if (variables === void 0) { variables = this.variables; }
97 return {
98 query: this.document,
99 variables: variables,
100 returnPartialData: true,
101 optimistic: true,
102 canonizeResults: (_a = this.observableQuery) === null || _a === void 0 ? void 0 : _a.options.canonizeResults,
103 };
104 };
105 QueryInfo.prototype.setDiff = function (diff) {
106 var _this = this;
107 var oldDiff = this.lastDiff && this.lastDiff.diff;
108 this.updateLastDiff(diff);
109 if (!this.dirty &&
110 !equal(oldDiff && oldDiff.result, diff && diff.result)) {
111 this.dirty = true;
112 if (!this.notifyTimeout) {
113 this.notifyTimeout = setTimeout(function () { return _this.notify(); }, 0);
114 }
115 }
116 };
117 QueryInfo.prototype.setObservableQuery = function (oq) {
118 var _this = this;
119 if (oq === this.observableQuery)
120 return;
121 if (this.oqListener) {
122 this.listeners.delete(this.oqListener);
123 }
124 this.observableQuery = oq;
125 if (oq) {
126 oq["queryInfo"] = this;
127 this.listeners.add(this.oqListener = function () {
128 var diff = _this.getDiff();
129 if (diff.fromOptimisticTransaction) {
130 oq["observe"]();
131 }
132 else {
133 reobserveCacheFirst(oq);
134 }
135 });
136 }
137 else {
138 delete this.oqListener;
139 }
140 };
141 QueryInfo.prototype.notify = function () {
142 var _this = this;
143 cancelNotifyTimeout(this);
144 if (this.shouldNotify()) {
145 this.listeners.forEach(function (listener) { return listener(_this); });
146 }
147 this.dirty = false;
148 };
149 QueryInfo.prototype.shouldNotify = function () {
150 if (!this.dirty || !this.listeners.size) {
151 return false;
152 }
153 if (isNetworkRequestInFlight(this.networkStatus) &&
154 this.observableQuery) {
155 var fetchPolicy = this.observableQuery.options.fetchPolicy;
156 if (fetchPolicy !== "cache-only" &&
157 fetchPolicy !== "cache-and-network") {
158 return false;
159 }
160 }
161 return true;
162 };
163 QueryInfo.prototype.stop = function () {
164 if (!this.stopped) {
165 this.stopped = true;
166 this.reset();
167 this.cancel();
168 this.cancel = QueryInfo.prototype.cancel;
169 this.subscriptions.forEach(function (sub) { return sub.unsubscribe(); });
170 var oq = this.observableQuery;
171 if (oq)
172 oq.stopPolling();
173 }
174 };
175 QueryInfo.prototype.cancel = function () { };
176 QueryInfo.prototype.updateWatch = function (variables) {
177 var _this = this;
178 if (variables === void 0) { variables = this.variables; }
179 var oq = this.observableQuery;
180 if (oq && oq.options.fetchPolicy === "no-cache") {
181 return;
182 }
183 var watchOptions = __assign(__assign({}, this.getDiffOptions(variables)), { watcher: this, callback: function (diff) { return _this.setDiff(diff); } });
184 if (!this.lastWatch ||
185 !equal(watchOptions, this.lastWatch)) {
186 this.cancel();
187 this.cancel = this.cache.watch(this.lastWatch = watchOptions);
188 }
189 };
190 QueryInfo.prototype.resetLastWrite = function () {
191 this.lastWrite = void 0;
192 };
193 QueryInfo.prototype.shouldWrite = function (result, variables) {
194 var lastWrite = this.lastWrite;
195 return !(lastWrite &&
196 lastWrite.dmCount === destructiveMethodCounts.get(this.cache) &&
197 equal(variables, lastWrite.variables) &&
198 equal(result.data, lastWrite.result.data));
199 };
200 QueryInfo.prototype.markResult = function (result, document, options, cacheWriteBehavior) {
201 var _this = this;
202 var merger = new DeepMerger();
203 var graphQLErrors = isNonEmptyArray(result.errors)
204 ? result.errors.slice(0)
205 : [];
206 this.reset();
207 if ('incremental' in result && isNonEmptyArray(result.incremental)) {
208 var mergedData = mergeIncrementalData(this.getDiff().result, result);
209 result.data = mergedData;
210 }
211 else if ('hasNext' in result && result.hasNext) {
212 var diff = this.getDiff();
213 result.data = merger.merge(diff.result, result.data);
214 }
215 this.graphQLErrors = graphQLErrors;
216 if (options.fetchPolicy === 'no-cache') {
217 this.updateLastDiff({ result: result.data, complete: true }, this.getDiffOptions(options.variables));
218 }
219 else if (cacheWriteBehavior !== 0) {
220 if (shouldWriteResult(result, options.errorPolicy)) {
221 this.cache.performTransaction(function (cache) {
222 if (_this.shouldWrite(result, options.variables)) {
223 cache.writeQuery({
224 query: document,
225 data: result.data,
226 variables: options.variables,
227 overwrite: cacheWriteBehavior === 1,
228 });
229 _this.lastWrite = {
230 result: result,
231 variables: options.variables,
232 dmCount: destructiveMethodCounts.get(_this.cache),
233 };
234 }
235 else {
236 if (_this.lastDiff &&
237 _this.lastDiff.diff.complete) {
238 result.data = _this.lastDiff.diff.result;
239 return;
240 }
241 }
242 var diffOptions = _this.getDiffOptions(options.variables);
243 var diff = cache.diff(diffOptions);
244 if (!_this.stopped) {
245 _this.updateWatch(options.variables);
246 }
247 _this.updateLastDiff(diff, diffOptions);
248 if (diff.complete) {
249 result.data = diff.result;
250 }
251 });
252 }
253 else {
254 this.lastWrite = void 0;
255 }
256 }
257 };
258 QueryInfo.prototype.markReady = function () {
259 this.networkError = null;
260 return this.networkStatus = NetworkStatus.ready;
261 };
262 QueryInfo.prototype.markError = function (error) {
263 this.networkStatus = NetworkStatus.error;
264 this.lastWrite = void 0;
265 this.reset();
266 if (error.graphQLErrors) {
267 this.graphQLErrors = error.graphQLErrors;
268 }
269 if (error.networkError) {
270 this.networkError = error.networkError;
271 }
272 return error;
273 };
274 return QueryInfo;
275}());
276export { QueryInfo };
277export function shouldWriteResult(result, errorPolicy) {
278 if (errorPolicy === void 0) { errorPolicy = "none"; }
279 var ignoreErrors = errorPolicy === "ignore" ||
280 errorPolicy === "all";
281 var writeWithErrors = !graphQLResultHasError(result);
282 if (!writeWithErrors && ignoreErrors && result.data) {
283 writeWithErrors = true;
284 }
285 return writeWithErrors;
286}
287//# sourceMappingURL=QueryInfo.js.map
\No newline at end of file