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