@nano_kit/query 1.0.0-alpha.1 → 1.0.0-alpha.11
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/dist/CacheStorage.d.ts +1 -1
- package/dist/CacheStorage.d.ts.map +1 -1
- package/dist/CacheStorage.types.d.ts +5 -0
- package/dist/CacheStorage.types.d.ts.map +1 -1
- package/dist/ClientContext.d.ts +7 -5
- package/dist/ClientContext.d.ts.map +1 -1
- package/dist/RequestContext.d.ts.map +1 -1
- package/dist/cache.d.ts +8 -2
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.types.d.ts +1 -0
- package/dist/cache.types.d.ts.map +1 -1
- package/dist/client.d.ts +7 -6
- package/dist/client.d.ts.map +1 -1
- package/dist/client.mock.d.ts.map +1 -1
- package/dist/client.types.d.ts.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +924 -840
- package/dist/index.js.map +1 -1
- package/dist/map.d.ts.map +1 -1
- package/dist/queries/base.d.ts +9 -9
- package/dist/queries/base.d.ts.map +1 -1
- package/dist/queries/index.d.ts.map +1 -1
- package/dist/queries/infinite.d.ts.map +1 -1
- package/dist/queries/mutation.d.ts.map +1 -1
- package/dist/queries/operation.d.ts.map +1 -1
- package/dist/queries/query.d.ts.map +1 -1
- package/dist/settings/abortable.d.ts.map +1 -1
- package/dist/settings/codec.d.ts +16 -0
- package/dist/settings/codec.d.ts.map +1 -0
- package/dist/settings/entities.d.ts.map +1 -1
- package/dist/settings/hydratable.d.ts +11 -0
- package/dist/settings/hydratable.d.ts.map +1 -0
- package/dist/settings/index.d.ts +5 -3
- package/dist/settings/index.d.ts.map +1 -1
- package/dist/settings/indexedDbStorage.d.ts +4 -6
- package/dist/settings/indexedDbStorage.d.ts.map +1 -1
- package/dist/settings/persistence.d.ts +16 -0
- package/dist/settings/persistence.d.ts.map +1 -0
- package/dist/settings/retryOnError.d.ts.map +1 -1
- package/dist/settings/revalidateOn.d.ts +9 -0
- package/dist/settings/revalidateOn.d.ts.map +1 -0
- package/dist/settings/ssr.d.ts +9 -0
- package/dist/settings/ssr.d.ts.map +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/settings/revalidateOnFocus.d.ts +0 -12
- package/dist/settings/revalidateOnFocus.d.ts.map +0 -1
- package/dist/settings/revalidateOnInterval.d.ts +0 -9
- package/dist/settings/revalidateOnInterval.d.ts.map +0 -1
- package/dist/settings/revalidateOnReconnect.d.ts +0 -12
- package/dist/settings/revalidateOnReconnect.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,951 +1,1035 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { $$insert, $get, $getMapKey, Hydratables$, Hydrator$, NoopCodec, TasksRunner$, action, batch, clearMap, computed, deleteMapKey, effect, effectScope, fireMapEvent, identity, inject, isFunction, listen, mountable, onStart, readonly, setMapKey, signal, subMapEvent, taskPromise, untracked } from "@nano_kit/store";
|
|
2
|
+
//#region src/settings/abortable.ts
|
|
3
3
|
const abortControllers = /* @__PURE__ */ new WeakMap();
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Returns the AbortSignal for the given RequestContext
|
|
6
|
+
* @param ctx - RequestContext
|
|
7
|
+
* @returns AbortSignal
|
|
8
|
+
*/
|
|
9
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
5
10
|
function abortSignal(ctx) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
return controller.signal;
|
|
11
|
+
let controller = abortControllers.get(ctx);
|
|
12
|
+
if (!controller) abortControllers.set(ctx, controller = new AbortController());
|
|
13
|
+
return controller.signal;
|
|
11
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Aborts request associated with the given Promise or RequestContext
|
|
17
|
+
* @param promiseOrCtx - Promise or RequestContext
|
|
18
|
+
*/
|
|
12
19
|
function abort(promiseOrCtx) {
|
|
13
|
-
|
|
20
|
+
abortControllers.get(promiseOrCtx)?.abort();
|
|
14
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Aborts previous request associated with the given RequestContext
|
|
24
|
+
* @param ctx - RequestContext
|
|
25
|
+
*/
|
|
15
26
|
function abortPrevious(ctx) {
|
|
16
|
-
|
|
27
|
+
abort(ctx.prevCtx);
|
|
17
28
|
}
|
|
18
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Make requests abortable.
|
|
31
|
+
* @returns The client setting function.
|
|
32
|
+
*/
|
|
33
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
19
34
|
function abortable() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
return promise;
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
ctx.abortable = true;
|
|
40
|
-
};
|
|
35
|
+
return (ctx) => {
|
|
36
|
+
if (ctx.abortable === void 0) {
|
|
37
|
+
const superRun = ctx.run;
|
|
38
|
+
ctx.run = function(requestCtx, start, onSettled, interrupt) {
|
|
39
|
+
let abortController;
|
|
40
|
+
const promise = superRun.call(this, requestCtx, start, onSettled, (error) => interrupt?.(error) || abortController?.signal.aborted === true);
|
|
41
|
+
if (abortController = abortControllers.get(requestCtx)) {
|
|
42
|
+
abortControllers.set(promise, abortController);
|
|
43
|
+
promise.finally(() => abortControllers.delete(promise));
|
|
44
|
+
}
|
|
45
|
+
return promise;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
ctx.abortable = true;
|
|
49
|
+
};
|
|
41
50
|
}
|
|
42
|
-
|
|
43
|
-
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/cache.ts
|
|
53
|
+
const keysSet = /* @__PURE__ */ new Set();
|
|
54
|
+
/**
|
|
55
|
+
* Create a query cache key builder.
|
|
56
|
+
* @param name - The cache shard name.
|
|
57
|
+
* @param filter - Optional filter to process parameters before building the key.
|
|
58
|
+
* @returns The cache key builder.
|
|
59
|
+
*/
|
|
60
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
44
61
|
function queryKey(name, filter = (params) => params) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
62
|
+
const key = ((...params) => ({
|
|
63
|
+
shard: name,
|
|
64
|
+
key: JSON.stringify(filter(params))
|
|
65
|
+
}));
|
|
66
|
+
key.shard = name;
|
|
67
|
+
key.key = void 0;
|
|
68
|
+
keysSet.add(key);
|
|
69
|
+
return key;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Iterate over all cache key builders and call the provided callback with each key.
|
|
73
|
+
* Iteration is batched to avoid unnecessary reactivity triggers.
|
|
74
|
+
* @param callback - The callback function to be called with each cache key builder.
|
|
75
|
+
*/
|
|
76
|
+
function keys(callback) {
|
|
77
|
+
batch(() => keysSet.forEach(callback));
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Create an operation cache key builder with extra parameters.
|
|
81
|
+
* @param name - The cache shard name.
|
|
82
|
+
* @param filter - Optional filter to process parameters before building the key.
|
|
83
|
+
* @returns The extras cache key builder.
|
|
84
|
+
*/
|
|
85
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
54
86
|
function operationKey(name, filter) {
|
|
55
|
-
|
|
87
|
+
return /* @__PURE__ */ queryKey(name, filter);
|
|
56
88
|
}
|
|
57
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Create cache getter/setter for data.
|
|
91
|
+
* @param cache - The cache map.
|
|
92
|
+
* @returns The data getter/setter.
|
|
93
|
+
*/
|
|
94
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
58
95
|
function dataCacheFacade(cache) {
|
|
59
|
-
|
|
96
|
+
return dataCacheGetterSetter.bind(cache);
|
|
60
97
|
}
|
|
61
98
|
function dataCacheGetterSetter(key, ...value) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
99
|
+
if (value.length) {
|
|
100
|
+
const newValue = value[0];
|
|
101
|
+
this.set(key, (entry = this.initial()) => ({
|
|
102
|
+
...entry,
|
|
103
|
+
data: isFunction(newValue) ? newValue(entry.data) : newValue
|
|
104
|
+
}));
|
|
105
|
+
} else return this.$get(key).data;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Create cache getter for loading state.
|
|
109
|
+
* @param cache - The cache map.
|
|
110
|
+
* @returns The loading state getter.
|
|
111
|
+
*/
|
|
112
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
73
113
|
function loadingCacheFacade(cache) {
|
|
74
|
-
|
|
114
|
+
return (key) => cache.$get(key).loading;
|
|
75
115
|
}
|
|
76
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Create cache getter for error state.
|
|
118
|
+
* @param cache - The cache map.
|
|
119
|
+
* @returns The error state getter.
|
|
120
|
+
*/
|
|
121
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
77
122
|
function errorCacheFacade(cache) {
|
|
78
|
-
|
|
123
|
+
return (key) => cache.$get(key).error;
|
|
79
124
|
}
|
|
80
|
-
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/settings/entities.ts
|
|
81
127
|
const ENTITY_KEY = "#entity";
|
|
82
|
-
const EntityKey = queryKey(ENTITY_KEY);
|
|
128
|
+
const EntityKey = /* @__PURE__ */ queryKey(ENTITY_KEY);
|
|
83
129
|
function isIdentifier(value) {
|
|
84
|
-
|
|
85
|
-
|
|
130
|
+
const type = typeof value;
|
|
131
|
+
return type === "number" || type === "string";
|
|
86
132
|
}
|
|
87
133
|
function isEntityRef(value) {
|
|
88
|
-
|
|
134
|
+
return ENTITY_KEY in value;
|
|
89
135
|
}
|
|
90
136
|
let currentCtx = null;
|
|
91
|
-
|
|
92
|
-
function entity(name, id = (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
...idOrRefOrEntity,
|
|
110
|
-
[ENTITY_KEY]: key
|
|
111
|
-
};
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
// @__NO_SIDE_EFFECTS__
|
|
137
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
138
|
+
function entity(name, id = (entity) => entity.id) {
|
|
139
|
+
return (idOrRefOrEntity) => {
|
|
140
|
+
if (isIdentifier(idOrRefOrEntity)) return EntityKey(name, idOrRefOrEntity);
|
|
141
|
+
if (!idOrRefOrEntity || !currentCtx) return idOrRefOrEntity;
|
|
142
|
+
if (isEntityRef(idOrRefOrEntity)) return currentCtx.$get(idOrRefOrEntity[ENTITY_KEY]).data;
|
|
143
|
+
const key = EntityKey(name, id(idOrRefOrEntity));
|
|
144
|
+
currentCtx.set(key, {
|
|
145
|
+
...currentCtx.initial(),
|
|
146
|
+
data: idOrRefOrEntity
|
|
147
|
+
});
|
|
148
|
+
return {
|
|
149
|
+
...idOrRefOrEntity,
|
|
150
|
+
[ENTITY_KEY]: key
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
115
155
|
function entities(mapper) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
ctx.mapComputedData = ctx.mapData = safeMapper;
|
|
129
|
-
};
|
|
156
|
+
return (ctx) => {
|
|
157
|
+
const safeMapper = (data) => {
|
|
158
|
+
if (data) try {
|
|
159
|
+
currentCtx = ctx;
|
|
160
|
+
return mapper(data);
|
|
161
|
+
} finally {
|
|
162
|
+
currentCtx = null;
|
|
163
|
+
}
|
|
164
|
+
return data;
|
|
165
|
+
};
|
|
166
|
+
ctx.mapComputedData = ctx.mapData = safeMapper;
|
|
167
|
+
};
|
|
130
168
|
}
|
|
131
|
-
|
|
132
|
-
|
|
169
|
+
//#endregion
|
|
170
|
+
//#region src/map.ts
|
|
171
|
+
/**
|
|
172
|
+
* Check if sharded map has the key.
|
|
173
|
+
* Checks full key.
|
|
174
|
+
* @param map - The sharded map.
|
|
175
|
+
* @param shardedKey - The sharded key.
|
|
176
|
+
* @returns True if the sharded map has the key, false otherwise.
|
|
177
|
+
*/
|
|
178
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
133
179
|
function hasShardedMapKey(map, shardedKey) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
180
|
+
const { shard, key } = shardedKey;
|
|
181
|
+
if (key === void 0) return map.has(shard);
|
|
182
|
+
return map.get(shard)?.has(key) || false;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Get value from sharded map by key.
|
|
186
|
+
* @param map - The sharded map.
|
|
187
|
+
* @param shardedKey - The sharded key.
|
|
188
|
+
* @returns The value or undefined if not found.
|
|
189
|
+
*/
|
|
144
190
|
function $getShardedMapKey(map, shardedKey) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
return void 0;
|
|
153
|
-
}
|
|
154
|
-
return $getMapKey(shardMap, key);
|
|
191
|
+
const { shard, key } = shardedKey;
|
|
192
|
+
let shardMap;
|
|
193
|
+
if ((shardMap = map.get(shard)) === void 0) {
|
|
194
|
+
subMapEvent(map, $$insert);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
return $getMapKey(shardMap, key);
|
|
155
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* Set value in sharded map by key.
|
|
201
|
+
* If sharded key contains only shard name, sets value for all entries in the shard.
|
|
202
|
+
* @param map - The sharded map.
|
|
203
|
+
* @param shardedKey - The sharded key.
|
|
204
|
+
* @param value - The value to set.
|
|
205
|
+
*/
|
|
156
206
|
function setShardedMapKey(map, shardedKey, value) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
if (!shardExists) {
|
|
174
|
-
map.set(
|
|
175
|
-
shard,
|
|
176
|
-
shardMap = /* @__PURE__ */ new Map()
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
setMapKey(shardMap, key, value);
|
|
180
|
-
if (!shardExists) {
|
|
181
|
-
fireMapEvent(map, $$insert);
|
|
182
|
-
}
|
|
207
|
+
const { shard, key } = shardedKey;
|
|
208
|
+
let shardMap = map.get(shard);
|
|
209
|
+
const shardExists = shardMap !== void 0;
|
|
210
|
+
if (key === void 0) {
|
|
211
|
+
if (shardExists) batch(() => {
|
|
212
|
+
for (const params of shardMap.keys()) setMapKey(shardMap, params, value);
|
|
213
|
+
});
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (!shardExists) map.set(shard, shardMap = /* @__PURE__ */ new Map());
|
|
217
|
+
setMapKey(shardMap, key, value);
|
|
218
|
+
if (!shardExists) fireMapEvent(map, $$insert);
|
|
183
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Delete sharded map key.
|
|
222
|
+
* If sharded key contains only shard name, deletes all entries in the shard.
|
|
223
|
+
* @param map - The sharded map.
|
|
224
|
+
* @param shardedKey - The sharded key.
|
|
225
|
+
*/
|
|
184
226
|
function deleteShardedMapKey(map, shardedKey) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (key === void 0) {
|
|
194
|
-
clearMap(shardMap);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
deleteMapKey(shardMap, key);
|
|
227
|
+
const { shard, key } = shardedKey;
|
|
228
|
+
const shardMap = map.get(shard);
|
|
229
|
+
if (shardMap === void 0) return;
|
|
230
|
+
if (key === void 0) {
|
|
231
|
+
clearMap(shardMap);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
deleteMapKey(shardMap, key);
|
|
198
235
|
}
|
|
199
|
-
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region src/CacheStorage.ts
|
|
200
238
|
const DEFAULT_DEDUPE_TIME = 4e3;
|
|
201
239
|
const DEFAULT_CACHE_TIME = Infinity;
|
|
202
240
|
const UNSET_REV = Infinity;
|
|
203
|
-
|
|
241
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
204
242
|
function revLock(rev) {
|
|
205
|
-
|
|
243
|
+
return rev > 0 ? rev * -1 : rev;
|
|
206
244
|
}
|
|
207
|
-
|
|
245
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
208
246
|
function revLocked(rev) {
|
|
209
|
-
|
|
247
|
+
return rev < 0;
|
|
210
248
|
}
|
|
211
249
|
let revCounter = 0;
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
250
|
+
var CacheStorage = class {
|
|
251
|
+
dedupeTime = DEFAULT_DEDUPE_TIME;
|
|
252
|
+
cacheTime = DEFAULT_CACHE_TIME;
|
|
253
|
+
cache = /* @__PURE__ */ new Map();
|
|
254
|
+
initial() {
|
|
255
|
+
return {
|
|
256
|
+
rev: UNSET_REV,
|
|
257
|
+
dedupes: 0,
|
|
258
|
+
expires: 0,
|
|
259
|
+
data: null,
|
|
260
|
+
error: null,
|
|
261
|
+
loading: false
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
$get(key) {
|
|
265
|
+
const cache = this.cache;
|
|
266
|
+
if (!/* @__PURE__ */ hasShardedMapKey(cache, key)) setShardedMapKey(cache, key, this.initial());
|
|
267
|
+
return $getShardedMapKey(cache, key);
|
|
268
|
+
}
|
|
269
|
+
set(key, entry) {
|
|
270
|
+
setShardedMapKey(this.cache, key, entry);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Invalidate cache entry for the given key.
|
|
274
|
+
* If shard key is used, invalidate all entries in the shard.
|
|
275
|
+
* @param key - The cache key to invalidate.
|
|
276
|
+
*/
|
|
277
|
+
invalidate(key) {
|
|
278
|
+
deleteShardedMapKey(this.cache, key);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Revalidate cache entry for the given key.
|
|
282
|
+
* If shard key is used, revalidate all entries in the shard.
|
|
283
|
+
* @param key - The cache key to revalidate.
|
|
284
|
+
*/
|
|
285
|
+
revalidate(key) {
|
|
286
|
+
if (key.key === void 0 || /* @__PURE__ */ hasShardedMapKey(this.cache, key)) this.set(key, (entry) => ({
|
|
287
|
+
...entry,
|
|
288
|
+
rev: UNSET_REV,
|
|
289
|
+
dedupes: 0
|
|
290
|
+
}));
|
|
291
|
+
}
|
|
292
|
+
mute(entry, loadingDedupe = true, timeDedupe = true) {
|
|
293
|
+
return loadingDedupe && entry.loading || timeDedupe && entry.dedupes > Date.now() || /* @__PURE__ */ revLocked(entry.rev);
|
|
294
|
+
}
|
|
295
|
+
loading(key) {
|
|
296
|
+
const rev = ++revCounter;
|
|
297
|
+
this.set(key, (entry = this.initial()) => ({
|
|
298
|
+
...entry,
|
|
299
|
+
rev,
|
|
300
|
+
data: entry.expires > Date.now() ? entry.data : null,
|
|
301
|
+
error: null,
|
|
302
|
+
loading: true
|
|
303
|
+
}));
|
|
304
|
+
return rev;
|
|
305
|
+
}
|
|
306
|
+
settled(key, data, error, rev) {
|
|
307
|
+
const now = Date.now();
|
|
308
|
+
this.set(key, (entry = this.initial()) => rev !== void 0 && rev !== entry.rev ? entry : {
|
|
309
|
+
...entry,
|
|
310
|
+
dedupes: now + this.dedupeTime,
|
|
311
|
+
expires: now + this.cacheTime,
|
|
312
|
+
data: error === null ? data : entry.data,
|
|
313
|
+
error,
|
|
314
|
+
loading: false
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
//#endregion
|
|
319
|
+
//#region src/settings/codec.ts
|
|
320
|
+
function mapEntry(entry, mapDetail, mapData) {
|
|
321
|
+
return {
|
|
322
|
+
...entry,
|
|
323
|
+
data: mapData(entry.data),
|
|
324
|
+
rev: mapDetail(entry.rev),
|
|
325
|
+
dedupes: mapDetail(entry.dedupes),
|
|
326
|
+
expires: mapDetail(entry.expires)
|
|
327
|
+
};
|
|
284
328
|
}
|
|
285
|
-
|
|
329
|
+
function encodeEntryData(entry, codec) {
|
|
330
|
+
return mapEntry(entry, identity, codec.encode);
|
|
331
|
+
}
|
|
332
|
+
function decodeEntryData(entry, codec) {
|
|
333
|
+
return mapEntry(entry, identity, codec.decode);
|
|
334
|
+
}
|
|
335
|
+
function encodeEntryDetails(entry) {
|
|
336
|
+
return mapEntry(entry, String, identity);
|
|
337
|
+
}
|
|
338
|
+
function decodeEntryDetails(entry) {
|
|
339
|
+
return mapEntry(entry, Number, identity);
|
|
340
|
+
}
|
|
341
|
+
function encodeEntry(entry, codec) {
|
|
342
|
+
return mapEntry(entry, String, codec.encode);
|
|
343
|
+
}
|
|
344
|
+
function decodeEntry(entry, codec) {
|
|
345
|
+
return mapEntry(entry, Number, codec.decode);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Set cache entry data codec for query client.
|
|
349
|
+
* @param codec - Codec used to encode and decode cached data.
|
|
350
|
+
* @returns The client setting function.
|
|
351
|
+
*/
|
|
352
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
353
|
+
function codec(codec) {
|
|
354
|
+
return (ctx) => {
|
|
355
|
+
ctx.codec = codec;
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region src/settings/persistence.ts
|
|
360
|
+
/**
|
|
361
|
+
* Generic persistent storage client setting.
|
|
362
|
+
* @param storage - The storage implementation to use for persistence.
|
|
363
|
+
* @param lifetime - How long to keep entries in storage in milliseconds.
|
|
364
|
+
* @returns The client setting function.
|
|
365
|
+
*/
|
|
366
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
367
|
+
function persistence(storage, lifetime) {
|
|
368
|
+
return (ctx) => {
|
|
369
|
+
if (!storage) return;
|
|
370
|
+
if (ctx.persistenceLifetime === void 0) {
|
|
371
|
+
const superGet = ctx.$get;
|
|
372
|
+
const superSet = ctx.set;
|
|
373
|
+
const superInvalidate = ctx.invalidate;
|
|
374
|
+
ctx.$get = function(key) {
|
|
375
|
+
const cache = this.cache;
|
|
376
|
+
const hasKey = /* @__PURE__ */ hasShardedMapKey(cache, key);
|
|
377
|
+
const entry = superGet.call(this, key);
|
|
378
|
+
if (hasKey) return entry;
|
|
379
|
+
superSet.call(this, key, {
|
|
380
|
+
...entry,
|
|
381
|
+
rev: /* @__PURE__ */ revLock(entry.rev)
|
|
382
|
+
});
|
|
383
|
+
this.task(storage.get(key).then((storedEntry) => superSet.call(this, key, {
|
|
384
|
+
...entry,
|
|
385
|
+
...storedEntry && decodeEntryData(storedEntry, this.codec),
|
|
386
|
+
rev: UNSET_REV
|
|
387
|
+
})));
|
|
388
|
+
return superGet.call(this, key);
|
|
389
|
+
};
|
|
390
|
+
function saveSingleEntry(key) {
|
|
391
|
+
const entry = untracked(superGet.bind(this, key));
|
|
392
|
+
if (entry && !entry.loading && !entry.error && !/* @__PURE__ */ revLocked(entry.rev)) this.task(storage.set(key, encodeEntryData(entry, this.codec), this.persistenceLifetime));
|
|
393
|
+
}
|
|
394
|
+
ctx.set = function(cacheKey, entry) {
|
|
395
|
+
superSet.call(this, cacheKey, entry);
|
|
396
|
+
const { shard, key } = cacheKey;
|
|
397
|
+
if (key !== void 0) saveSingleEntry.call(this, cacheKey);
|
|
398
|
+
else {
|
|
399
|
+
const shardMap = this.cache.get(shard);
|
|
400
|
+
if (shardMap) for (const key of shardMap.keys()) saveSingleEntry.call(this, {
|
|
401
|
+
shard,
|
|
402
|
+
key
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
ctx.invalidate = function(key) {
|
|
407
|
+
superInvalidate.call(this, key);
|
|
408
|
+
this.task(storage.delete(key));
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
ctx.persistenceLifetime = lifetime;
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
//#endregion
|
|
415
|
+
//#region src/settings/indexedDbStorage.ts
|
|
286
416
|
const DB_NAME = "nano_kit";
|
|
287
417
|
const STORE_NAME = "query";
|
|
288
|
-
const DB_VERSION = 1;
|
|
289
418
|
function connect() {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
});
|
|
300
|
-
store.createIndex("shard", "shard", {
|
|
301
|
-
unique: false
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
};
|
|
305
|
-
});
|
|
419
|
+
return new Promise((resolve) => {
|
|
420
|
+
const request = indexedDB.open(DB_NAME, 1);
|
|
421
|
+
request.onerror = () => resolve(null);
|
|
422
|
+
request.onsuccess = () => resolve(request.result);
|
|
423
|
+
request.onupgradeneeded = () => {
|
|
424
|
+
const db = request.result;
|
|
425
|
+
if (!db.objectStoreNames.contains("query")) db.createObjectStore(STORE_NAME, { keyPath: ["shard", "key"] }).createIndex("shard", "shard", { unique: false });
|
|
426
|
+
};
|
|
427
|
+
});
|
|
306
428
|
}
|
|
307
429
|
async function SELECT(connection, key) {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
resolve(result.data);
|
|
330
|
-
};
|
|
331
|
-
});
|
|
430
|
+
const db = await connection;
|
|
431
|
+
if (!db) return null;
|
|
432
|
+
return new Promise((resolve) => {
|
|
433
|
+
const request = db.transaction(STORE_NAME, "readonly").objectStore(STORE_NAME).get([key.shard, key.key]);
|
|
434
|
+
request.onerror = () => resolve(null);
|
|
435
|
+
request.onsuccess = () => {
|
|
436
|
+
const result = request.result;
|
|
437
|
+
if (!result) {
|
|
438
|
+
resolve(null);
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
const now = Date.now();
|
|
442
|
+
if (result.expires < now || result.data.expires < now) {
|
|
443
|
+
DELETE(connection, key);
|
|
444
|
+
resolve(null);
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
resolve(result.data);
|
|
448
|
+
};
|
|
449
|
+
});
|
|
332
450
|
}
|
|
333
451
|
async function SET(connection, cacheKey, entry, lifetime) {
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
expires: Date.now() + lifetime
|
|
350
|
-
};
|
|
351
|
-
const request = store.put(storedEntry);
|
|
352
|
-
request.onerror = () => resolve();
|
|
353
|
-
request.onsuccess = () => resolve();
|
|
354
|
-
});
|
|
452
|
+
const db = await connection;
|
|
453
|
+
if (!db) return;
|
|
454
|
+
return new Promise((resolve) => {
|
|
455
|
+
const { shard, key } = cacheKey;
|
|
456
|
+
const store = db.transaction(STORE_NAME, "readwrite").objectStore(STORE_NAME);
|
|
457
|
+
const storedEntry = {
|
|
458
|
+
shard,
|
|
459
|
+
key,
|
|
460
|
+
data: entry,
|
|
461
|
+
expires: Date.now() + lifetime
|
|
462
|
+
};
|
|
463
|
+
const request = store.put(storedEntry);
|
|
464
|
+
request.onerror = () => resolve();
|
|
465
|
+
request.onsuccess = () => resolve();
|
|
466
|
+
});
|
|
355
467
|
}
|
|
356
468
|
async function DELETE(connection, cacheKey) {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
}
|
|
381
|
-
};
|
|
382
|
-
} else {
|
|
383
|
-
const request = store.delete([shard, key]);
|
|
384
|
-
request.onerror = () => resolve();
|
|
385
|
-
request.onsuccess = () => resolve();
|
|
386
|
-
}
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
// @__NO_SIDE_EFFECTS__
|
|
390
|
-
function indexedDbStorage(lifetime) {
|
|
391
|
-
return (ctx) => {
|
|
392
|
-
if (typeof indexedDB === "undefined") {
|
|
393
|
-
return;
|
|
394
|
-
}
|
|
395
|
-
if (ctx.indexedDbStorageLifetime === void 0) {
|
|
396
|
-
let saveSingleEntry = function(key) {
|
|
397
|
-
const entry = untracked(() => superGet.call(this, key));
|
|
398
|
-
if (entry && !entry.loading && !revLocked(entry.rev)) {
|
|
399
|
-
void this.task(SET(db, key, entry, this.indexedDbStorageLifetime));
|
|
400
|
-
}
|
|
401
|
-
};
|
|
402
|
-
const superGet = ctx.$get;
|
|
403
|
-
const superSet = ctx.set;
|
|
404
|
-
const superInvalidate = ctx.invalidate;
|
|
405
|
-
const db = connect();
|
|
406
|
-
ctx.$get = function(key) {
|
|
407
|
-
const cache = this.cache;
|
|
408
|
-
const hasKey = hasShardedMapKey(cache, key);
|
|
409
|
-
const entry = superGet.call(this, key);
|
|
410
|
-
if (hasKey) {
|
|
411
|
-
return entry;
|
|
412
|
-
}
|
|
413
|
-
superSet.call(this, key, {
|
|
414
|
-
...entry,
|
|
415
|
-
rev: revLock(entry.rev)
|
|
416
|
-
});
|
|
417
|
-
void this.task(SELECT(db, key).then((storedEntry) => superSet.call(this, key, {
|
|
418
|
-
...entry,
|
|
419
|
-
...storedEntry,
|
|
420
|
-
rev: UNSET_REV
|
|
421
|
-
})));
|
|
422
|
-
return superGet.call(this, key);
|
|
423
|
-
};
|
|
424
|
-
ctx.set = function(cacheKey, entry) {
|
|
425
|
-
superSet.call(this, cacheKey, entry);
|
|
426
|
-
const {
|
|
427
|
-
shard,
|
|
428
|
-
key
|
|
429
|
-
} = cacheKey;
|
|
430
|
-
if (key !== void 0) {
|
|
431
|
-
saveSingleEntry.call(this, cacheKey);
|
|
432
|
-
} else {
|
|
433
|
-
const shardMap = this.cache.get(shard);
|
|
434
|
-
if (shardMap) {
|
|
435
|
-
for (const key2 of shardMap.keys()) {
|
|
436
|
-
saveSingleEntry.call(this, {
|
|
437
|
-
shard,
|
|
438
|
-
key: key2
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
};
|
|
444
|
-
ctx.invalidate = function(key) {
|
|
445
|
-
superInvalidate.call(this, key);
|
|
446
|
-
void this.task(DELETE(db, key));
|
|
447
|
-
};
|
|
448
|
-
}
|
|
449
|
-
ctx.indexedDbStorageLifetime = lifetime;
|
|
450
|
-
};
|
|
469
|
+
const db = await connection;
|
|
470
|
+
if (!db) return;
|
|
471
|
+
return new Promise((resolve) => {
|
|
472
|
+
const { shard, key } = cacheKey;
|
|
473
|
+
const store = db.transaction(STORE_NAME, "readwrite").objectStore(STORE_NAME);
|
|
474
|
+
if (key === void 0) {
|
|
475
|
+
const index = store.index("shard");
|
|
476
|
+
const range = IDBKeyRange.only(shard);
|
|
477
|
+
const request = index.openCursor(range);
|
|
478
|
+
request.onerror = () => resolve();
|
|
479
|
+
request.onsuccess = () => {
|
|
480
|
+
const cursor = request.result;
|
|
481
|
+
if (cursor) {
|
|
482
|
+
cursor.delete();
|
|
483
|
+
cursor.continue();
|
|
484
|
+
} else resolve();
|
|
485
|
+
};
|
|
486
|
+
} else {
|
|
487
|
+
const request = store.delete([shard, key]);
|
|
488
|
+
request.onerror = () => resolve();
|
|
489
|
+
request.onsuccess = () => resolve();
|
|
490
|
+
}
|
|
491
|
+
});
|
|
451
492
|
}
|
|
452
|
-
|
|
453
|
-
|
|
493
|
+
/**
|
|
494
|
+
* IndexedDB adapter for persistent storage.
|
|
495
|
+
* @returns IndexedDB storage implementation or null if IndexedDB is not supported.
|
|
496
|
+
*/
|
|
497
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
498
|
+
function indexedDbStorage() {
|
|
499
|
+
if (typeof indexedDB === "undefined") return null;
|
|
500
|
+
const db = connect();
|
|
501
|
+
return {
|
|
502
|
+
get(key) {
|
|
503
|
+
return SELECT(db, key);
|
|
504
|
+
},
|
|
505
|
+
set(cacheKey, entry, lifetime) {
|
|
506
|
+
return SET(db, cacheKey, entry, lifetime);
|
|
507
|
+
},
|
|
508
|
+
delete(cacheKey) {
|
|
509
|
+
return DELETE(db, cacheKey);
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
//#endregion
|
|
514
|
+
//#region src/utils.ts
|
|
515
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
454
516
|
function addFn(prevFn, fn) {
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
fn.apply(this, args);
|
|
461
|
-
};
|
|
517
|
+
if (prevFn === void 0) return fn;
|
|
518
|
+
return function(...args) {
|
|
519
|
+
prevFn.apply(this, args);
|
|
520
|
+
fn.apply(this, args);
|
|
521
|
+
};
|
|
462
522
|
}
|
|
463
523
|
function settle(promise, onSettled) {
|
|
464
|
-
|
|
465
|
-
onSettled,
|
|
466
|
-
(error) => onSettled(void 0, error)
|
|
467
|
-
);
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
const $windowVisible = external(($windowVisible2) => {
|
|
471
|
-
if (typeof document === "undefined") {
|
|
472
|
-
$windowVisible2(true);
|
|
473
|
-
return;
|
|
474
|
-
}
|
|
475
|
-
const set = () => $windowVisible2(!document.hidden);
|
|
476
|
-
set();
|
|
477
|
-
onMount(mountable($windowVisible2), () => {
|
|
478
|
-
document.addEventListener("visibilitychange", set);
|
|
479
|
-
return () => {
|
|
480
|
-
document.removeEventListener("visibilitychange", set);
|
|
481
|
-
};
|
|
482
|
-
});
|
|
483
|
-
});
|
|
484
|
-
// @__NO_SIDE_EFFECTS__
|
|
485
|
-
function revalidateOnFocus() {
|
|
486
|
-
return (ctx) => {
|
|
487
|
-
if (ctx.revalidateOnFocusEnabled === void 0) {
|
|
488
|
-
ctx.mounted = addFn(ctx.mounted, function() {
|
|
489
|
-
listen($windowVisible, (visible) => {
|
|
490
|
-
if (visible) {
|
|
491
|
-
this.revalidate(this.$key());
|
|
492
|
-
}
|
|
493
|
-
});
|
|
494
|
-
});
|
|
495
|
-
}
|
|
496
|
-
ctx.revalidateOnFocusEnabled = true;
|
|
497
|
-
};
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
// @__NO_SIDE_EFFECTS__
|
|
501
|
-
function revalidateOnInterval(interval) {
|
|
502
|
-
return (ctx) => {
|
|
503
|
-
if (ctx.revalidateInterval === void 0) {
|
|
504
|
-
ctx.mounted = addFn(ctx.mounted, function() {
|
|
505
|
-
effect(() => {
|
|
506
|
-
const id = setInterval(() => {
|
|
507
|
-
this.revalidate(this.$key());
|
|
508
|
-
}, this.revalidateInterval);
|
|
509
|
-
return () => clearInterval(id);
|
|
510
|
-
});
|
|
511
|
-
});
|
|
512
|
-
}
|
|
513
|
-
ctx.revalidateInterval = interval;
|
|
514
|
-
};
|
|
524
|
+
return promise.then(onSettled, (error) => onSettled(void 0, error));
|
|
515
525
|
}
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
});
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
ctx.mounted = addFn(ctx.mounted, function() {
|
|
538
|
-
listen($networkOnline, (online) => {
|
|
539
|
-
if (online) {
|
|
540
|
-
this.revalidate(this.$key());
|
|
541
|
-
}
|
|
542
|
-
});
|
|
543
|
-
});
|
|
544
|
-
}
|
|
545
|
-
ctx.revalidateOnReconnectEnabled = true;
|
|
546
|
-
};
|
|
526
|
+
//#endregion
|
|
527
|
+
//#region src/settings/revalidateOn.ts
|
|
528
|
+
/**
|
|
529
|
+
* Revalidate the query when the reactive condition becomes true.
|
|
530
|
+
* @returns The client setting function.
|
|
531
|
+
*/
|
|
532
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
533
|
+
function revalidateOn(...conditions) {
|
|
534
|
+
return (ctx) => {
|
|
535
|
+
if (ctx.revalidateOn === void 0) ctx.revalidateOn = /* @__PURE__ */ new Set();
|
|
536
|
+
conditions.forEach(($condition) => {
|
|
537
|
+
if (!ctx.revalidateOn.has($condition)) {
|
|
538
|
+
ctx.mounted = /* @__PURE__ */ addFn(ctx.mounted, function() {
|
|
539
|
+
listen($condition, (visible) => {
|
|
540
|
+
if (visible) this.revalidate(this.$key());
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
ctx.revalidateOn.add($condition);
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
};
|
|
547
547
|
}
|
|
548
|
-
|
|
549
|
-
|
|
548
|
+
//#endregion
|
|
549
|
+
//#region src/settings/retryOnError.ts
|
|
550
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
550
551
|
function defaultCalcRetryDelay(retryCount) {
|
|
551
|
-
|
|
552
|
-
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
|
|
553
|
-
~~((Math.random() + 0.5) * (1 << (retryCount < 8 ? retryCount : 8))) * 2e3
|
|
554
|
-
);
|
|
552
|
+
return ~~((Math.random() + .5) * (1 << (retryCount < 8 ? retryCount : 8))) * 2e3;
|
|
555
553
|
}
|
|
556
554
|
function getRetryCount(ctx, key) {
|
|
557
|
-
|
|
555
|
+
return ctx.retryCounts?.get(key.key) || 0;
|
|
558
556
|
}
|
|
559
557
|
function setRetryCount(ctx, key, count) {
|
|
560
|
-
|
|
558
|
+
(ctx.retryCounts ??= /* @__PURE__ */ new Map()).set(key.key, count);
|
|
561
559
|
}
|
|
562
560
|
function clearRetryCount(ctx, key) {
|
|
563
|
-
|
|
561
|
+
ctx.retryCounts?.delete(key.key);
|
|
564
562
|
}
|
|
565
|
-
|
|
563
|
+
/**
|
|
564
|
+
* Retry the query on error with exponential backoff.
|
|
565
|
+
* @param calcRetryDelay - Function to calculate the delay before retrying.
|
|
566
|
+
* @returns The client setting function.
|
|
567
|
+
*/
|
|
568
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
566
569
|
function retryOnError(calcRetryDelay = defaultCalcRetryDelay) {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
return promise;
|
|
592
|
-
};
|
|
593
|
-
}
|
|
594
|
-
ctx.calcRetryDelay = calcRetryDelay;
|
|
595
|
-
};
|
|
570
|
+
return (ctx) => {
|
|
571
|
+
if (ctx.calcRetryDelay === void 0) {
|
|
572
|
+
const superRun = ctx.run;
|
|
573
|
+
ctx.run = function(queryCtx, start, onSettled, interrupt) {
|
|
574
|
+
clearTimeout(this.retryTimeoutId);
|
|
575
|
+
const promise = superRun.call(this, queryCtx, start, onSettled, interrupt);
|
|
576
|
+
if (!("shard" in queryCtx)) return promise;
|
|
577
|
+
this.task(promise.then((result) => {
|
|
578
|
+
const error = result?.[1];
|
|
579
|
+
if (!error) clearRetryCount(this, queryCtx);
|
|
580
|
+
else {
|
|
581
|
+
const retryCount = getRetryCount(this, queryCtx) + 1;
|
|
582
|
+
const delay = this.calcRetryDelay(retryCount, error);
|
|
583
|
+
this.retryTimeoutId = setTimeout(() => {
|
|
584
|
+
this.invalidate(queryCtx);
|
|
585
|
+
setRetryCount(this, queryCtx, retryCount);
|
|
586
|
+
}, delay);
|
|
587
|
+
}
|
|
588
|
+
}));
|
|
589
|
+
return promise;
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
ctx.calcRetryDelay = calcRetryDelay;
|
|
593
|
+
};
|
|
596
594
|
}
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
595
|
+
//#endregion
|
|
596
|
+
//#region src/settings/hydratable.ts
|
|
597
|
+
const id = "@nano_kit/query";
|
|
598
|
+
function encode({ cache, codec }) {
|
|
599
|
+
const encoded = [];
|
|
600
|
+
cache.forEach((shard, shardKey) => {
|
|
601
|
+
shard.forEach(($signal, key) => {
|
|
602
|
+
const value = $signal?.();
|
|
603
|
+
if (value !== void 0) encoded.push([
|
|
604
|
+
shardKey,
|
|
605
|
+
key,
|
|
606
|
+
encodeEntry(value, codec)
|
|
607
|
+
]);
|
|
608
|
+
});
|
|
609
|
+
});
|
|
610
|
+
return encoded;
|
|
611
|
+
}
|
|
612
|
+
function decode({ cache, codec }, encoded) {
|
|
613
|
+
encoded.forEach(([shard, key, value]) => setShardedMapKey(cache, {
|
|
614
|
+
shard,
|
|
615
|
+
key
|
|
616
|
+
}, decodeEntry(value, codec)));
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Make client cache hydratable.
|
|
620
|
+
* Without arguments, it will try to resolve {@link Hydrator$} and {@link Hydratables$} from the injection context.
|
|
621
|
+
* @param hydrator - Optional hydrator to use for rehydrating the cache. Pass `null` to skip hydration and only register for dehydration.
|
|
622
|
+
* @param hydratables - Optional map to register the cache collector for dehydration.
|
|
623
|
+
* @returns The client setting function.
|
|
624
|
+
*/
|
|
625
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
626
|
+
function hydratable(hydrator, hydratables) {
|
|
627
|
+
return (ctx) => {
|
|
628
|
+
if (!ctx.hydratable) {
|
|
629
|
+
const finalHydrator = hydrator === void 0 ? inject(Hydrator$) : hydrator;
|
|
630
|
+
if (finalHydrator) finalHydrator.pull(id, (value) => decode(ctx, value));
|
|
631
|
+
else {
|
|
632
|
+
const finalHydratables = hydratables === void 0 ? inject(Hydratables$) : hydratables;
|
|
633
|
+
if (finalHydratables) finalHydratables.set(id, () => encode(ctx));
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
ctx.hydratable = true;
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
//#endregion
|
|
640
|
+
//#region src/ClientContext.ts
|
|
641
|
+
var ClientContext = class extends CacheStorage {
|
|
642
|
+
$key = void 0;
|
|
643
|
+
$disabled = void 0;
|
|
644
|
+
loadingDedupe = true;
|
|
645
|
+
timeDedupe = true;
|
|
646
|
+
onEveryError = void 0;
|
|
647
|
+
codec = NoopCodec;
|
|
648
|
+
task(task) {
|
|
649
|
+
return taskPromise(task);
|
|
650
|
+
}
|
|
651
|
+
mapData(data) {
|
|
652
|
+
return data;
|
|
653
|
+
}
|
|
654
|
+
mapComputedData(data) {
|
|
655
|
+
return data;
|
|
656
|
+
}
|
|
657
|
+
mapError(error) {
|
|
658
|
+
return error?.message;
|
|
659
|
+
}
|
|
660
|
+
mounted() {}
|
|
661
|
+
mute(entry) {
|
|
662
|
+
return this.$disabled?.() === true || super.mute(entry, this.loadingDedupe, this.timeDedupe);
|
|
663
|
+
}
|
|
664
|
+
run(requestCtx, start, onSettled, interrupt) {
|
|
665
|
+
const { mapData, mapError } = this;
|
|
666
|
+
return this.task(settle(start(), (data, error) => {
|
|
667
|
+
if (error && interrupt?.(error)) return;
|
|
668
|
+
const mappedData = mapData(data);
|
|
669
|
+
onSettled(error ? null : mappedData, error ? mapError(error) : null);
|
|
670
|
+
requestCtx.settled(mappedData, error);
|
|
671
|
+
this.handleError(error, requestCtx.stopErrorPropagation);
|
|
672
|
+
return [mappedData, error];
|
|
673
|
+
}));
|
|
674
|
+
}
|
|
675
|
+
handleError(error, stopped) {
|
|
676
|
+
if (error !== void 0) this.onEveryError?.(error, stopped);
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
645
680
|
function forkMutationClient(ctx, settings = []) {
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
}
|
|
650
|
-
return child;
|
|
681
|
+
const child = Object.create(ctx);
|
|
682
|
+
for (const setting of settings) setting(child);
|
|
683
|
+
return child;
|
|
651
684
|
}
|
|
652
|
-
|
|
685
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
653
686
|
function forkQueryClient(ctx, $key, settings = []) {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
687
|
+
const child = Object.create(ctx);
|
|
688
|
+
child.$key = $key;
|
|
689
|
+
for (const setting of settings) setting(child);
|
|
690
|
+
return child;
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Set dedupe time in which identical requests are deduped.
|
|
694
|
+
* @param time - Dedupe time in milliseconds.
|
|
695
|
+
* @returns The client setting function.
|
|
696
|
+
*/
|
|
697
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
662
698
|
function dedupeTime(time) {
|
|
663
|
-
|
|
699
|
+
return (ctx) => ctx.dedupeTime = time;
|
|
664
700
|
}
|
|
665
|
-
|
|
701
|
+
/**
|
|
702
|
+
* Set cache time for cached query results.
|
|
703
|
+
* @param time - Cache time in milliseconds.
|
|
704
|
+
* @returns The client setting function.
|
|
705
|
+
*/
|
|
706
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
666
707
|
function cacheTime(time) {
|
|
667
|
-
|
|
708
|
+
return (ctx) => ctx.cacheTime = time;
|
|
668
709
|
}
|
|
669
|
-
|
|
710
|
+
/**
|
|
711
|
+
* Map error object to string.
|
|
712
|
+
* @param fn - Function to map error to string.
|
|
713
|
+
* @returns The client setting function.
|
|
714
|
+
*/
|
|
715
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
670
716
|
function mapError(fn) {
|
|
671
|
-
|
|
717
|
+
return (ctx) => ctx.mapError = fn;
|
|
672
718
|
}
|
|
673
|
-
|
|
719
|
+
/**
|
|
720
|
+
* Register a callback to be called on every error.
|
|
721
|
+
* @param fn - The error callback.
|
|
722
|
+
* @returns The client setting function.
|
|
723
|
+
*/
|
|
724
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
674
725
|
function onEveryError(fn) {
|
|
675
|
-
|
|
726
|
+
return (ctx) => ctx.onEveryError = /* @__PURE__ */ addFn(ctx.onEveryError, fn);
|
|
676
727
|
}
|
|
677
|
-
|
|
728
|
+
/**
|
|
729
|
+
* Disable requests when the signal is true.
|
|
730
|
+
* @param $disabled - Readable signal indicating whether requests are disabled.
|
|
731
|
+
* @returns The client setting function.
|
|
732
|
+
*/
|
|
733
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
678
734
|
function disabled($disabled) {
|
|
679
|
-
|
|
735
|
+
return (ctx) => ctx.$disabled = $disabled;
|
|
680
736
|
}
|
|
681
|
-
|
|
737
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
682
738
|
function dedupe(loading, time = loading) {
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
739
|
+
return (ctx) => {
|
|
740
|
+
ctx.loadingDedupe = loading;
|
|
741
|
+
ctx.timeDedupe = time;
|
|
742
|
+
};
|
|
687
743
|
}
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
744
|
+
/**
|
|
745
|
+
* Set task runner for handling tasks.
|
|
746
|
+
* Without arguments, it will try to resolve a tasks runner from the current injection context.
|
|
747
|
+
* @param runner - The tasks runner function.
|
|
748
|
+
* @returns The client setting function.
|
|
749
|
+
*/
|
|
750
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
751
|
+
function tasks(runner = inject(TasksRunner$)) {
|
|
752
|
+
return (ctx) => ctx.task = runner;
|
|
691
753
|
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
this.onError?.(error);
|
|
708
|
-
} else {
|
|
709
|
-
this.onSuccess?.(data);
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
class QueryContext extends RequestContext {
|
|
714
|
-
shard;
|
|
715
|
-
key;
|
|
716
|
-
P;
|
|
717
|
-
R;
|
|
718
|
-
constructor(key, prevCtx) {
|
|
719
|
-
super(prevCtx);
|
|
720
|
-
this.shard = key.shard;
|
|
721
|
-
this.key = key.key;
|
|
722
|
-
}
|
|
754
|
+
//#endregion
|
|
755
|
+
//#region src/settings/ssr.ts
|
|
756
|
+
/**
|
|
757
|
+
* Client setting for enabling SSR support.
|
|
758
|
+
* It combines tasks management and cache hydration to ensure that queries can be executed on the server and their results can be sent to the client for hydration.
|
|
759
|
+
* Should be called inside injection context.
|
|
760
|
+
* @returns The client setting function.
|
|
761
|
+
*/
|
|
762
|
+
function ssr() {
|
|
763
|
+
const tasksSetting = /* @__PURE__ */ tasks();
|
|
764
|
+
const hydratableSetting = /* @__PURE__ */ hydratable();
|
|
765
|
+
return (ctx) => {
|
|
766
|
+
tasksSetting(ctx);
|
|
767
|
+
hydratableSetting(ctx);
|
|
768
|
+
};
|
|
723
769
|
}
|
|
770
|
+
//#endregion
|
|
771
|
+
//#region src/RequestContext.ts
|
|
772
|
+
var RequestContext = class {
|
|
773
|
+
onSuccess = void 0;
|
|
774
|
+
onError = void 0;
|
|
775
|
+
onSettled = void 0;
|
|
776
|
+
stopErrorPropagation = false;
|
|
777
|
+
prevCtx = void 0;
|
|
778
|
+
constructor(prevCtx) {
|
|
779
|
+
if (this.prevCtx = prevCtx) prevCtx.prevCtx = void 0;
|
|
780
|
+
}
|
|
781
|
+
settled(data, error) {
|
|
782
|
+
this.onSettled?.(data, error);
|
|
783
|
+
if (error !== void 0) this.onError?.(error);
|
|
784
|
+
else this.onSuccess?.(data);
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
var QueryContext = class extends RequestContext {
|
|
788
|
+
shard;
|
|
789
|
+
key;
|
|
790
|
+
P;
|
|
791
|
+
R;
|
|
792
|
+
constructor(key, prevCtx) {
|
|
793
|
+
super(prevCtx);
|
|
794
|
+
this.shard = key.shard;
|
|
795
|
+
this.key = key.key;
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
/**
|
|
799
|
+
* Add a success callback to the query context.
|
|
800
|
+
* @param ctx - The query context.
|
|
801
|
+
* @param fn - The success callback.
|
|
802
|
+
*/
|
|
724
803
|
function onSuccess(ctx, fn) {
|
|
725
|
-
|
|
804
|
+
ctx.onSuccess = /* @__PURE__ */ addFn(ctx.onSuccess, fn);
|
|
726
805
|
}
|
|
806
|
+
/**
|
|
807
|
+
* Add an error callback to the query context.
|
|
808
|
+
* @param ctx - The query context.
|
|
809
|
+
* @param fn - The error callback.
|
|
810
|
+
*/
|
|
727
811
|
function onError(ctx, fn) {
|
|
728
|
-
|
|
812
|
+
ctx.onError = /* @__PURE__ */ addFn(ctx.onError, fn);
|
|
729
813
|
}
|
|
814
|
+
/**
|
|
815
|
+
* Add a settled callback to the query context.
|
|
816
|
+
* @param ctx - The query context.
|
|
817
|
+
* @param fn - The settled callback.
|
|
818
|
+
*/
|
|
730
819
|
function onSettled(ctx, fn) {
|
|
731
|
-
|
|
820
|
+
ctx.onSettled = /* @__PURE__ */ addFn(ctx.onSettled, fn);
|
|
732
821
|
}
|
|
822
|
+
/**
|
|
823
|
+
* Mark error as "stopped", so error will be passed to onEveryError with stopped=true.
|
|
824
|
+
* @param ctx - The query context.
|
|
825
|
+
*/
|
|
733
826
|
function stopErrorPropagation(ctx) {
|
|
734
|
-
|
|
827
|
+
ctx.stopErrorPropagation = true;
|
|
735
828
|
}
|
|
736
|
-
|
|
737
|
-
|
|
829
|
+
//#endregion
|
|
830
|
+
//#region src/queries/base.ts
|
|
831
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
738
832
|
function baseQuery(rootCtx, key, params, fn, settings) {
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
$key,
|
|
777
|
-
$rev,
|
|
778
|
-
$data,
|
|
779
|
-
$error,
|
|
780
|
-
$loading
|
|
781
|
-
};
|
|
833
|
+
const $params = computed(() => params.map($get));
|
|
834
|
+
const $key = computed((prevKey) => {
|
|
835
|
+
const nextKey = key(...$params());
|
|
836
|
+
if (prevKey && prevKey.shard === nextKey.shard && prevKey.key === nextKey.key) return prevKey;
|
|
837
|
+
return nextKey;
|
|
838
|
+
});
|
|
839
|
+
const clientCtx = /* @__PURE__ */ forkQueryClient(rootCtx, $key, settings);
|
|
840
|
+
const $entry = computed(() => clientCtx.$get($key()));
|
|
841
|
+
/**
|
|
842
|
+
* Changes on every entry rev reset
|
|
843
|
+
*/
|
|
844
|
+
const $rev = computed((v = 0) => $entry().rev === Infinity ? v + 1 : v);
|
|
845
|
+
const { mapComputedData } = clientCtx;
|
|
846
|
+
const $data = mountable(computed(() => mapComputedData($entry().data)));
|
|
847
|
+
const $error = computed(() => $entry().error);
|
|
848
|
+
const $loading = computed(() => $entry().loading);
|
|
849
|
+
let prevQueryCtx;
|
|
850
|
+
return {
|
|
851
|
+
clientCtx,
|
|
852
|
+
fetch: action((...extraParams) => {
|
|
853
|
+
if (clientCtx.mute($entry())) return Promise.resolve();
|
|
854
|
+
const key = $key();
|
|
855
|
+
const params = $params();
|
|
856
|
+
const queryCtx = prevQueryCtx = new QueryContext(key, prevQueryCtx);
|
|
857
|
+
let rev;
|
|
858
|
+
return clientCtx.run(queryCtx, () => {
|
|
859
|
+
rev = clientCtx.loading(key);
|
|
860
|
+
return fn(...params, ...extraParams, queryCtx);
|
|
861
|
+
}, (data, error) => clientCtx.settled(key, data, error, rev));
|
|
862
|
+
}),
|
|
863
|
+
$params,
|
|
864
|
+
$key,
|
|
865
|
+
$rev,
|
|
866
|
+
$data,
|
|
867
|
+
$error,
|
|
868
|
+
$loading
|
|
869
|
+
};
|
|
782
870
|
}
|
|
783
|
-
|
|
784
|
-
|
|
871
|
+
//#endregion
|
|
872
|
+
//#region src/queries/query.ts
|
|
873
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
785
874
|
function query(key, params, fn, settings) {
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
});
|
|
803
|
-
clientCtx.mounted();
|
|
804
|
-
}));
|
|
805
|
-
return [$data, $error, $loading, $key];
|
|
875
|
+
const { clientCtx, fetch, $params, $key, $rev, $data, $error, $loading } = /* @__PURE__ */ baseQuery(this, key, params, fn, settings);
|
|
876
|
+
onStart($data, () => effectScope(() => {
|
|
877
|
+
effect(() => {
|
|
878
|
+
$rev();
|
|
879
|
+
$params();
|
|
880
|
+
clientCtx.$disabled?.();
|
|
881
|
+
fetch();
|
|
882
|
+
});
|
|
883
|
+
clientCtx.mounted();
|
|
884
|
+
}));
|
|
885
|
+
return [
|
|
886
|
+
$data,
|
|
887
|
+
$error,
|
|
888
|
+
$loading,
|
|
889
|
+
$key
|
|
890
|
+
];
|
|
806
891
|
}
|
|
807
|
-
|
|
808
|
-
|
|
892
|
+
//#endregion
|
|
893
|
+
//#region src/queries/infinite.ts
|
|
894
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
809
895
|
function infinite(key, params, next, fn, settings) {
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
});
|
|
847
|
-
clientCtx.mounted();
|
|
848
|
-
}));
|
|
849
|
-
return [fetchNext, $data, $error, $loading, $key];
|
|
896
|
+
const { clientCtx, fetch, $params, $key, $rev, $data, $error, $loading } = /* @__PURE__ */ baseQuery(this, key, params, async (...args) => {
|
|
897
|
+
const queryCtx = args[args.length - 1];
|
|
898
|
+
const cursor = args[args.length - 2];
|
|
899
|
+
const data = clientCtx.$get(queryCtx).data;
|
|
900
|
+
const page = await fn(...args);
|
|
901
|
+
const nextValue = next(page);
|
|
902
|
+
return {
|
|
903
|
+
pages: cursor === void 0 ? [page] : [...data?.pages || [], page],
|
|
904
|
+
next: nextValue,
|
|
905
|
+
more: Boolean(nextValue)
|
|
906
|
+
};
|
|
907
|
+
}, settings);
|
|
908
|
+
const initialTimeDedupe = clientCtx.timeDedupe;
|
|
909
|
+
const fetchNext = action(() => {
|
|
910
|
+
const data = $data();
|
|
911
|
+
if (!data?.more) return Promise.resolve();
|
|
912
|
+
clientCtx.timeDedupe = false;
|
|
913
|
+
return fetch(data.next);
|
|
914
|
+
});
|
|
915
|
+
onStart($data, () => effectScope(() => {
|
|
916
|
+
effect(() => {
|
|
917
|
+
$rev();
|
|
918
|
+
$params();
|
|
919
|
+
clientCtx.$disabled?.();
|
|
920
|
+
clientCtx.timeDedupe = initialTimeDedupe;
|
|
921
|
+
fetch(void 0);
|
|
922
|
+
});
|
|
923
|
+
clientCtx.mounted();
|
|
924
|
+
}));
|
|
925
|
+
return [
|
|
926
|
+
fetchNext,
|
|
927
|
+
$data,
|
|
928
|
+
$error,
|
|
929
|
+
$loading,
|
|
930
|
+
$key
|
|
931
|
+
];
|
|
850
932
|
}
|
|
851
|
-
|
|
852
|
-
|
|
933
|
+
//#endregion
|
|
934
|
+
//#region src/queries/operation.ts
|
|
935
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
853
936
|
function operation(key, params, fn, settings = []) {
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
return [fetch, $data, $error, $loading, $key];
|
|
937
|
+
const { clientCtx, fetch, $key, $data, $error, $loading } = /* @__PURE__ */ baseQuery(this, key, params, fn, [/* @__PURE__ */ dedupe(true, false), ...settings]);
|
|
938
|
+
onStart($data, () => effectScope(() => clientCtx.mounted()));
|
|
939
|
+
return [
|
|
940
|
+
fetch,
|
|
941
|
+
$data,
|
|
942
|
+
$error,
|
|
943
|
+
$loading,
|
|
944
|
+
$key
|
|
945
|
+
];
|
|
864
946
|
}
|
|
865
|
-
|
|
866
|
-
|
|
947
|
+
//#endregion
|
|
948
|
+
//#region src/queries/mutation.ts
|
|
949
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
867
950
|
function mutation(fn, settings = []) {
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
$result(data);
|
|
895
|
-
}
|
|
896
|
-
$error(error);
|
|
897
|
-
$loading(false);
|
|
898
|
-
});
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
);
|
|
902
|
-
});
|
|
903
|
-
return [
|
|
904
|
-
fetch,
|
|
905
|
-
$data,
|
|
906
|
-
readonly($error),
|
|
907
|
-
readonly($loading)
|
|
908
|
-
];
|
|
951
|
+
const clientCtx = /* @__PURE__ */ forkMutationClient(this, settings);
|
|
952
|
+
const { mapComputedData, $disabled, loadingDedupe } = clientCtx;
|
|
953
|
+
const $result = signal(null);
|
|
954
|
+
const $data = computed(() => mapComputedData($result()));
|
|
955
|
+
const $error = signal(null);
|
|
956
|
+
const $loading = signal(false);
|
|
957
|
+
let prevRequestCtx;
|
|
958
|
+
return [
|
|
959
|
+
action((...params) => {
|
|
960
|
+
if ($disabled?.() === true || loadingDedupe && $loading()) return Promise.resolve();
|
|
961
|
+
const requestCtx = prevRequestCtx = new RequestContext(prevRequestCtx);
|
|
962
|
+
return clientCtx.run(requestCtx, () => {
|
|
963
|
+
$loading(true);
|
|
964
|
+
return fn(...params, requestCtx);
|
|
965
|
+
}, (data, error) => {
|
|
966
|
+
if (prevRequestCtx === requestCtx) batch(() => {
|
|
967
|
+
if (error === null) $result(data);
|
|
968
|
+
$error(error);
|
|
969
|
+
$loading(false);
|
|
970
|
+
});
|
|
971
|
+
});
|
|
972
|
+
}),
|
|
973
|
+
$data,
|
|
974
|
+
readonly($error),
|
|
975
|
+
readonly($loading)
|
|
976
|
+
];
|
|
909
977
|
}
|
|
910
|
-
|
|
911
|
-
|
|
978
|
+
//#endregion
|
|
979
|
+
//#region src/client.ts
|
|
980
|
+
/**
|
|
981
|
+
* Create a query client with optional settings and extensions.
|
|
982
|
+
* @param settings - The client settings and extensions.
|
|
983
|
+
* @returns The query client.
|
|
984
|
+
*/
|
|
985
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
912
986
|
function client(...settings) {
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
987
|
+
const ctx = new ClientContext();
|
|
988
|
+
const client = {
|
|
989
|
+
query: query.bind(ctx),
|
|
990
|
+
invalidate: ((key) => ctx.invalidate(key)),
|
|
991
|
+
revalidate: ((key) => ctx.revalidate(key)),
|
|
992
|
+
$data: /* @__PURE__ */ dataCacheFacade(ctx),
|
|
993
|
+
$error: /* @__PURE__ */ errorCacheFacade(ctx),
|
|
994
|
+
$loading: /* @__PURE__ */ loadingCacheFacade(ctx)
|
|
995
|
+
};
|
|
996
|
+
for (const setting of settings) setting(ctx, client);
|
|
997
|
+
return client;
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Extend client with infinite query capability.
|
|
1001
|
+
* @returns The client extension.
|
|
1002
|
+
*/
|
|
1003
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
929
1004
|
function infinites() {
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
1005
|
+
return ((ctx, client) => {
|
|
1006
|
+
client.infinite = infinite.bind(ctx);
|
|
1007
|
+
return client;
|
|
1008
|
+
});
|
|
934
1009
|
}
|
|
935
|
-
|
|
1010
|
+
/**
|
|
1011
|
+
* Extend client with operation capability.
|
|
1012
|
+
* @returns The client extension.
|
|
1013
|
+
*/
|
|
1014
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
936
1015
|
function operations() {
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
1016
|
+
return ((ctx, client) => {
|
|
1017
|
+
client.operation = operation.bind(ctx);
|
|
1018
|
+
return client;
|
|
1019
|
+
});
|
|
941
1020
|
}
|
|
942
|
-
|
|
1021
|
+
/**
|
|
1022
|
+
* Extend client with mutation capability.
|
|
1023
|
+
* @returns The client extension.
|
|
1024
|
+
*/
|
|
1025
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
943
1026
|
function mutations() {
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
1027
|
+
return ((ctx, client) => {
|
|
1028
|
+
client.mutation = mutation.bind(ctx);
|
|
1029
|
+
return client;
|
|
1030
|
+
});
|
|
948
1031
|
}
|
|
1032
|
+
//#endregion
|
|
1033
|
+
export { ClientContext, DEFAULT_CACHE_TIME, DEFAULT_DEDUPE_TIME, QueryContext, RequestContext, abort, abortPrevious, abortSignal, abortable, cacheTime, client, codec, decodeEntry, decodeEntryData, decodeEntryDetails, dedupe, dedupeTime, defaultCalcRetryDelay, disabled, encodeEntry, encodeEntryData, encodeEntryDetails, entities, entity, hydratable, indexedDbStorage, infinites, keys, mapError, mutations, onError, onEveryError, onSettled, onSuccess, operationKey, operations, persistence, queryKey, retryOnError, revalidateOn, ssr, stopErrorPropagation, tasks };
|
|
949
1034
|
|
|
950
|
-
|
|
951
|
-
//# sourceMappingURL=index.js.map
|
|
1035
|
+
//# sourceMappingURL=index.js.map
|