@bevelo/query 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +72 -2
- package/dist/broadcast-DfOq-zMG.d.ts +485 -0
- package/dist/broadcast-G0JvCc7n.d.cts +485 -0
- package/dist/index-C7f4d6Gg.d.cts +485 -0
- package/dist/index-C7f4d6Gg.d.ts +485 -0
- package/dist/key-BtYrMAlu.js +282 -0
- package/dist/key-DS_LgCI1.cjs +371 -0
- package/dist/live/broadcast.cjs +68 -0
- package/dist/live/broadcast.d.cts +2 -0
- package/dist/live/broadcast.d.ts +2 -0
- package/dist/live/broadcast.js +67 -0
- package/dist/live/index.cjs +2184 -0
- package/dist/live/index.d.cts +3 -0
- package/dist/live/index.d.ts +3 -0
- package/dist/live/index.js +2127 -0
- package/dist/live/persist.cjs +91 -0
- package/dist/live/persist.d.cts +2 -0
- package/dist/live/persist.d.ts +2 -0
- package/dist/live/persist.js +89 -0
- package/dist/pure/index.cjs +525 -0
- package/dist/pure/index.d.cts +2 -0
- package/dist/pure/index.d.ts +2 -0
- package/dist/pure/index.js +494 -0
- package/package.json +53 -3
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
// src/pure/policy.ts
|
|
2
|
+
/** Resolves dynamic or static staleness window into milliseconds (`Infinity` for static). */
|
|
3
|
+
function resolveStaleMs(staleTime, state) {
|
|
4
|
+
const resolved = typeof staleTime === "function" ? staleTime(state) : staleTime;
|
|
5
|
+
if (resolved === "static") return Infinity;
|
|
6
|
+
return resolved ?? 0;
|
|
7
|
+
}
|
|
8
|
+
/** Returns true when a query entry must be treated as stale at `now`. */
|
|
9
|
+
function isEntryStale(state, staleTime, now) {
|
|
10
|
+
if (state.data === void 0 && state.status === "pending") return true;
|
|
11
|
+
if (state.invalidated) return true;
|
|
12
|
+
const windowMs = resolveStaleMs(staleTime, state);
|
|
13
|
+
if (windowMs === Infinity) return false;
|
|
14
|
+
if (windowMs <= 0) return true;
|
|
15
|
+
if (state.dataUpdatedAt === 0) return true;
|
|
16
|
+
return now - state.dataUpdatedAt >= windowMs;
|
|
17
|
+
}
|
|
18
|
+
/** Decides whether another attempt should run after `failureCount` failures (1-based after failure). */
|
|
19
|
+
function shouldRetryAttempt(policy, failureCount, error, isServer) {
|
|
20
|
+
if (policy === false) return false;
|
|
21
|
+
if (typeof policy === "function") return policy(failureCount, error);
|
|
22
|
+
if (policy === true) return true;
|
|
23
|
+
return failureCount <= (typeof policy === "number" ? policy : isServer ? 0 : 3);
|
|
24
|
+
}
|
|
25
|
+
/** Computes exponential backoff in milliseconds, capped at 30 seconds by default. */
|
|
26
|
+
function computeBackoffMs(delay, attemptIndex, error) {
|
|
27
|
+
if (typeof delay === "function") return Math.max(0, delay(attemptIndex, error));
|
|
28
|
+
if (typeof delay === "number") return Math.max(0, delay);
|
|
29
|
+
return Math.min(1e3 * 2 ** attemptIndex, 3e4);
|
|
30
|
+
}
|
|
31
|
+
/** Determines whether an attempt can execute immediately or should pause while offline. */
|
|
32
|
+
function resolveNetworkAction(policy, online, hasRunOnce) {
|
|
33
|
+
const mode = policy ?? "online";
|
|
34
|
+
if (mode === "always") return "run";
|
|
35
|
+
if (online) return "run";
|
|
36
|
+
if (mode === "offline-first" && !hasRunOnce) return "run";
|
|
37
|
+
return "pause";
|
|
38
|
+
}
|
|
39
|
+
/** Applies a `maxPages` sliding window after appending/prepending a page. */
|
|
40
|
+
function clampPages(data, maxPages, direction) {
|
|
41
|
+
if (maxPages === void 0 || maxPages <= 0 || data.pages.length <= maxPages) return data;
|
|
42
|
+
const excess = data.pages.length - maxPages;
|
|
43
|
+
if (direction === "forward") return {
|
|
44
|
+
pages: data.pages.slice(excess),
|
|
45
|
+
pageParams: data.pageParams.slice(excess)
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
pages: data.pages.slice(0, maxPages),
|
|
49
|
+
pageParams: data.pageParams.slice(0, maxPages)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** Reusable builder that tags `queryKey` with phantom data and error types. */
|
|
53
|
+
function querySpec(spec) {
|
|
54
|
+
return spec;
|
|
55
|
+
}
|
|
56
|
+
/** Reusable infinite query builder that tags `queryKey` with `PageData` types. */
|
|
57
|
+
function infiniteQuerySpec(spec) {
|
|
58
|
+
return spec;
|
|
59
|
+
}
|
|
60
|
+
/** Identity helper for typing reusable mutation specifications. */
|
|
61
|
+
function mutationSpec(spec) {
|
|
62
|
+
return spec;
|
|
63
|
+
}
|
|
64
|
+
// src/pure/key.ts
|
|
65
|
+
/** Checks whether `value` is a plain JSON-style record object. */
|
|
66
|
+
function isPlainObject(value) {
|
|
67
|
+
if (typeof value !== "object" || value === null) return false;
|
|
68
|
+
const proto = Object.getPrototypeOf(value);
|
|
69
|
+
return proto === Object.prototype || proto === null;
|
|
70
|
+
}
|
|
71
|
+
function normalizeForKey(val) {
|
|
72
|
+
if (val === null || typeof val !== "object") return val;
|
|
73
|
+
if (Array.isArray(val)) {
|
|
74
|
+
const len = val.length;
|
|
75
|
+
let copy;
|
|
76
|
+
for (let i = 0; i < len; i += 1) {
|
|
77
|
+
const item = val[i];
|
|
78
|
+
const next = normalizeForKey(item);
|
|
79
|
+
if (copy !== void 0) copy[i] = next;
|
|
80
|
+
else if (!Object.is(next, item)) {
|
|
81
|
+
copy = new Array(len);
|
|
82
|
+
for (let j = 0; j < i; j += 1) copy[j] = val[j];
|
|
83
|
+
copy[i] = next;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return copy ?? val;
|
|
87
|
+
}
|
|
88
|
+
if (!isPlainObject(val)) return val;
|
|
89
|
+
const keys = Object.keys(val);
|
|
90
|
+
const len = keys.length;
|
|
91
|
+
if (len === 0) return val;
|
|
92
|
+
if (len === 1) {
|
|
93
|
+
const k0 = keys[0];
|
|
94
|
+
const v0 = val[k0];
|
|
95
|
+
const next0 = normalizeForKey(v0);
|
|
96
|
+
return Object.is(next0, v0) ? val : { [k0]: next0 };
|
|
97
|
+
}
|
|
98
|
+
let sorted = true;
|
|
99
|
+
for (let i = 1; i < len; i += 1) if (keys[i - 1] > keys[i]) {
|
|
100
|
+
sorted = false;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
if (sorted) {
|
|
104
|
+
let out;
|
|
105
|
+
for (let i = 0; i < len; i += 1) {
|
|
106
|
+
const k = keys[i];
|
|
107
|
+
const v = val[k];
|
|
108
|
+
const next = normalizeForKey(v);
|
|
109
|
+
if (out !== void 0) out[k] = next;
|
|
110
|
+
else if (!Object.is(next, v)) {
|
|
111
|
+
out = {};
|
|
112
|
+
for (let j = 0; j < i; j += 1) {
|
|
113
|
+
const prevK = keys[j];
|
|
114
|
+
out[prevK] = val[prevK];
|
|
115
|
+
}
|
|
116
|
+
out[k] = next;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return out ?? val;
|
|
120
|
+
}
|
|
121
|
+
keys.sort();
|
|
122
|
+
const out = {};
|
|
123
|
+
for (let i = 0; i < len; i += 1) {
|
|
124
|
+
const k = keys[i];
|
|
125
|
+
out[k] = normalizeForKey(val[k]);
|
|
126
|
+
}
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
function isSimpleJsonString(value) {
|
|
130
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
131
|
+
const code = value.charCodeAt(i);
|
|
132
|
+
if (code < 32 || code === 34 || code === 92 || code >= 55296 && code <= 57343) return false;
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
function fastJsonScalar(value) {
|
|
137
|
+
if (typeof value === "string") return isSimpleJsonString(value) ? `"${value}"` : void 0;
|
|
138
|
+
if (typeof value === "number") return Number.isFinite(value) ? value === 0 ? "0" : String(value) : "null";
|
|
139
|
+
if (typeof value === "boolean") return value ? "true" : "false";
|
|
140
|
+
if (value === null) return "null";
|
|
141
|
+
}
|
|
142
|
+
function fastJsonPrimitive(value) {
|
|
143
|
+
return value === void 0 ? "null" : fastJsonScalar(value);
|
|
144
|
+
}
|
|
145
|
+
function stringifySimplePlainObject(obj) {
|
|
146
|
+
const keys = Object.keys(obj);
|
|
147
|
+
const len = keys.length;
|
|
148
|
+
if (len === 0) return "{}";
|
|
149
|
+
if (len === 2) {
|
|
150
|
+
if (keys[0] > keys[1]) {
|
|
151
|
+
const tmp = keys[0];
|
|
152
|
+
keys[0] = keys[1];
|
|
153
|
+
keys[1] = tmp;
|
|
154
|
+
}
|
|
155
|
+
} else if (len === 3) {
|
|
156
|
+
if (keys[0] > keys[1]) {
|
|
157
|
+
const tmp = keys[0];
|
|
158
|
+
keys[0] = keys[1];
|
|
159
|
+
keys[1] = tmp;
|
|
160
|
+
}
|
|
161
|
+
if (keys[1] > keys[2]) {
|
|
162
|
+
const tmp = keys[1];
|
|
163
|
+
keys[1] = keys[2];
|
|
164
|
+
keys[2] = tmp;
|
|
165
|
+
}
|
|
166
|
+
if (keys[0] > keys[1]) {
|
|
167
|
+
const tmp = keys[0];
|
|
168
|
+
keys[0] = keys[1];
|
|
169
|
+
keys[1] = tmp;
|
|
170
|
+
}
|
|
171
|
+
} else if (len > 3) {
|
|
172
|
+
let sorted = true;
|
|
173
|
+
for (let i = 1; i < len; i += 1) if (keys[i - 1] > keys[i]) {
|
|
174
|
+
sorted = false;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
if (!sorted) keys.sort();
|
|
178
|
+
}
|
|
179
|
+
let body = "";
|
|
180
|
+
for (let i = 0; i < len; i += 1) {
|
|
181
|
+
const k = keys[i];
|
|
182
|
+
if (!isSimpleJsonString(k)) return void 0;
|
|
183
|
+
const v = obj[k];
|
|
184
|
+
if (v === void 0) continue;
|
|
185
|
+
const valStr = fastJsonScalar(v);
|
|
186
|
+
if (valStr === void 0) return void 0;
|
|
187
|
+
body = body === "" ? `"${k}":${valStr}` : `${body},"${k}":${valStr}`;
|
|
188
|
+
}
|
|
189
|
+
return `{${body}}`;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Serializes a `QueryKey` into a deterministic hash string by sorting plain
|
|
193
|
+
* object keys recursively. Object property order never affects cache identity.
|
|
194
|
+
*/
|
|
195
|
+
function serializeKey(key) {
|
|
196
|
+
const len = key.length;
|
|
197
|
+
if (len === 1) {
|
|
198
|
+
const p0 = fastJsonPrimitive(key[0]);
|
|
199
|
+
if (p0 !== void 0) return `[${p0}]`;
|
|
200
|
+
} else if (len === 2) {
|
|
201
|
+
const p0 = fastJsonPrimitive(key[0]);
|
|
202
|
+
if (p0 !== void 0) {
|
|
203
|
+
const k1 = key[1];
|
|
204
|
+
const p1 = fastJsonPrimitive(k1);
|
|
205
|
+
if (p1 !== void 0) return `[${p0},${p1}]`;
|
|
206
|
+
if (isPlainObject(k1)) {
|
|
207
|
+
const objStr = stringifySimplePlainObject(k1);
|
|
208
|
+
if (objStr !== void 0) return `[${p0},${objStr}]`;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
} else if (len === 3) {
|
|
212
|
+
const p0 = fastJsonPrimitive(key[0]);
|
|
213
|
+
if (p0 !== void 0) {
|
|
214
|
+
const p1 = fastJsonPrimitive(key[1]);
|
|
215
|
+
if (p1 !== void 0) {
|
|
216
|
+
const p2 = fastJsonPrimitive(key[2]);
|
|
217
|
+
if (p2 !== void 0) return `[${p0},${p1},${p2}]`;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
for (let i = 0; i < len; i += 1) {
|
|
222
|
+
const item = key[i];
|
|
223
|
+
if (item !== null && typeof item !== "string" && typeof item !== "number" && typeof item !== "boolean") return JSON.stringify(normalizeForKey(key));
|
|
224
|
+
}
|
|
225
|
+
return JSON.stringify(key);
|
|
226
|
+
}
|
|
227
|
+
/** Deep prefix comparison between a candidate value and a filter pattern. */
|
|
228
|
+
function partialMatchValue(target, pattern) {
|
|
229
|
+
if (Object.is(target, pattern)) return true;
|
|
230
|
+
if (typeof target !== typeof pattern) return false;
|
|
231
|
+
if (target === null || pattern === null) return false;
|
|
232
|
+
if (Array.isArray(target) && Array.isArray(pattern)) {
|
|
233
|
+
if (pattern.length > target.length) return false;
|
|
234
|
+
for (let i = 0; i < pattern.length; i += 1) if (!partialMatchValue(target[i], pattern[i])) return false;
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
if (isPlainObject(target) && isPlainObject(pattern)) {
|
|
238
|
+
for (const key of Object.keys(pattern)) {
|
|
239
|
+
if (!Object.prototype.hasOwnProperty.call(target, key)) return false;
|
|
240
|
+
if (!partialMatchValue(target[key], pattern[key])) return false;
|
|
241
|
+
}
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
/** Returns true when `target` begins with `prefix` under structural matching. */
|
|
247
|
+
function keyStartsWith(target, prefix) {
|
|
248
|
+
return partialMatchValue(target, prefix);
|
|
249
|
+
}
|
|
250
|
+
/** Evaluates whether a query record matches a `QueryFilter`. */
|
|
251
|
+
function matchesQueryFilter(record, filter, now, precomputedExactHash) {
|
|
252
|
+
if (!filter) return true;
|
|
253
|
+
const { queryKey, exact = false, type = "all", stale, fetchPhase, predicate } = filter;
|
|
254
|
+
if (queryKey !== void 0) {
|
|
255
|
+
if (exact) {
|
|
256
|
+
if (record.queryHash !== (precomputedExactHash ?? serializeKey(queryKey))) return false;
|
|
257
|
+
} else if (!keyStartsWith(record.queryKey, queryKey)) return false;
|
|
258
|
+
}
|
|
259
|
+
if (type === "active" && record.observerCount === 0) return false;
|
|
260
|
+
if (type === "inactive" && record.observerCount > 0) return false;
|
|
261
|
+
if (stale !== void 0) {
|
|
262
|
+
if (isEntryStale(record.state, record.staleTime ?? 0, now) !== stale) return false;
|
|
263
|
+
}
|
|
264
|
+
if (fetchPhase !== void 0 && record.state.fetchPhase !== fetchPhase) return false;
|
|
265
|
+
if (predicate && !predicate(record)) return false;
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
/** Evaluates whether a mutation record matches a `MutationFilter`. */
|
|
269
|
+
function matchesMutationFilter(record, filter, precomputedExactHash) {
|
|
270
|
+
if (!filter) return true;
|
|
271
|
+
const { mutationKey, exact = false, status, predicate } = filter;
|
|
272
|
+
if (mutationKey !== void 0) {
|
|
273
|
+
if (record.mutationKey === void 0) return false;
|
|
274
|
+
if (exact) {
|
|
275
|
+
if ((record.mutationHash ?? serializeKey(record.mutationKey)) !== (precomputedExactHash ?? serializeKey(mutationKey))) return false;
|
|
276
|
+
} else if (!keyStartsWith(record.mutationKey, mutationKey)) return false;
|
|
277
|
+
}
|
|
278
|
+
if (status !== void 0 && record.state.status !== status) return false;
|
|
279
|
+
if (predicate && !predicate(record)) return false;
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
export { partialMatchValue as a, computeBackoffMs as c, mutationSpec as d, querySpec as f, shouldRetryAttempt as h, matchesQueryFilter as i, infiniteQuerySpec as l, resolveStaleMs as m, keyStartsWith as n, serializeKey as o, resolveNetworkAction as p, matchesMutationFilter as r, clampPages as s, isPlainObject as t, isEntryStale as u };
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
// src/pure/policy.ts
|
|
2
|
+
/** Resolves dynamic or static staleness window into milliseconds (`Infinity` for static). */
|
|
3
|
+
function resolveStaleMs(staleTime, state) {
|
|
4
|
+
const resolved = typeof staleTime === "function" ? staleTime(state) : staleTime;
|
|
5
|
+
if (resolved === "static") return Infinity;
|
|
6
|
+
return resolved ?? 0;
|
|
7
|
+
}
|
|
8
|
+
/** Returns true when a query entry must be treated as stale at `now`. */
|
|
9
|
+
function isEntryStale(state, staleTime, now) {
|
|
10
|
+
if (state.data === void 0 && state.status === "pending") return true;
|
|
11
|
+
if (state.invalidated) return true;
|
|
12
|
+
const windowMs = resolveStaleMs(staleTime, state);
|
|
13
|
+
if (windowMs === Infinity) return false;
|
|
14
|
+
if (windowMs <= 0) return true;
|
|
15
|
+
if (state.dataUpdatedAt === 0) return true;
|
|
16
|
+
return now - state.dataUpdatedAt >= windowMs;
|
|
17
|
+
}
|
|
18
|
+
/** Decides whether another attempt should run after `failureCount` failures (1-based after failure). */
|
|
19
|
+
function shouldRetryAttempt(policy, failureCount, error, isServer) {
|
|
20
|
+
if (policy === false) return false;
|
|
21
|
+
if (typeof policy === "function") return policy(failureCount, error);
|
|
22
|
+
if (policy === true) return true;
|
|
23
|
+
return failureCount <= (typeof policy === "number" ? policy : isServer ? 0 : 3);
|
|
24
|
+
}
|
|
25
|
+
/** Computes exponential backoff in milliseconds, capped at 30 seconds by default. */
|
|
26
|
+
function computeBackoffMs(delay, attemptIndex, error) {
|
|
27
|
+
if (typeof delay === "function") return Math.max(0, delay(attemptIndex, error));
|
|
28
|
+
if (typeof delay === "number") return Math.max(0, delay);
|
|
29
|
+
return Math.min(1e3 * 2 ** attemptIndex, 3e4);
|
|
30
|
+
}
|
|
31
|
+
/** Determines whether an attempt can execute immediately or should pause while offline. */
|
|
32
|
+
function resolveNetworkAction(policy, online, hasRunOnce) {
|
|
33
|
+
const mode = policy ?? "online";
|
|
34
|
+
if (mode === "always") return "run";
|
|
35
|
+
if (online) return "run";
|
|
36
|
+
if (mode === "offline-first" && !hasRunOnce) return "run";
|
|
37
|
+
return "pause";
|
|
38
|
+
}
|
|
39
|
+
/** Applies a `maxPages` sliding window after appending/prepending a page. */
|
|
40
|
+
function clampPages(data, maxPages, direction) {
|
|
41
|
+
if (maxPages === void 0 || maxPages <= 0 || data.pages.length <= maxPages) return data;
|
|
42
|
+
const excess = data.pages.length - maxPages;
|
|
43
|
+
if (direction === "forward") return {
|
|
44
|
+
pages: data.pages.slice(excess),
|
|
45
|
+
pageParams: data.pageParams.slice(excess)
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
pages: data.pages.slice(0, maxPages),
|
|
49
|
+
pageParams: data.pageParams.slice(0, maxPages)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** Reusable builder that tags `queryKey` with phantom data and error types. */
|
|
53
|
+
function querySpec(spec) {
|
|
54
|
+
return spec;
|
|
55
|
+
}
|
|
56
|
+
/** Reusable infinite query builder that tags `queryKey` with `PageData` types. */
|
|
57
|
+
function infiniteQuerySpec(spec) {
|
|
58
|
+
return spec;
|
|
59
|
+
}
|
|
60
|
+
/** Identity helper for typing reusable mutation specifications. */
|
|
61
|
+
function mutationSpec(spec) {
|
|
62
|
+
return spec;
|
|
63
|
+
}
|
|
64
|
+
// src/pure/key.ts
|
|
65
|
+
/** Checks whether `value` is a plain JSON-style record object. */
|
|
66
|
+
function isPlainObject(value) {
|
|
67
|
+
if (typeof value !== "object" || value === null) return false;
|
|
68
|
+
const proto = Object.getPrototypeOf(value);
|
|
69
|
+
return proto === Object.prototype || proto === null;
|
|
70
|
+
}
|
|
71
|
+
function normalizeForKey(val) {
|
|
72
|
+
if (val === null || typeof val !== "object") return val;
|
|
73
|
+
if (Array.isArray(val)) {
|
|
74
|
+
const len = val.length;
|
|
75
|
+
let copy;
|
|
76
|
+
for (let i = 0; i < len; i += 1) {
|
|
77
|
+
const item = val[i];
|
|
78
|
+
const next = normalizeForKey(item);
|
|
79
|
+
if (copy !== void 0) copy[i] = next;
|
|
80
|
+
else if (!Object.is(next, item)) {
|
|
81
|
+
copy = new Array(len);
|
|
82
|
+
for (let j = 0; j < i; j += 1) copy[j] = val[j];
|
|
83
|
+
copy[i] = next;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return copy ?? val;
|
|
87
|
+
}
|
|
88
|
+
if (!isPlainObject(val)) return val;
|
|
89
|
+
const keys = Object.keys(val);
|
|
90
|
+
const len = keys.length;
|
|
91
|
+
if (len === 0) return val;
|
|
92
|
+
if (len === 1) {
|
|
93
|
+
const k0 = keys[0];
|
|
94
|
+
const v0 = val[k0];
|
|
95
|
+
const next0 = normalizeForKey(v0);
|
|
96
|
+
return Object.is(next0, v0) ? val : { [k0]: next0 };
|
|
97
|
+
}
|
|
98
|
+
let sorted = true;
|
|
99
|
+
for (let i = 1; i < len; i += 1) if (keys[i - 1] > keys[i]) {
|
|
100
|
+
sorted = false;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
if (sorted) {
|
|
104
|
+
let out;
|
|
105
|
+
for (let i = 0; i < len; i += 1) {
|
|
106
|
+
const k = keys[i];
|
|
107
|
+
const v = val[k];
|
|
108
|
+
const next = normalizeForKey(v);
|
|
109
|
+
if (out !== void 0) out[k] = next;
|
|
110
|
+
else if (!Object.is(next, v)) {
|
|
111
|
+
out = {};
|
|
112
|
+
for (let j = 0; j < i; j += 1) {
|
|
113
|
+
const prevK = keys[j];
|
|
114
|
+
out[prevK] = val[prevK];
|
|
115
|
+
}
|
|
116
|
+
out[k] = next;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return out ?? val;
|
|
120
|
+
}
|
|
121
|
+
keys.sort();
|
|
122
|
+
const out = {};
|
|
123
|
+
for (let i = 0; i < len; i += 1) {
|
|
124
|
+
const k = keys[i];
|
|
125
|
+
out[k] = normalizeForKey(val[k]);
|
|
126
|
+
}
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
function isSimpleJsonString(value) {
|
|
130
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
131
|
+
const code = value.charCodeAt(i);
|
|
132
|
+
if (code < 32 || code === 34 || code === 92 || code >= 55296 && code <= 57343) return false;
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
function fastJsonScalar(value) {
|
|
137
|
+
if (typeof value === "string") return isSimpleJsonString(value) ? `"${value}"` : void 0;
|
|
138
|
+
if (typeof value === "number") return Number.isFinite(value) ? value === 0 ? "0" : String(value) : "null";
|
|
139
|
+
if (typeof value === "boolean") return value ? "true" : "false";
|
|
140
|
+
if (value === null) return "null";
|
|
141
|
+
}
|
|
142
|
+
function fastJsonPrimitive(value) {
|
|
143
|
+
return value === void 0 ? "null" : fastJsonScalar(value);
|
|
144
|
+
}
|
|
145
|
+
function stringifySimplePlainObject(obj) {
|
|
146
|
+
const keys = Object.keys(obj);
|
|
147
|
+
const len = keys.length;
|
|
148
|
+
if (len === 0) return "{}";
|
|
149
|
+
if (len === 2) {
|
|
150
|
+
if (keys[0] > keys[1]) {
|
|
151
|
+
const tmp = keys[0];
|
|
152
|
+
keys[0] = keys[1];
|
|
153
|
+
keys[1] = tmp;
|
|
154
|
+
}
|
|
155
|
+
} else if (len === 3) {
|
|
156
|
+
if (keys[0] > keys[1]) {
|
|
157
|
+
const tmp = keys[0];
|
|
158
|
+
keys[0] = keys[1];
|
|
159
|
+
keys[1] = tmp;
|
|
160
|
+
}
|
|
161
|
+
if (keys[1] > keys[2]) {
|
|
162
|
+
const tmp = keys[1];
|
|
163
|
+
keys[1] = keys[2];
|
|
164
|
+
keys[2] = tmp;
|
|
165
|
+
}
|
|
166
|
+
if (keys[0] > keys[1]) {
|
|
167
|
+
const tmp = keys[0];
|
|
168
|
+
keys[0] = keys[1];
|
|
169
|
+
keys[1] = tmp;
|
|
170
|
+
}
|
|
171
|
+
} else if (len > 3) {
|
|
172
|
+
let sorted = true;
|
|
173
|
+
for (let i = 1; i < len; i += 1) if (keys[i - 1] > keys[i]) {
|
|
174
|
+
sorted = false;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
if (!sorted) keys.sort();
|
|
178
|
+
}
|
|
179
|
+
let body = "";
|
|
180
|
+
for (let i = 0; i < len; i += 1) {
|
|
181
|
+
const k = keys[i];
|
|
182
|
+
if (!isSimpleJsonString(k)) return void 0;
|
|
183
|
+
const v = obj[k];
|
|
184
|
+
if (v === void 0) continue;
|
|
185
|
+
const valStr = fastJsonScalar(v);
|
|
186
|
+
if (valStr === void 0) return void 0;
|
|
187
|
+
body = body === "" ? `"${k}":${valStr}` : `${body},"${k}":${valStr}`;
|
|
188
|
+
}
|
|
189
|
+
return `{${body}}`;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Serializes a `QueryKey` into a deterministic hash string by sorting plain
|
|
193
|
+
* object keys recursively. Object property order never affects cache identity.
|
|
194
|
+
*/
|
|
195
|
+
function serializeKey(key) {
|
|
196
|
+
const len = key.length;
|
|
197
|
+
if (len === 1) {
|
|
198
|
+
const p0 = fastJsonPrimitive(key[0]);
|
|
199
|
+
if (p0 !== void 0) return `[${p0}]`;
|
|
200
|
+
} else if (len === 2) {
|
|
201
|
+
const p0 = fastJsonPrimitive(key[0]);
|
|
202
|
+
if (p0 !== void 0) {
|
|
203
|
+
const k1 = key[1];
|
|
204
|
+
const p1 = fastJsonPrimitive(k1);
|
|
205
|
+
if (p1 !== void 0) return `[${p0},${p1}]`;
|
|
206
|
+
if (isPlainObject(k1)) {
|
|
207
|
+
const objStr = stringifySimplePlainObject(k1);
|
|
208
|
+
if (objStr !== void 0) return `[${p0},${objStr}]`;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
} else if (len === 3) {
|
|
212
|
+
const p0 = fastJsonPrimitive(key[0]);
|
|
213
|
+
if (p0 !== void 0) {
|
|
214
|
+
const p1 = fastJsonPrimitive(key[1]);
|
|
215
|
+
if (p1 !== void 0) {
|
|
216
|
+
const p2 = fastJsonPrimitive(key[2]);
|
|
217
|
+
if (p2 !== void 0) return `[${p0},${p1},${p2}]`;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
for (let i = 0; i < len; i += 1) {
|
|
222
|
+
const item = key[i];
|
|
223
|
+
if (item !== null && typeof item !== "string" && typeof item !== "number" && typeof item !== "boolean") return JSON.stringify(normalizeForKey(key));
|
|
224
|
+
}
|
|
225
|
+
return JSON.stringify(key);
|
|
226
|
+
}
|
|
227
|
+
/** Deep prefix comparison between a candidate value and a filter pattern. */
|
|
228
|
+
function partialMatchValue(target, pattern) {
|
|
229
|
+
if (Object.is(target, pattern)) return true;
|
|
230
|
+
if (typeof target !== typeof pattern) return false;
|
|
231
|
+
if (target === null || pattern === null) return false;
|
|
232
|
+
if (Array.isArray(target) && Array.isArray(pattern)) {
|
|
233
|
+
if (pattern.length > target.length) return false;
|
|
234
|
+
for (let i = 0; i < pattern.length; i += 1) if (!partialMatchValue(target[i], pattern[i])) return false;
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
if (isPlainObject(target) && isPlainObject(pattern)) {
|
|
238
|
+
for (const key of Object.keys(pattern)) {
|
|
239
|
+
if (!Object.prototype.hasOwnProperty.call(target, key)) return false;
|
|
240
|
+
if (!partialMatchValue(target[key], pattern[key])) return false;
|
|
241
|
+
}
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
/** Returns true when `target` begins with `prefix` under structural matching. */
|
|
247
|
+
function keyStartsWith(target, prefix) {
|
|
248
|
+
return partialMatchValue(target, prefix);
|
|
249
|
+
}
|
|
250
|
+
/** Evaluates whether a query record matches a `QueryFilter`. */
|
|
251
|
+
function matchesQueryFilter(record, filter, now, precomputedExactHash) {
|
|
252
|
+
if (!filter) return true;
|
|
253
|
+
const { queryKey, exact = false, type = "all", stale, fetchPhase, predicate } = filter;
|
|
254
|
+
if (queryKey !== void 0) {
|
|
255
|
+
if (exact) {
|
|
256
|
+
if (record.queryHash !== (precomputedExactHash ?? serializeKey(queryKey))) return false;
|
|
257
|
+
} else if (!keyStartsWith(record.queryKey, queryKey)) return false;
|
|
258
|
+
}
|
|
259
|
+
if (type === "active" && record.observerCount === 0) return false;
|
|
260
|
+
if (type === "inactive" && record.observerCount > 0) return false;
|
|
261
|
+
if (stale !== void 0) {
|
|
262
|
+
if (isEntryStale(record.state, record.staleTime ?? 0, now) !== stale) return false;
|
|
263
|
+
}
|
|
264
|
+
if (fetchPhase !== void 0 && record.state.fetchPhase !== fetchPhase) return false;
|
|
265
|
+
if (predicate && !predicate(record)) return false;
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
/** Evaluates whether a mutation record matches a `MutationFilter`. */
|
|
269
|
+
function matchesMutationFilter(record, filter, precomputedExactHash) {
|
|
270
|
+
if (!filter) return true;
|
|
271
|
+
const { mutationKey, exact = false, status, predicate } = filter;
|
|
272
|
+
if (mutationKey !== void 0) {
|
|
273
|
+
if (record.mutationKey === void 0) return false;
|
|
274
|
+
if (exact) {
|
|
275
|
+
if ((record.mutationHash ?? serializeKey(record.mutationKey)) !== (precomputedExactHash ?? serializeKey(mutationKey))) return false;
|
|
276
|
+
} else if (!keyStartsWith(record.mutationKey, mutationKey)) return false;
|
|
277
|
+
}
|
|
278
|
+
if (status !== void 0 && record.state.status !== status) return false;
|
|
279
|
+
if (predicate && !predicate(record)) return false;
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
Object.defineProperty(exports, "clampPages", {
|
|
283
|
+
enumerable: true,
|
|
284
|
+
get: function() {
|
|
285
|
+
return clampPages;
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
Object.defineProperty(exports, "computeBackoffMs", {
|
|
289
|
+
enumerable: true,
|
|
290
|
+
get: function() {
|
|
291
|
+
return computeBackoffMs;
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
Object.defineProperty(exports, "infiniteQuerySpec", {
|
|
295
|
+
enumerable: true,
|
|
296
|
+
get: function() {
|
|
297
|
+
return infiniteQuerySpec;
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
Object.defineProperty(exports, "isEntryStale", {
|
|
301
|
+
enumerable: true,
|
|
302
|
+
get: function() {
|
|
303
|
+
return isEntryStale;
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
Object.defineProperty(exports, "isPlainObject", {
|
|
307
|
+
enumerable: true,
|
|
308
|
+
get: function() {
|
|
309
|
+
return isPlainObject;
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
Object.defineProperty(exports, "keyStartsWith", {
|
|
313
|
+
enumerable: true,
|
|
314
|
+
get: function() {
|
|
315
|
+
return keyStartsWith;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
Object.defineProperty(exports, "matchesMutationFilter", {
|
|
319
|
+
enumerable: true,
|
|
320
|
+
get: function() {
|
|
321
|
+
return matchesMutationFilter;
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
Object.defineProperty(exports, "matchesQueryFilter", {
|
|
325
|
+
enumerable: true,
|
|
326
|
+
get: function() {
|
|
327
|
+
return matchesQueryFilter;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
Object.defineProperty(exports, "mutationSpec", {
|
|
331
|
+
enumerable: true,
|
|
332
|
+
get: function() {
|
|
333
|
+
return mutationSpec;
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
Object.defineProperty(exports, "partialMatchValue", {
|
|
337
|
+
enumerable: true,
|
|
338
|
+
get: function() {
|
|
339
|
+
return partialMatchValue;
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
Object.defineProperty(exports, "querySpec", {
|
|
343
|
+
enumerable: true,
|
|
344
|
+
get: function() {
|
|
345
|
+
return querySpec;
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
Object.defineProperty(exports, "resolveNetworkAction", {
|
|
349
|
+
enumerable: true,
|
|
350
|
+
get: function() {
|
|
351
|
+
return resolveNetworkAction;
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
Object.defineProperty(exports, "resolveStaleMs", {
|
|
355
|
+
enumerable: true,
|
|
356
|
+
get: function() {
|
|
357
|
+
return resolveStaleMs;
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
Object.defineProperty(exports, "serializeKey", {
|
|
361
|
+
enumerable: true,
|
|
362
|
+
get: function() {
|
|
363
|
+
return serializeKey;
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
Object.defineProperty(exports, "shouldRetryAttempt", {
|
|
367
|
+
enumerable: true,
|
|
368
|
+
get: function() {
|
|
369
|
+
return shouldRetryAttempt;
|
|
370
|
+
}
|
|
371
|
+
});
|