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