@vite-hub/device 0.4.1 → 0.5.1
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/{device-CxuDKYfP.js → device-Bh2mN8kx.js} +20 -64
- package/dist/index.d.ts +0 -4
- package/dist/index.js +2 -3
- package/dist/vue.d.ts +1 -3
- package/dist/vue.js +16 -54
- package/package.json +9 -9
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defineErrors } from "@vite-hub/runtime";
|
|
2
|
-
|
|
2
|
+
// src/errors.ts
|
|
3
3
|
const deviceErrors = /*#__PURE__*/ defineErrors({
|
|
4
4
|
docs: "https://vitehub.dev/docs/device/diagnostics",
|
|
5
5
|
codes: {
|
|
@@ -47,8 +47,7 @@ function warn(error) {
|
|
|
47
47
|
if (error.cause === void 0) console.warn(message);
|
|
48
48
|
else console.warn(message, error.cause);
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
//#region src/indexeddb.ts
|
|
50
|
+
// src/indexeddb.ts
|
|
52
51
|
const databaseName = "vitehub-device";
|
|
53
52
|
const format = 3;
|
|
54
53
|
const metaKey = "device";
|
|
@@ -86,10 +85,6 @@ function openIndexedDB() {
|
|
|
86
85
|
request.onerror = () => reject(request.error);
|
|
87
86
|
});
|
|
88
87
|
}
|
|
89
|
-
/**
|
|
90
|
-
* Deletes the entries that expired at `now`, then the oldest entries of every tab until the entries hold at most
|
|
91
|
-
* `maxBytes`. Runs inside the write, so it picks from what the disk holds, not from what a tab remembers.
|
|
92
|
-
*/
|
|
93
88
|
function trim(entries, maxBytes, now) {
|
|
94
89
|
const rows = [];
|
|
95
90
|
let total = 0;
|
|
@@ -209,11 +204,8 @@ function browserChannel() {
|
|
|
209
204
|
}
|
|
210
205
|
};
|
|
211
206
|
}
|
|
212
|
-
|
|
213
|
-
//#region src/device.ts
|
|
214
|
-
/** The device keeps at most 5 MB of JSON. `openDevice()` reads all of it before the app mounts. */
|
|
207
|
+
// src/device.ts
|
|
215
208
|
const maxDeviceBytes = 5242880;
|
|
216
|
-
/** `openDevice()` waits at most 1 second for IndexedDB, so a slow or stuck browser does not stop the app. */
|
|
217
209
|
const openTimeout = 1e3;
|
|
218
210
|
const encoder = /*#__PURE__*/ new TextEncoder();
|
|
219
211
|
const ok = [void 0, void 0];
|
|
@@ -231,44 +223,26 @@ function checkMaxAge(maxAge) {
|
|
|
231
223
|
if (typeof maxAge !== "number" || Number.isNaN(maxAge) || maxAge < 0) throw deviceErrors.DEVICE_INVALID_MAX_AGE({ maxAge: describe(maxAge) });
|
|
232
224
|
return maxAge;
|
|
233
225
|
}
|
|
234
|
-
/** A random id for a new generation of the disk. */
|
|
235
226
|
function newGeneration() {
|
|
236
227
|
return Array.from(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(8)), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
237
228
|
}
|
|
238
229
|
function createDevice(options) {
|
|
239
230
|
const now = options.now ?? (() => Date.now());
|
|
240
231
|
const maxBytes = options.maxBytes ?? maxDeviceBytes;
|
|
241
|
-
/** The copies that new reads of this tab show: the copies of the disk, and the answers that the device stores. */
|
|
242
232
|
const entries = /* @__PURE__ */ new Map();
|
|
243
|
-
|
|
233
|
+
let heldBytes = 0;
|
|
244
234
|
const pending = /* @__PURE__ */ new Map();
|
|
245
235
|
const listeners = /* @__PURE__ */ new Set();
|
|
246
236
|
const warned = /* @__PURE__ */ new Set();
|
|
247
237
|
let storage;
|
|
248
238
|
let opening;
|
|
249
|
-
/** `true` once `open()` finished, so `setAccount()` can name the account at once. */
|
|
250
239
|
let opened = false;
|
|
251
|
-
/** `true` when the device opened without a disk, so nothing waits to be written. */
|
|
252
240
|
let diskless = false;
|
|
253
241
|
let version = "";
|
|
254
|
-
/**
|
|
255
|
-
* The meta that this tab expects on the disk. A change of this tab sets it in the queue, after its disk operation, so
|
|
256
|
-
* it follows the order of the operations. A message of another tab sets it at once.
|
|
257
|
-
*/
|
|
258
242
|
let meta;
|
|
259
|
-
/**
|
|
260
|
-
* `true` once this tab saw the disk in the version of its build. When the disk shows another version for the same
|
|
261
|
-
* account after that, a newer build opened in another tab, and this tab leaves the disk to it.
|
|
262
|
-
*/
|
|
263
243
|
let versionSeen = false;
|
|
264
|
-
/** The account that this page named. `undefined` before it calls `setAccount()`, or after another tab named another account. */
|
|
265
244
|
let named;
|
|
266
|
-
/** The account of the latest `setAccount()` call, also while the device opens. */
|
|
267
245
|
let requested;
|
|
268
|
-
/**
|
|
269
|
-
* Changes when the page names an account, and when the copies and the answers in memory stop being valid. The device
|
|
270
|
-
* stores an answer only when its loader call started in the current epoch, after the page named an account.
|
|
271
|
-
*/
|
|
272
246
|
let epoch = 0;
|
|
273
247
|
let queue = Promise.resolve();
|
|
274
248
|
let flushQueued = false;
|
|
@@ -282,7 +256,6 @@ function createDevice(options) {
|
|
|
282
256
|
if (event.type === "load" && !event.keys.length) return;
|
|
283
257
|
for (const listener of listeners) listener(event);
|
|
284
258
|
}
|
|
285
|
-
/** Runs disk operations, and the changes of `meta` that come with them, one after another in the order of the calls. */
|
|
286
259
|
function enqueue(task) {
|
|
287
260
|
const run = queue.then(task);
|
|
288
261
|
queue = run.catch(() => void 0);
|
|
@@ -297,34 +270,27 @@ function createDevice(options) {
|
|
|
297
270
|
cause
|
|
298
271
|
}));
|
|
299
272
|
}
|
|
300
|
-
/** Forgets every copy and answer in memory, and tells the reads to start over. */
|
|
301
273
|
function invalidate() {
|
|
302
274
|
epoch++;
|
|
303
275
|
entries.clear();
|
|
276
|
+
heldBytes = 0;
|
|
304
277
|
pending.clear();
|
|
305
278
|
emit({ type: "reset" });
|
|
306
279
|
}
|
|
307
|
-
/** Sets the meta that this tab expects on the disk. */
|
|
308
280
|
function setMeta(next) {
|
|
309
281
|
meta = next;
|
|
310
282
|
if (next?.version === version) versionSeen = true;
|
|
311
283
|
}
|
|
312
|
-
/** Writes `change` when the disk still has the generation `expected`. Runs in the queue. */
|
|
313
284
|
async function persist(target, expected, change) {
|
|
314
285
|
const found = await target.write(expected, change);
|
|
315
286
|
if (found?.generation === expected.generation || meta !== expected) return;
|
|
316
287
|
adopt(found);
|
|
317
288
|
}
|
|
318
|
-
/**
|
|
319
|
-
* Takes the meta that the disk has, after another tab changed the disk and this tab missed its message. Everything of
|
|
320
|
-
* this tab is old. After another account, this tab stores again only after it names an account.
|
|
321
|
-
*/
|
|
322
289
|
function adopt(found) {
|
|
323
290
|
setMeta(found);
|
|
324
291
|
if (found?.account !== named) named = void 0;
|
|
325
292
|
invalidate();
|
|
326
293
|
}
|
|
327
|
-
/** Writes the pending changes in one write, after the operations that are already queued. */
|
|
328
294
|
function flush() {
|
|
329
295
|
if (flushQueued || !pending.size) return;
|
|
330
296
|
flushQueued = true;
|
|
@@ -345,7 +311,6 @@ function createDevice(options) {
|
|
|
345
311
|
await persist(target, expected, change);
|
|
346
312
|
}).catch(backgroundFailure);
|
|
347
313
|
}
|
|
348
|
-
/** Deletes the expired copies on the disk, and the oldest ones over the cap. The disk picks them in its transaction. */
|
|
349
314
|
function trim() {
|
|
350
315
|
enqueue(async () => {
|
|
351
316
|
if (storage && meta) await persist(storage, meta, {
|
|
@@ -356,15 +321,19 @@ function createDevice(options) {
|
|
|
356
321
|
});
|
|
357
322
|
}).catch(backgroundFailure);
|
|
358
323
|
}
|
|
359
|
-
|
|
324
|
+
function hold(entry) {
|
|
325
|
+
heldBytes += entry.bytes - (entries.get(entry.key)?.bytes ?? 0);
|
|
326
|
+
entries.set(entry.key, entry);
|
|
327
|
+
}
|
|
328
|
+
function release(key) {
|
|
329
|
+
heldBytes -= entries.get(key)?.bytes ?? 0;
|
|
330
|
+
entries.delete(key);
|
|
331
|
+
}
|
|
360
332
|
function evict(keep) {
|
|
361
|
-
|
|
362
|
-
for (const entry of entries.values()) total += entry.bytes;
|
|
333
|
+
if (heldBytes <= maxBytes) return;
|
|
363
334
|
for (const entry of [...entries.values()].sort((a, b) => a.storedAt - b.storedAt)) {
|
|
364
|
-
if (
|
|
365
|
-
if (entry.key
|
|
366
|
-
entries.delete(entry.key);
|
|
367
|
-
total -= entry.bytes;
|
|
335
|
+
if (heldBytes <= maxBytes) return;
|
|
336
|
+
if (entry.key !== keep) release(entry.key);
|
|
368
337
|
}
|
|
369
338
|
}
|
|
370
339
|
function receive(message) {
|
|
@@ -416,7 +385,6 @@ function createDevice(options) {
|
|
|
416
385
|
const since = epoch;
|
|
417
386
|
enqueue(async () => epoch === since ? ensure(account, false, fromPage) : ok).then(([error]) => error && warnOnce("storage", error));
|
|
418
387
|
}
|
|
419
|
-
/** Takes the copies that the disk had when the device opened. */
|
|
420
388
|
function take(found, stored) {
|
|
421
389
|
setMeta(found);
|
|
422
390
|
if (!found || found.version !== version || found.account === null || requested !== void 0 && requested !== found.account) return;
|
|
@@ -431,7 +399,7 @@ function createDevice(options) {
|
|
|
431
399
|
}
|
|
432
400
|
total += entry.bytes;
|
|
433
401
|
if (!entries.has(entry.key)) {
|
|
434
|
-
|
|
402
|
+
hold({
|
|
435
403
|
...entry,
|
|
436
404
|
account: found.account
|
|
437
405
|
});
|
|
@@ -445,15 +413,6 @@ function createDevice(options) {
|
|
|
445
413
|
keys: loaded.filter((key) => entries.has(key))
|
|
446
414
|
});
|
|
447
415
|
}
|
|
448
|
-
/**
|
|
449
|
-
* Brings the disk to `account` in the version of this build. When the disk holds another account or version, every copy
|
|
450
|
-
* goes and the disk gets a new generation, and the other tabs hear of it. Runs in the queue. In this tab it is not a
|
|
451
|
-
* reset: the tab raised the epoch when it named the account, and holds no copy of another account or version.
|
|
452
|
-
*
|
|
453
|
-
* One naming, one reset attempt. The reset applies only on the generation that this tab knew when the task started.
|
|
454
|
-
* On any other generation, the tab takes the disk as it is and does not write the meta again until the page names an
|
|
455
|
-
* account.
|
|
456
|
-
*/
|
|
457
416
|
async function ensure(account, announce, fromPage) {
|
|
458
417
|
if (fromPage && named !== account) return ok;
|
|
459
418
|
const target = storage;
|
|
@@ -501,7 +460,6 @@ function createDevice(options) {
|
|
|
501
460
|
flush();
|
|
502
461
|
return ok;
|
|
503
462
|
}
|
|
504
|
-
/** Names `account` in memory, at once, so that loader calls that started before it are never stored. */
|
|
505
463
|
function name(account) {
|
|
506
464
|
const previous = named;
|
|
507
465
|
named = account;
|
|
@@ -558,7 +516,7 @@ function createDevice(options) {
|
|
|
558
516
|
function claim(key, value, maxAge, since, account) {
|
|
559
517
|
checkAccount(account);
|
|
560
518
|
if (!opening) warnOnce("open", deviceErrors.DEVICE_NOT_OPEN());
|
|
561
|
-
if (since !== epoch
|
|
519
|
+
if (since !== epoch) return false;
|
|
562
520
|
const naming = account !== named;
|
|
563
521
|
if (naming) {
|
|
564
522
|
requested = account;
|
|
@@ -569,7 +527,6 @@ function createDevice(options) {
|
|
|
569
527
|
if (typeof account === "string") store(key, value, maxAge, account);
|
|
570
528
|
return true;
|
|
571
529
|
}
|
|
572
|
-
/** Keeps `value` as the copy of `key` for `account`, in memory and on the disk. */
|
|
573
530
|
function store(key, value, maxAge, account) {
|
|
574
531
|
let json;
|
|
575
532
|
try {
|
|
@@ -591,7 +548,7 @@ function createDevice(options) {
|
|
|
591
548
|
json = void 0;
|
|
592
549
|
}
|
|
593
550
|
if (json === void 0) {
|
|
594
|
-
|
|
551
|
+
release(key);
|
|
595
552
|
if (!diskless) pending.set(key, void 0);
|
|
596
553
|
return flush();
|
|
597
554
|
}
|
|
@@ -603,7 +560,7 @@ function createDevice(options) {
|
|
|
603
560
|
expiresAt: time + maxAge,
|
|
604
561
|
bytes
|
|
605
562
|
};
|
|
606
|
-
|
|
563
|
+
hold({
|
|
607
564
|
...entry,
|
|
608
565
|
account
|
|
609
566
|
});
|
|
@@ -682,5 +639,4 @@ function currentDevice() {
|
|
|
682
639
|
});
|
|
683
640
|
return browserDevice;
|
|
684
641
|
}
|
|
685
|
-
//#endregion
|
|
686
642
|
export { deviceErrors as a, currentDevice as i, checkKey as n, checkMaxAge as r, checkAccount as t };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Result } from "@vite-hub/runtime";
|
|
2
|
-
//#region src/device.d.ts
|
|
3
2
|
/** What `inspectDevice()` returns. */
|
|
4
3
|
interface DeviceInspection {
|
|
5
4
|
/**
|
|
@@ -25,8 +24,6 @@ interface OpenDeviceOptions {
|
|
|
25
24
|
*/
|
|
26
25
|
version?: string;
|
|
27
26
|
}
|
|
28
|
-
//#endregion
|
|
29
|
-
//#region src/index.d.ts
|
|
30
27
|
/**
|
|
31
28
|
* Reads the stored copies into memory, so the first render of `useStoredData()` shows them at once. Await it before
|
|
32
29
|
* `app.mount()`. It never fails: without IndexedDB, or when IndexedDB takes more than 1 second, the page keeps its
|
|
@@ -52,5 +49,4 @@ export declare function setDeviceAccount(account: string | null): Promise<Result
|
|
|
52
49
|
export declare function clearDevice(): Promise<Result<void, "DEVICE_STORAGE_FAILED">>;
|
|
53
50
|
/** The account, the storage, and each stored copy of this tab, to debug stored reads in code or in the browser console. */
|
|
54
51
|
export declare function inspectDevice(): DeviceInspection;
|
|
55
|
-
//#endregion
|
|
56
52
|
export type { DeviceInspection, OpenDeviceOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { i as currentDevice, t as checkAccount } from "./device-
|
|
2
|
-
|
|
1
|
+
import { i as currentDevice, t as checkAccount } from "./device-Bh2mN8kx.js";
|
|
2
|
+
// src/index.ts
|
|
3
3
|
/**
|
|
4
4
|
* Reads the stored copies into memory, so the first render of `useStoredData()` shows them at once. Await it before
|
|
5
5
|
* `app.mount()`. It never fails: without IndexedDB, or when IndexedDB takes more than 1 second, the page keeps its
|
|
@@ -39,5 +39,4 @@ function inspectDevice() {
|
|
|
39
39
|
entries: []
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
|
-
//#endregion
|
|
43
42
|
export { clearDevice, inspectDevice, openDevice, setDeviceAccount };
|
package/dist/vue.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { MaybeRefOrGetter, Ref } from "vue";
|
|
2
|
-
//#region src/vue.d.ts
|
|
3
2
|
/** `idle` on the server and before the loader starts, `pending` while the loader runs, then `success` or `error`. */
|
|
4
3
|
export type StoredDataStatus = "idle" | "pending" | "success" | "error";
|
|
5
4
|
export interface UseStoredDataOptions<TData, TDefault> {
|
|
@@ -74,5 +73,4 @@ export declare function useStoredData<TData, TDefault = undefined>(key: MaybeRef
|
|
|
74
73
|
export declare function useStoredFetch<TData = unknown>(url: MaybeRefOrGetter<string>, options: UseStoredFetchOptions<TData, TData> & {
|
|
75
74
|
default: () => NoInfer<TData>;
|
|
76
75
|
}): UseStoredDataReturn<TData, TData>;
|
|
77
|
-
export declare function useStoredFetch<TData = unknown, TDefault = undefined>(url: MaybeRefOrGetter<string>, options?: UseStoredFetchOptions<TData, TDefault>): UseStoredDataReturn<TData, TDefault>;
|
|
78
|
-
//#endregion
|
|
76
|
+
export declare function useStoredFetch<TData = unknown, TDefault = undefined>(url: MaybeRefOrGetter<string>, options?: UseStoredFetchOptions<TData, TDefault>): UseStoredDataReturn<TData, TDefault>;
|
package/dist/vue.js
CHANGED
|
@@ -1,27 +1,12 @@
|
|
|
1
|
-
import { a as deviceErrors, i as currentDevice, n as checkKey, r as checkMaxAge, t as checkAccount } from "./device-
|
|
1
|
+
import { a as deviceErrors, i as currentDevice, n as checkKey, r as checkMaxAge, t as checkAccount } from "./device-Bh2mN8kx.js";
|
|
2
2
|
import { computed, getCurrentInstance, getCurrentScope, onMounted, onScopeDispose, shallowRef, toValue, watch } from "vue";
|
|
3
|
-
|
|
3
|
+
// src/vue.ts
|
|
4
4
|
const week = 6048e5;
|
|
5
5
|
const reads = /* @__PURE__ */ new Map();
|
|
6
6
|
let watching = false;
|
|
7
|
-
/**
|
|
8
|
-
* The read whose answer names the account right now, and the epoch of the event of that naming. For that event only, it
|
|
9
|
-
* keeps its answer and does not start over. A later event, such as a naming that the event started, applies to it.
|
|
10
|
-
*/
|
|
11
7
|
let claiming;
|
|
12
|
-
/** The reads with `account` whose loader runs. */
|
|
13
8
|
const identifying = /* @__PURE__ */ new Set();
|
|
14
|
-
/**
|
|
15
|
-
* The reads that show a stored copy and wait for the naming before they call their loader. A held read always starts
|
|
16
|
-
* its loader, at the naming or when the last running read with `account` ends or goes. The hold only saves the call that
|
|
17
|
-
* the naming would start over: it never decides what is stored. It applies to reads that start after the session read,
|
|
18
|
-
* and not to reads that wait for the mount during hydration.
|
|
19
|
-
*/
|
|
20
9
|
const held = /* @__PURE__ */ new Set();
|
|
21
|
-
/**
|
|
22
|
-
* Calls the loaders of the held reads, when the read that says who is signed in failed or went before it named an
|
|
23
|
-
* account. A naming calls them through its event.
|
|
24
|
-
*/
|
|
25
10
|
function startHeld(device) {
|
|
26
11
|
const waiting = [...held];
|
|
27
12
|
for (const read of waiting) {
|
|
@@ -29,10 +14,6 @@ function startHeld(device) {
|
|
|
29
14
|
load(read, read.loader, device);
|
|
30
15
|
}
|
|
31
16
|
}
|
|
32
|
-
/**
|
|
33
|
-
* Follows the device: a copy that loads late shows, a naming of the account calls every loader again, and a reset starts
|
|
34
|
-
* every read over.
|
|
35
|
-
*/
|
|
36
17
|
function watchDevice(device) {
|
|
37
18
|
if (watching) return;
|
|
38
19
|
watching = true;
|
|
@@ -93,25 +74,21 @@ async function load(read, loader, device) {
|
|
|
93
74
|
return device.write(read.key, value, read.maxAge, since);
|
|
94
75
|
}
|
|
95
76
|
checkAccount(account);
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
claiming = void 0;
|
|
110
|
-
}
|
|
111
|
-
if (claimed) break;
|
|
112
|
-
if (controller.signal.aborted || device.epoch() !== since || !device.loading()) return;
|
|
77
|
+
if (device.loading()) {
|
|
78
|
+
await device.whenOpen();
|
|
79
|
+
if (controller.signal.aborted || device.epoch() !== since) return;
|
|
80
|
+
}
|
|
81
|
+
claiming = {
|
|
82
|
+
read,
|
|
83
|
+
epoch: since + 1
|
|
84
|
+
};
|
|
85
|
+
let claimed;
|
|
86
|
+
try {
|
|
87
|
+
claimed = device.claim(read.key, value, read.maxAge, since, account);
|
|
88
|
+
} finally {
|
|
89
|
+
claiming = void 0;
|
|
113
90
|
}
|
|
114
|
-
if (controller.signal.aborted) return;
|
|
91
|
+
if (!claimed || controller.signal.aborted) return;
|
|
115
92
|
publish(read, value);
|
|
116
93
|
} catch (error) {
|
|
117
94
|
if (controller.signal.aborted || device.epoch() !== since) return;
|
|
@@ -125,7 +102,6 @@ async function load(read, loader, device) {
|
|
|
125
102
|
}
|
|
126
103
|
}
|
|
127
104
|
}
|
|
128
|
-
/** Shows an answer of the loader in the read. */
|
|
129
105
|
function publish(read, value) {
|
|
130
106
|
read.answered = true;
|
|
131
107
|
read.data.value = value;
|
|
@@ -133,7 +109,6 @@ function publish(read, value) {
|
|
|
133
109
|
read.error.value = void 0;
|
|
134
110
|
read.status.value = "success";
|
|
135
111
|
}
|
|
136
|
-
/** Returns the shared read of `key`. A new read calls its loader at once, unless `start` is `false`: then it stays `idle`. */
|
|
137
112
|
function acquire(key, loader, fallback, maxAge, account, device, owner, start = true) {
|
|
138
113
|
const existing = reads.get(key);
|
|
139
114
|
if (existing) {
|
|
@@ -163,11 +138,6 @@ function acquire(key, loader, fallback, maxAge, account, device, owner, start =
|
|
|
163
138
|
if (start) load(read, loader, device);
|
|
164
139
|
return read;
|
|
165
140
|
}
|
|
166
|
-
/**
|
|
167
|
-
* When a user of a read goes, such as a component that unmounts or moves to another key, its loader goes too: a reset
|
|
168
|
-
* calls the loader of a user that still reads this key, not one that now reads another key. When the last user goes,
|
|
169
|
-
* the loader call stops, so a late answer cannot replace a newer one.
|
|
170
|
-
*/
|
|
171
141
|
function release(read, device, owner) {
|
|
172
142
|
read.users--;
|
|
173
143
|
const gone = read.loaders.get(owner);
|
|
@@ -208,11 +178,6 @@ function useStoredFetch(url, options = {}) {
|
|
|
208
178
|
const { query, ...read } = options;
|
|
209
179
|
return useRead(() => fetchKey(toValue(url), toValue(query)), fetchJSON, read);
|
|
210
180
|
}
|
|
211
|
-
/**
|
|
212
|
-
* The key of a `useStoredFetch()` read, which is also the URL that it requests: the path and the query of a URL on the
|
|
213
|
-
* origin of the page, or the whole URL on another origin. The query is sorted by name, so the order in the code does
|
|
214
|
-
* not make another key. The hash goes.
|
|
215
|
-
*/
|
|
216
181
|
function fetchKey(url, query) {
|
|
217
182
|
const page = typeof document === "undefined" ? "http://localhost/" : document.baseURI;
|
|
218
183
|
const target = new URL(url, page);
|
|
@@ -221,7 +186,6 @@ function fetchKey(url, query) {
|
|
|
221
186
|
target.hash = "";
|
|
222
187
|
return target.origin === new URL(page).origin ? target.pathname + target.search : target.href;
|
|
223
188
|
}
|
|
224
|
-
/** Asks for the JSON at `key`. An answer that is not 2xx throws, so the device never stores an error body. */
|
|
225
189
|
async function fetchJSON({ signal }, key) {
|
|
226
190
|
const response = await fetch(key, {
|
|
227
191
|
signal,
|
|
@@ -233,7 +197,6 @@ async function fetchJSON({ signal }, key) {
|
|
|
233
197
|
});
|
|
234
198
|
return await response.json();
|
|
235
199
|
}
|
|
236
|
-
/** The shared read of `key` for one component, or the server render of it. */
|
|
237
200
|
function useRead(key, loader, options) {
|
|
238
201
|
const maxAge = checkMaxAge(options.maxAge ?? week);
|
|
239
202
|
const fallback = options.default ?? (() => void 0);
|
|
@@ -278,5 +241,4 @@ function useRead(key, loader, options) {
|
|
|
278
241
|
refresh: () => load(current.value, loader, device)
|
|
279
242
|
};
|
|
280
243
|
}
|
|
281
|
-
//#endregion
|
|
282
244
|
export { useStoredData, useStoredFetch };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vite-hub/device",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Stored reads for the browser: show the last server answer at once from the device, for the signed-in account, then refresh it.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cache",
|
|
@@ -40,19 +40,19 @@
|
|
|
40
40
|
"./package.json": "./package.json"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@vite-hub/runtime": "0.
|
|
43
|
+
"@vite-hub/runtime": "0.5.1"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@types/node": "^24.19.
|
|
46
|
+
"@types/node": "^24.19.2",
|
|
47
47
|
"@vite-hub/internal": "0.0.1",
|
|
48
|
-
"happy-dom": "21.0.
|
|
49
|
-
"playwright-core": "^1.
|
|
48
|
+
"happy-dom": "21.0.4",
|
|
49
|
+
"playwright-core": "^1.65.1",
|
|
50
50
|
"publint": "^0.3.25",
|
|
51
51
|
"typescript": "^6.0.3",
|
|
52
|
-
"vite": "^8.
|
|
53
|
-
"vite-plus": "^
|
|
54
|
-
"vitest": "
|
|
55
|
-
"vue": "3.5.
|
|
52
|
+
"vite": "^8.3.8",
|
|
53
|
+
"vite-plus": "^1.7.0",
|
|
54
|
+
"vitest": "5.1.0",
|
|
55
|
+
"vue": "3.5.46"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"vue": "^3.5.0"
|