@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,91 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
// src/live/persist.ts
|
|
3
|
+
/** Creates a `Persister` backed by `localStorage`, `sessionStorage`, or custom async storage. */
|
|
4
|
+
function createStoragePersister(options) {
|
|
5
|
+
const storageKey = options.key ?? "bevelo:query-cache";
|
|
6
|
+
const serialize = options.serialize ?? JSON.stringify;
|
|
7
|
+
const deserialize = options.deserialize ?? ((raw) => JSON.parse(raw));
|
|
8
|
+
return {
|
|
9
|
+
async persistClient(envelope) {
|
|
10
|
+
if (!options.storage) return;
|
|
11
|
+
await options.storage.setItem(storageKey, serialize(envelope));
|
|
12
|
+
},
|
|
13
|
+
async restoreClient() {
|
|
14
|
+
if (!options.storage) return void 0;
|
|
15
|
+
const raw = await options.storage.getItem(storageKey);
|
|
16
|
+
if (raw === null || raw === "") return void 0;
|
|
17
|
+
try {
|
|
18
|
+
return deserialize(raw);
|
|
19
|
+
} catch {
|
|
20
|
+
await options.storage.removeItem(storageKey);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
async removeClient() {
|
|
25
|
+
if (!options.storage) return;
|
|
26
|
+
await options.storage.removeItem(storageKey);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** Restores cached queries from `persister` and subscribes to persist future updates. */
|
|
31
|
+
function persistQueryClient(options) {
|
|
32
|
+
const { queryClient, persister, maxAge = 864e5, buster = "", throttleMs = 1e3, dehydrateOptions, hydrateOptions } = options;
|
|
33
|
+
const env = queryClient.getRuntimeEnv();
|
|
34
|
+
let disposed = false;
|
|
35
|
+
let pendingTimer;
|
|
36
|
+
let unsubQuery;
|
|
37
|
+
let unsubMutation;
|
|
38
|
+
const saveNow = () => {
|
|
39
|
+
if (disposed) return;
|
|
40
|
+
const envelope = {
|
|
41
|
+
timestamp: env.clock.now(),
|
|
42
|
+
buster,
|
|
43
|
+
clientState: queryClient.dehydrate(dehydrateOptions)
|
|
44
|
+
};
|
|
45
|
+
Promise.resolve(persister.persistClient(envelope)).catch(() => void 0);
|
|
46
|
+
};
|
|
47
|
+
const scheduleSave = () => {
|
|
48
|
+
if (disposed) return;
|
|
49
|
+
if (throttleMs <= 0) {
|
|
50
|
+
saveNow();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (pendingTimer !== void 0) return;
|
|
54
|
+
pendingTimer = env.clock.setTimeout(() => {
|
|
55
|
+
pendingTimer = void 0;
|
|
56
|
+
saveNow();
|
|
57
|
+
}, throttleMs);
|
|
58
|
+
};
|
|
59
|
+
const subscribe = () => {
|
|
60
|
+
if (disposed) return;
|
|
61
|
+
unsubQuery = queryClient.getQueryCache().subscribe(scheduleSave);
|
|
62
|
+
unsubMutation = queryClient.getMutationCache().subscribe(scheduleSave);
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
restorePromise: Promise.resolve(persister.restoreClient()).then(async (envelope) => {
|
|
66
|
+
if (disposed || envelope === void 0) return;
|
|
67
|
+
const expired = Number.isFinite(maxAge) && env.clock.now() - envelope.timestamp > maxAge;
|
|
68
|
+
const busted = envelope.buster !== buster;
|
|
69
|
+
if (expired || busted) {
|
|
70
|
+
await persister.removeClient();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
queryClient.hydrate(envelope.clientState, hydrateOptions);
|
|
74
|
+
}).finally(() => {
|
|
75
|
+
subscribe();
|
|
76
|
+
}),
|
|
77
|
+
unsubscribe() {
|
|
78
|
+
disposed = true;
|
|
79
|
+
if (pendingTimer !== void 0) {
|
|
80
|
+
pendingTimer.cancel();
|
|
81
|
+
pendingTimer = void 0;
|
|
82
|
+
}
|
|
83
|
+
unsubQuery?.();
|
|
84
|
+
unsubMutation?.();
|
|
85
|
+
unsubQuery = void 0;
|
|
86
|
+
unsubMutation = void 0;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
exports.createStoragePersister = createStoragePersister;
|
|
91
|
+
exports.persistQueryClient = persistQueryClient;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { d as PersistedCacheEnvelope, f as Persister, g as persistQueryClient, h as createStoragePersister, l as PersistClientHandle, m as StoragePersisterOptions, p as StorageLike, u as PersistClientOptions } from "../broadcast-G0JvCc7n.cjs";
|
|
2
|
+
export { PersistClientHandle, PersistClientOptions, PersistedCacheEnvelope, Persister, StorageLike, StoragePersisterOptions, createStoragePersister, persistQueryClient };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { d as PersistedCacheEnvelope, f as Persister, g as persistQueryClient, h as createStoragePersister, l as PersistClientHandle, m as StoragePersisterOptions, p as StorageLike, u as PersistClientOptions } from "../broadcast-DfOq-zMG.js";
|
|
2
|
+
export { PersistClientHandle, PersistClientOptions, PersistedCacheEnvelope, Persister, StorageLike, StoragePersisterOptions, createStoragePersister, persistQueryClient };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// src/live/persist.ts
|
|
2
|
+
/** Creates a `Persister` backed by `localStorage`, `sessionStorage`, or custom async storage. */
|
|
3
|
+
function createStoragePersister(options) {
|
|
4
|
+
const storageKey = options.key ?? "bevelo:query-cache";
|
|
5
|
+
const serialize = options.serialize ?? JSON.stringify;
|
|
6
|
+
const deserialize = options.deserialize ?? ((raw) => JSON.parse(raw));
|
|
7
|
+
return {
|
|
8
|
+
async persistClient(envelope) {
|
|
9
|
+
if (!options.storage) return;
|
|
10
|
+
await options.storage.setItem(storageKey, serialize(envelope));
|
|
11
|
+
},
|
|
12
|
+
async restoreClient() {
|
|
13
|
+
if (!options.storage) return void 0;
|
|
14
|
+
const raw = await options.storage.getItem(storageKey);
|
|
15
|
+
if (raw === null || raw === "") return void 0;
|
|
16
|
+
try {
|
|
17
|
+
return deserialize(raw);
|
|
18
|
+
} catch {
|
|
19
|
+
await options.storage.removeItem(storageKey);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
async removeClient() {
|
|
24
|
+
if (!options.storage) return;
|
|
25
|
+
await options.storage.removeItem(storageKey);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** Restores cached queries from `persister` and subscribes to persist future updates. */
|
|
30
|
+
function persistQueryClient(options) {
|
|
31
|
+
const { queryClient, persister, maxAge = 864e5, buster = "", throttleMs = 1e3, dehydrateOptions, hydrateOptions } = options;
|
|
32
|
+
const env = queryClient.getRuntimeEnv();
|
|
33
|
+
let disposed = false;
|
|
34
|
+
let pendingTimer;
|
|
35
|
+
let unsubQuery;
|
|
36
|
+
let unsubMutation;
|
|
37
|
+
const saveNow = () => {
|
|
38
|
+
if (disposed) return;
|
|
39
|
+
const envelope = {
|
|
40
|
+
timestamp: env.clock.now(),
|
|
41
|
+
buster,
|
|
42
|
+
clientState: queryClient.dehydrate(dehydrateOptions)
|
|
43
|
+
};
|
|
44
|
+
Promise.resolve(persister.persistClient(envelope)).catch(() => void 0);
|
|
45
|
+
};
|
|
46
|
+
const scheduleSave = () => {
|
|
47
|
+
if (disposed) return;
|
|
48
|
+
if (throttleMs <= 0) {
|
|
49
|
+
saveNow();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (pendingTimer !== void 0) return;
|
|
53
|
+
pendingTimer = env.clock.setTimeout(() => {
|
|
54
|
+
pendingTimer = void 0;
|
|
55
|
+
saveNow();
|
|
56
|
+
}, throttleMs);
|
|
57
|
+
};
|
|
58
|
+
const subscribe = () => {
|
|
59
|
+
if (disposed) return;
|
|
60
|
+
unsubQuery = queryClient.getQueryCache().subscribe(scheduleSave);
|
|
61
|
+
unsubMutation = queryClient.getMutationCache().subscribe(scheduleSave);
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
restorePromise: Promise.resolve(persister.restoreClient()).then(async (envelope) => {
|
|
65
|
+
if (disposed || envelope === void 0) return;
|
|
66
|
+
const expired = Number.isFinite(maxAge) && env.clock.now() - envelope.timestamp > maxAge;
|
|
67
|
+
const busted = envelope.buster !== buster;
|
|
68
|
+
if (expired || busted) {
|
|
69
|
+
await persister.removeClient();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
queryClient.hydrate(envelope.clientState, hydrateOptions);
|
|
73
|
+
}).finally(() => {
|
|
74
|
+
subscribe();
|
|
75
|
+
}),
|
|
76
|
+
unsubscribe() {
|
|
77
|
+
disposed = true;
|
|
78
|
+
if (pendingTimer !== void 0) {
|
|
79
|
+
pendingTimer.cancel();
|
|
80
|
+
pendingTimer = void 0;
|
|
81
|
+
}
|
|
82
|
+
unsubQuery?.();
|
|
83
|
+
unsubMutation?.();
|
|
84
|
+
unsubQuery = void 0;
|
|
85
|
+
unsubMutation = void 0;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export { createStoragePersister, persistQueryClient };
|
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_key = require("../key-DS_LgCI1.cjs");
|
|
3
|
+
// src/pure/types.ts
|
|
4
|
+
const DATA_TAG = Symbol("bevelo.query.data");
|
|
5
|
+
const ERROR_TAG = Symbol("bevelo.query.error");
|
|
6
|
+
/** Marker value passed instead of `queryFn` to pause a query cleanly. */
|
|
7
|
+
const skipToken = Symbol("bevelo.query.skip");
|
|
8
|
+
/** Placeholder helper that keeps the previous query data while keys change. */
|
|
9
|
+
function keepPrevious(previousData) {
|
|
10
|
+
return previousData;
|
|
11
|
+
}
|
|
12
|
+
// src/pure/structural.ts
|
|
13
|
+
/**
|
|
14
|
+
* Compares `prev` and `next` recursively for plain objects and arrays. Whenever every
|
|
15
|
+
* child keeps the same value or reference, `prev` is returned so downstream equality
|
|
16
|
+
* checks (`Object.is`) see zero change.
|
|
17
|
+
*/
|
|
18
|
+
function keepEqual(prev, next) {
|
|
19
|
+
if (Object.is(prev, next)) return next;
|
|
20
|
+
const prevArray = Array.isArray(prev);
|
|
21
|
+
const nextArray = Array.isArray(next);
|
|
22
|
+
if (prevArray && nextArray) {
|
|
23
|
+
const pArr = prev;
|
|
24
|
+
const nArr = next;
|
|
25
|
+
const len = nArr.length;
|
|
26
|
+
let copy = pArr.length === len ? void 0 : new Array(len);
|
|
27
|
+
for (let i = 0; i < len; i += 1) {
|
|
28
|
+
const child = keepEqual(i < pArr.length ? pArr[i] : void 0, nArr[i]);
|
|
29
|
+
if (copy !== void 0) copy[i] = child;
|
|
30
|
+
else if (!Object.is(child, pArr[i])) {
|
|
31
|
+
copy = new Array(len);
|
|
32
|
+
for (let j = 0; j < i; j += 1) copy[j] = pArr[j];
|
|
33
|
+
copy[i] = child;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return copy ?? pArr;
|
|
37
|
+
}
|
|
38
|
+
if (!prevArray && !nextArray && require_key.isPlainObject(prev) && require_key.isPlainObject(next)) {
|
|
39
|
+
const pObj = prev;
|
|
40
|
+
const nObj = next;
|
|
41
|
+
const pKeys = Object.keys(pObj);
|
|
42
|
+
const nKeys = Object.keys(nObj);
|
|
43
|
+
let result = pKeys.length === nKeys.length ? void 0 : {};
|
|
44
|
+
for (let i = 0; i < nKeys.length; i += 1) {
|
|
45
|
+
const k = nKeys[i];
|
|
46
|
+
const hasPrev = Object.prototype.hasOwnProperty.call(pObj, k);
|
|
47
|
+
const merged = hasPrev ? keepEqual(pObj[k], nObj[k]) : nObj[k];
|
|
48
|
+
if (result !== void 0) result[k] = merged;
|
|
49
|
+
else if (!hasPrev || !Object.is(merged, pObj[k])) {
|
|
50
|
+
result = {};
|
|
51
|
+
for (let j = 0; j < i; j += 1) {
|
|
52
|
+
const prevK = nKeys[j];
|
|
53
|
+
result[prevK] = pObj[prevK];
|
|
54
|
+
}
|
|
55
|
+
result[k] = merged;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return result ?? prev;
|
|
59
|
+
}
|
|
60
|
+
return next;
|
|
61
|
+
}
|
|
62
|
+
// src/pure/state.ts
|
|
63
|
+
/** Creates the initial state record for a cache entry. */
|
|
64
|
+
function initialQueryState(seed) {
|
|
65
|
+
const hasSeedData = seed !== void 0 && seed.data !== void 0;
|
|
66
|
+
return {
|
|
67
|
+
data: hasSeedData ? seed.data : void 0,
|
|
68
|
+
dataUpdatedAt: hasSeedData ? seed.dataUpdatedAt ?? 0 : 0,
|
|
69
|
+
dataUpdateCount: hasSeedData ? 1 : 0,
|
|
70
|
+
error: null,
|
|
71
|
+
errorUpdatedAt: 0,
|
|
72
|
+
errorUpdateCount: 0,
|
|
73
|
+
fetchFailureCount: 0,
|
|
74
|
+
fetchFailureReason: null,
|
|
75
|
+
fetchMeta: null,
|
|
76
|
+
invalidated: false,
|
|
77
|
+
status: hasSeedData ? "success" : seed?.status ?? "pending",
|
|
78
|
+
fetchPhase: "idle"
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/** Pure state transition function for a `QueryEntry`. */
|
|
82
|
+
function reduceQueryState(state, action) {
|
|
83
|
+
switch (action.type) {
|
|
84
|
+
case "fetch": return {
|
|
85
|
+
...state,
|
|
86
|
+
fetchPhase: "fetching",
|
|
87
|
+
fetchMeta: action.meta ?? null,
|
|
88
|
+
fetchFailureCount: 0,
|
|
89
|
+
fetchFailureReason: null
|
|
90
|
+
};
|
|
91
|
+
case "pause": return state.fetchPhase === "fetching" ? {
|
|
92
|
+
...state,
|
|
93
|
+
fetchPhase: "paused"
|
|
94
|
+
} : state;
|
|
95
|
+
case "continue": return state.fetchPhase === "paused" ? {
|
|
96
|
+
...state,
|
|
97
|
+
fetchPhase: "fetching"
|
|
98
|
+
} : state;
|
|
99
|
+
case "fail": return {
|
|
100
|
+
...state,
|
|
101
|
+
fetchFailureCount: state.fetchFailureCount + 1,
|
|
102
|
+
fetchFailureReason: action.error
|
|
103
|
+
};
|
|
104
|
+
case "success": {
|
|
105
|
+
const nextData = action.structuralSharing ? keepEqual(state.data, action.data) : action.data;
|
|
106
|
+
const keepPrevData = nextData === void 0;
|
|
107
|
+
return {
|
|
108
|
+
...state,
|
|
109
|
+
data: keepPrevData ? state.data : nextData,
|
|
110
|
+
dataUpdatedAt: action.updatedAt,
|
|
111
|
+
dataUpdateCount: state.dataUpdateCount + 1,
|
|
112
|
+
error: null,
|
|
113
|
+
errorUpdatedAt: state.errorUpdatedAt,
|
|
114
|
+
fetchFailureCount: 0,
|
|
115
|
+
fetchFailureReason: null,
|
|
116
|
+
invalidated: false,
|
|
117
|
+
status: "success",
|
|
118
|
+
fetchPhase: "idle"
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
case "error": return {
|
|
122
|
+
...state,
|
|
123
|
+
error: action.error,
|
|
124
|
+
errorUpdatedAt: action.updatedAt,
|
|
125
|
+
errorUpdateCount: state.errorUpdateCount + 1,
|
|
126
|
+
fetchFailureCount: state.fetchFailureCount + 1,
|
|
127
|
+
fetchFailureReason: action.error,
|
|
128
|
+
status: "error",
|
|
129
|
+
fetchPhase: "idle"
|
|
130
|
+
};
|
|
131
|
+
case "cancel": return action.revert ?? {
|
|
132
|
+
...state,
|
|
133
|
+
fetchPhase: "idle"
|
|
134
|
+
};
|
|
135
|
+
case "invalidate": return {
|
|
136
|
+
...state,
|
|
137
|
+
invalidated: true
|
|
138
|
+
};
|
|
139
|
+
case "setData": {
|
|
140
|
+
const nextData = action.structuralSharing ? keepEqual(state.data, action.data) : action.data;
|
|
141
|
+
return {
|
|
142
|
+
...state,
|
|
143
|
+
data: nextData,
|
|
144
|
+
dataUpdatedAt: action.updatedAt,
|
|
145
|
+
dataUpdateCount: state.dataUpdateCount + 1,
|
|
146
|
+
error: null,
|
|
147
|
+
invalidated: false,
|
|
148
|
+
status: "success"
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/** Builds a discriminated `QuerySnapshot` from raw entry state and selected data. */
|
|
154
|
+
function deriveQuerySnapshot(input) {
|
|
155
|
+
const { state, selectedData, isPlaceholder, isFetchedAfterMount, staleTime, now, refetch, promise } = input;
|
|
156
|
+
const isFetching = state.fetchPhase === "fetching";
|
|
157
|
+
const isPaused = state.fetchPhase === "paused";
|
|
158
|
+
const isStale = isPlaceholder || require_key.isEntryStale(state, staleTime, now);
|
|
159
|
+
const isFetched = state.dataUpdateCount > 0 || state.errorUpdateCount > 0;
|
|
160
|
+
const hasError = state.status === "error" && state.error !== null;
|
|
161
|
+
const effectiveData = selectedData;
|
|
162
|
+
const hasData = effectiveData !== void 0;
|
|
163
|
+
if (isPlaceholder && hasData) return {
|
|
164
|
+
status: "success",
|
|
165
|
+
data: effectiveData,
|
|
166
|
+
error: null,
|
|
167
|
+
dataUpdatedAt: state.dataUpdatedAt,
|
|
168
|
+
errorUpdatedAt: state.errorUpdatedAt,
|
|
169
|
+
failureCount: state.fetchFailureCount,
|
|
170
|
+
failureReason: state.fetchFailureReason,
|
|
171
|
+
errorUpdateCount: state.errorUpdateCount,
|
|
172
|
+
fetchPhase: state.fetchPhase,
|
|
173
|
+
isPending: false,
|
|
174
|
+
isLoading: false,
|
|
175
|
+
isInitialLoading: false,
|
|
176
|
+
isError: false,
|
|
177
|
+
isLoadingError: false,
|
|
178
|
+
isRefetchError: false,
|
|
179
|
+
isSuccess: true,
|
|
180
|
+
isFetching,
|
|
181
|
+
isPaused,
|
|
182
|
+
isPlaceholderData: true,
|
|
183
|
+
isRefetching: isFetching,
|
|
184
|
+
isStale,
|
|
185
|
+
isFetched,
|
|
186
|
+
isFetchedAfterMount,
|
|
187
|
+
refetch,
|
|
188
|
+
promise
|
|
189
|
+
};
|
|
190
|
+
if (hasError && !hasData) return {
|
|
191
|
+
status: "error",
|
|
192
|
+
data: void 0,
|
|
193
|
+
error: state.error,
|
|
194
|
+
dataUpdatedAt: state.dataUpdatedAt,
|
|
195
|
+
errorUpdatedAt: state.errorUpdatedAt,
|
|
196
|
+
failureCount: state.fetchFailureCount,
|
|
197
|
+
failureReason: state.fetchFailureReason,
|
|
198
|
+
errorUpdateCount: state.errorUpdateCount,
|
|
199
|
+
fetchPhase: state.fetchPhase,
|
|
200
|
+
isPending: false,
|
|
201
|
+
isLoading: false,
|
|
202
|
+
isInitialLoading: false,
|
|
203
|
+
isError: true,
|
|
204
|
+
isLoadingError: true,
|
|
205
|
+
isRefetchError: false,
|
|
206
|
+
isSuccess: false,
|
|
207
|
+
isFetching,
|
|
208
|
+
isPaused,
|
|
209
|
+
isPlaceholderData: false,
|
|
210
|
+
isRefetching: isFetching,
|
|
211
|
+
isStale,
|
|
212
|
+
isFetched,
|
|
213
|
+
isFetchedAfterMount,
|
|
214
|
+
refetch,
|
|
215
|
+
promise
|
|
216
|
+
};
|
|
217
|
+
if (hasError && hasData) return {
|
|
218
|
+
status: "error",
|
|
219
|
+
data: effectiveData,
|
|
220
|
+
error: state.error,
|
|
221
|
+
dataUpdatedAt: state.dataUpdatedAt,
|
|
222
|
+
errorUpdatedAt: state.errorUpdatedAt,
|
|
223
|
+
failureCount: state.fetchFailureCount,
|
|
224
|
+
failureReason: state.fetchFailureReason,
|
|
225
|
+
errorUpdateCount: state.errorUpdateCount,
|
|
226
|
+
fetchPhase: state.fetchPhase,
|
|
227
|
+
isPending: false,
|
|
228
|
+
isLoading: false,
|
|
229
|
+
isInitialLoading: false,
|
|
230
|
+
isError: true,
|
|
231
|
+
isLoadingError: false,
|
|
232
|
+
isRefetchError: true,
|
|
233
|
+
isSuccess: false,
|
|
234
|
+
isFetching,
|
|
235
|
+
isPaused,
|
|
236
|
+
isPlaceholderData: false,
|
|
237
|
+
isRefetching: isFetching,
|
|
238
|
+
isStale,
|
|
239
|
+
isFetched,
|
|
240
|
+
isFetchedAfterMount,
|
|
241
|
+
refetch,
|
|
242
|
+
promise
|
|
243
|
+
};
|
|
244
|
+
if (state.status === "success" && hasData) return {
|
|
245
|
+
status: "success",
|
|
246
|
+
data: effectiveData,
|
|
247
|
+
error: null,
|
|
248
|
+
dataUpdatedAt: state.dataUpdatedAt,
|
|
249
|
+
errorUpdatedAt: state.errorUpdatedAt,
|
|
250
|
+
failureCount: state.fetchFailureCount,
|
|
251
|
+
failureReason: state.fetchFailureReason,
|
|
252
|
+
errorUpdateCount: state.errorUpdateCount,
|
|
253
|
+
fetchPhase: state.fetchPhase,
|
|
254
|
+
isPending: false,
|
|
255
|
+
isLoading: false,
|
|
256
|
+
isInitialLoading: false,
|
|
257
|
+
isError: false,
|
|
258
|
+
isLoadingError: false,
|
|
259
|
+
isRefetchError: false,
|
|
260
|
+
isSuccess: true,
|
|
261
|
+
isFetching,
|
|
262
|
+
isPaused,
|
|
263
|
+
isPlaceholderData: false,
|
|
264
|
+
isRefetching: isFetching,
|
|
265
|
+
isStale,
|
|
266
|
+
isFetched,
|
|
267
|
+
isFetchedAfterMount,
|
|
268
|
+
refetch,
|
|
269
|
+
promise
|
|
270
|
+
};
|
|
271
|
+
const isLoading = isFetching;
|
|
272
|
+
return {
|
|
273
|
+
status: "pending",
|
|
274
|
+
data: void 0,
|
|
275
|
+
error: null,
|
|
276
|
+
dataUpdatedAt: state.dataUpdatedAt,
|
|
277
|
+
errorUpdatedAt: state.errorUpdatedAt,
|
|
278
|
+
failureCount: state.fetchFailureCount,
|
|
279
|
+
failureReason: state.fetchFailureReason,
|
|
280
|
+
errorUpdateCount: state.errorUpdateCount,
|
|
281
|
+
fetchPhase: state.fetchPhase,
|
|
282
|
+
isPending: true,
|
|
283
|
+
isLoading,
|
|
284
|
+
isInitialLoading: isLoading,
|
|
285
|
+
isError: false,
|
|
286
|
+
isLoadingError: false,
|
|
287
|
+
isRefetchError: false,
|
|
288
|
+
isSuccess: false,
|
|
289
|
+
isFetching,
|
|
290
|
+
isPaused,
|
|
291
|
+
isPlaceholderData: false,
|
|
292
|
+
isRefetching: false,
|
|
293
|
+
isStale,
|
|
294
|
+
isFetched,
|
|
295
|
+
isFetchedAfterMount,
|
|
296
|
+
refetch,
|
|
297
|
+
promise
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
/** Derives an `InfiniteSnapshot` from a base snapshot plus pagination flags. */
|
|
301
|
+
function deriveInfiniteSnapshot(base, pagination) {
|
|
302
|
+
return {
|
|
303
|
+
...base,
|
|
304
|
+
...pagination
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
/** Creates initial state for a `Mutation`. */
|
|
308
|
+
function initialMutationState() {
|
|
309
|
+
return {
|
|
310
|
+
context: void 0,
|
|
311
|
+
data: void 0,
|
|
312
|
+
error: null,
|
|
313
|
+
failureCount: 0,
|
|
314
|
+
failureReason: null,
|
|
315
|
+
isPaused: false,
|
|
316
|
+
status: "idle",
|
|
317
|
+
variables: void 0,
|
|
318
|
+
submittedAt: 0
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
/** Pure state transition function for a `MutationEntry`. */
|
|
322
|
+
function reduceMutationState(state, action) {
|
|
323
|
+
switch (action.type) {
|
|
324
|
+
case "pending": return {
|
|
325
|
+
...state,
|
|
326
|
+
context: action.context,
|
|
327
|
+
data: void 0,
|
|
328
|
+
error: null,
|
|
329
|
+
failureCount: 0,
|
|
330
|
+
failureReason: null,
|
|
331
|
+
isPaused: false,
|
|
332
|
+
status: "pending",
|
|
333
|
+
variables: action.variables,
|
|
334
|
+
submittedAt: action.submittedAt
|
|
335
|
+
};
|
|
336
|
+
case "context": return {
|
|
337
|
+
...state,
|
|
338
|
+
context: action.context
|
|
339
|
+
};
|
|
340
|
+
case "fail": return state.status === "pending" ? {
|
|
341
|
+
...state,
|
|
342
|
+
failureCount: state.failureCount + 1,
|
|
343
|
+
failureReason: action.error
|
|
344
|
+
} : state;
|
|
345
|
+
case "pause": return state.status === "pending" ? {
|
|
346
|
+
...state,
|
|
347
|
+
isPaused: true
|
|
348
|
+
} : state;
|
|
349
|
+
case "continue": return state.status === "pending" ? {
|
|
350
|
+
...state,
|
|
351
|
+
isPaused: false
|
|
352
|
+
} : state;
|
|
353
|
+
case "success": return {
|
|
354
|
+
...state,
|
|
355
|
+
data: action.data,
|
|
356
|
+
error: null,
|
|
357
|
+
failureCount: 0,
|
|
358
|
+
failureReason: null,
|
|
359
|
+
isPaused: false,
|
|
360
|
+
status: "success"
|
|
361
|
+
};
|
|
362
|
+
case "error": return {
|
|
363
|
+
...state,
|
|
364
|
+
data: void 0,
|
|
365
|
+
error: action.error,
|
|
366
|
+
failureCount: state.failureCount + 1,
|
|
367
|
+
failureReason: action.error,
|
|
368
|
+
isPaused: false,
|
|
369
|
+
status: "error"
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
/** Derives a public discriminated `MutationSnapshot`. */
|
|
374
|
+
function deriveMutationSnapshot(state, commands) {
|
|
375
|
+
const shared = {
|
|
376
|
+
variables: state.variables,
|
|
377
|
+
context: state.context,
|
|
378
|
+
failureCount: state.failureCount,
|
|
379
|
+
failureReason: state.failureReason,
|
|
380
|
+
isPaused: state.isPaused,
|
|
381
|
+
submittedAt: state.submittedAt,
|
|
382
|
+
...commands
|
|
383
|
+
};
|
|
384
|
+
if (state.status === "pending") return {
|
|
385
|
+
...shared,
|
|
386
|
+
status: "pending",
|
|
387
|
+
data: void 0,
|
|
388
|
+
error: null,
|
|
389
|
+
isIdle: false,
|
|
390
|
+
isPending: true,
|
|
391
|
+
isError: false,
|
|
392
|
+
isSuccess: false,
|
|
393
|
+
variables: state.variables
|
|
394
|
+
};
|
|
395
|
+
if (state.status === "error") return {
|
|
396
|
+
...shared,
|
|
397
|
+
status: "error",
|
|
398
|
+
data: void 0,
|
|
399
|
+
error: state.error,
|
|
400
|
+
isIdle: false,
|
|
401
|
+
isPending: false,
|
|
402
|
+
isError: true,
|
|
403
|
+
isSuccess: false,
|
|
404
|
+
variables: state.variables
|
|
405
|
+
};
|
|
406
|
+
if (state.status === "success") return {
|
|
407
|
+
...shared,
|
|
408
|
+
status: "success",
|
|
409
|
+
data: state.data,
|
|
410
|
+
error: null,
|
|
411
|
+
isIdle: false,
|
|
412
|
+
isPending: false,
|
|
413
|
+
isError: false,
|
|
414
|
+
isSuccess: true,
|
|
415
|
+
variables: state.variables
|
|
416
|
+
};
|
|
417
|
+
return {
|
|
418
|
+
...shared,
|
|
419
|
+
status: "idle",
|
|
420
|
+
data: void 0,
|
|
421
|
+
error: null,
|
|
422
|
+
isIdle: true,
|
|
423
|
+
isPending: false,
|
|
424
|
+
isError: false,
|
|
425
|
+
isSuccess: false
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
// src/pure/hydration.ts
|
|
429
|
+
function defaultShouldDehydrateQuery(record) {
|
|
430
|
+
return record.state.status === "success";
|
|
431
|
+
}
|
|
432
|
+
function defaultShouldDehydrateMutation(record) {
|
|
433
|
+
return record.state.isPaused;
|
|
434
|
+
}
|
|
435
|
+
/** Converts live cache records into a serializable `DehydratedCache` snapshot. */
|
|
436
|
+
function packCache(queries, mutations, options = {}) {
|
|
437
|
+
const shouldQuery = options.shouldDehydrateQuery ?? defaultShouldDehydrateQuery;
|
|
438
|
+
const shouldMutation = options.shouldDehydrateMutation ?? defaultShouldDehydrateMutation;
|
|
439
|
+
const serializeData = options.serializeData;
|
|
440
|
+
const outQueries = [];
|
|
441
|
+
for (const q of queries) {
|
|
442
|
+
if (!shouldQuery(q)) continue;
|
|
443
|
+
const rawData = q.state.data;
|
|
444
|
+
const data = serializeData && rawData !== void 0 ? serializeData(rawData) : rawData;
|
|
445
|
+
const isPendingWithPromise = q.state.status === "pending" && q.promise !== void 0;
|
|
446
|
+
outQueries.push({
|
|
447
|
+
queryHash: q.queryHash,
|
|
448
|
+
queryKey: q.queryKey,
|
|
449
|
+
state: {
|
|
450
|
+
...q.state,
|
|
451
|
+
data,
|
|
452
|
+
fetchPhase: isPendingWithPromise ? "fetching" : "idle"
|
|
453
|
+
},
|
|
454
|
+
...q.meta !== void 0 ? { meta: q.meta } : {},
|
|
455
|
+
...isPendingWithPromise ? { promise: q.promise } : {}
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
const outMutations = [];
|
|
459
|
+
for (const m of mutations) {
|
|
460
|
+
if (!shouldMutation({
|
|
461
|
+
mutationKey: m.mutationKey,
|
|
462
|
+
state: m.state
|
|
463
|
+
})) continue;
|
|
464
|
+
outMutations.push({
|
|
465
|
+
state: m.state,
|
|
466
|
+
...m.mutationKey !== void 0 ? { mutationKey: m.mutationKey } : {},
|
|
467
|
+
...m.meta !== void 0 ? { meta: m.meta } : {},
|
|
468
|
+
...m.scope !== void 0 ? { scope: m.scope } : {}
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
return {
|
|
472
|
+
queries: outQueries,
|
|
473
|
+
mutations: outMutations
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
/** Filters dehydrated queries to those fresher than existing cache entries and deserializes data. */
|
|
477
|
+
function selectHydrateQueries(cache, existingUpdatedAt, options = {}) {
|
|
478
|
+
const deserialize = options.deserializeData;
|
|
479
|
+
const accepted = [];
|
|
480
|
+
for (const entry of cache.queries) {
|
|
481
|
+
const existingTs = existingUpdatedAt(entry.queryHash);
|
|
482
|
+
if (existingTs !== void 0 && existingTs >= entry.state.dataUpdatedAt) continue;
|
|
483
|
+
const rawData = entry.state.data;
|
|
484
|
+
const data = deserialize && rawData !== void 0 ? deserialize(rawData) : rawData;
|
|
485
|
+
accepted.push({
|
|
486
|
+
...entry,
|
|
487
|
+
state: {
|
|
488
|
+
...entry.state,
|
|
489
|
+
data
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
return accepted;
|
|
494
|
+
}
|
|
495
|
+
exports.DATA_TAG = DATA_TAG;
|
|
496
|
+
exports.ERROR_TAG = ERROR_TAG;
|
|
497
|
+
exports.clampPages = require_key.clampPages;
|
|
498
|
+
exports.computeBackoffMs = require_key.computeBackoffMs;
|
|
499
|
+
exports.defaultShouldDehydrateMutation = defaultShouldDehydrateMutation;
|
|
500
|
+
exports.defaultShouldDehydrateQuery = defaultShouldDehydrateQuery;
|
|
501
|
+
exports.deriveInfiniteSnapshot = deriveInfiniteSnapshot;
|
|
502
|
+
exports.deriveMutationSnapshot = deriveMutationSnapshot;
|
|
503
|
+
exports.deriveQuerySnapshot = deriveQuerySnapshot;
|
|
504
|
+
exports.infiniteQuerySpec = require_key.infiniteQuerySpec;
|
|
505
|
+
exports.initialMutationState = initialMutationState;
|
|
506
|
+
exports.initialQueryState = initialQueryState;
|
|
507
|
+
exports.isEntryStale = require_key.isEntryStale;
|
|
508
|
+
exports.isPlainObject = require_key.isPlainObject;
|
|
509
|
+
exports.keepEqual = keepEqual;
|
|
510
|
+
exports.keepPrevious = keepPrevious;
|
|
511
|
+
exports.keyStartsWith = require_key.keyStartsWith;
|
|
512
|
+
exports.matchesMutationFilter = require_key.matchesMutationFilter;
|
|
513
|
+
exports.matchesQueryFilter = require_key.matchesQueryFilter;
|
|
514
|
+
exports.mutationSpec = require_key.mutationSpec;
|
|
515
|
+
exports.packCache = packCache;
|
|
516
|
+
exports.partialMatchValue = require_key.partialMatchValue;
|
|
517
|
+
exports.querySpec = require_key.querySpec;
|
|
518
|
+
exports.reduceMutationState = reduceMutationState;
|
|
519
|
+
exports.reduceQueryState = reduceQueryState;
|
|
520
|
+
exports.resolveNetworkAction = require_key.resolveNetworkAction;
|
|
521
|
+
exports.resolveStaleMs = require_key.resolveStaleMs;
|
|
522
|
+
exports.selectHydrateQueries = selectHydrateQueries;
|
|
523
|
+
exports.serializeKey = require_key.serializeKey;
|
|
524
|
+
exports.shouldRetryAttempt = require_key.shouldRetryAttempt;
|
|
525
|
+
exports.skipToken = skipToken;
|