@vite-hub/device 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{device-CxuDKYfP.js → device-BTHQPRQv.js} +17 -58
- package/dist/index.js +1 -1
- package/dist/vue.js +15 -52
- package/package.json +3 -3
|
@@ -86,10 +86,6 @@ function openIndexedDB() {
|
|
|
86
86
|
request.onerror = () => reject(request.error);
|
|
87
87
|
});
|
|
88
88
|
}
|
|
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
89
|
function trim(entries, maxBytes, now) {
|
|
94
90
|
const rows = [];
|
|
95
91
|
let total = 0;
|
|
@@ -211,9 +207,7 @@ function browserChannel() {
|
|
|
211
207
|
}
|
|
212
208
|
//#endregion
|
|
213
209
|
//#region src/device.ts
|
|
214
|
-
/** The device keeps at most 5 MB of JSON. `openDevice()` reads all of it before the app mounts. */
|
|
215
210
|
const maxDeviceBytes = 5242880;
|
|
216
|
-
/** `openDevice()` waits at most 1 second for IndexedDB, so a slow or stuck browser does not stop the app. */
|
|
217
211
|
const openTimeout = 1e3;
|
|
218
212
|
const encoder = /*#__PURE__*/ new TextEncoder();
|
|
219
213
|
const ok = [void 0, void 0];
|
|
@@ -231,44 +225,26 @@ function checkMaxAge(maxAge) {
|
|
|
231
225
|
if (typeof maxAge !== "number" || Number.isNaN(maxAge) || maxAge < 0) throw deviceErrors.DEVICE_INVALID_MAX_AGE({ maxAge: describe(maxAge) });
|
|
232
226
|
return maxAge;
|
|
233
227
|
}
|
|
234
|
-
/** A random id for a new generation of the disk. */
|
|
235
228
|
function newGeneration() {
|
|
236
229
|
return Array.from(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(8)), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
237
230
|
}
|
|
238
231
|
function createDevice(options) {
|
|
239
232
|
const now = options.now ?? (() => Date.now());
|
|
240
233
|
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
234
|
const entries = /* @__PURE__ */ new Map();
|
|
243
|
-
|
|
235
|
+
let heldBytes = 0;
|
|
244
236
|
const pending = /* @__PURE__ */ new Map();
|
|
245
237
|
const listeners = /* @__PURE__ */ new Set();
|
|
246
238
|
const warned = /* @__PURE__ */ new Set();
|
|
247
239
|
let storage;
|
|
248
240
|
let opening;
|
|
249
|
-
/** `true` once `open()` finished, so `setAccount()` can name the account at once. */
|
|
250
241
|
let opened = false;
|
|
251
|
-
/** `true` when the device opened without a disk, so nothing waits to be written. */
|
|
252
242
|
let diskless = false;
|
|
253
243
|
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
244
|
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
245
|
let versionSeen = false;
|
|
264
|
-
/** The account that this page named. `undefined` before it calls `setAccount()`, or after another tab named another account. */
|
|
265
246
|
let named;
|
|
266
|
-
/** The account of the latest `setAccount()` call, also while the device opens. */
|
|
267
247
|
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
248
|
let epoch = 0;
|
|
273
249
|
let queue = Promise.resolve();
|
|
274
250
|
let flushQueued = false;
|
|
@@ -282,7 +258,6 @@ function createDevice(options) {
|
|
|
282
258
|
if (event.type === "load" && !event.keys.length) return;
|
|
283
259
|
for (const listener of listeners) listener(event);
|
|
284
260
|
}
|
|
285
|
-
/** Runs disk operations, and the changes of `meta` that come with them, one after another in the order of the calls. */
|
|
286
261
|
function enqueue(task) {
|
|
287
262
|
const run = queue.then(task);
|
|
288
263
|
queue = run.catch(() => void 0);
|
|
@@ -297,34 +272,27 @@ function createDevice(options) {
|
|
|
297
272
|
cause
|
|
298
273
|
}));
|
|
299
274
|
}
|
|
300
|
-
/** Forgets every copy and answer in memory, and tells the reads to start over. */
|
|
301
275
|
function invalidate() {
|
|
302
276
|
epoch++;
|
|
303
277
|
entries.clear();
|
|
278
|
+
heldBytes = 0;
|
|
304
279
|
pending.clear();
|
|
305
280
|
emit({ type: "reset" });
|
|
306
281
|
}
|
|
307
|
-
/** Sets the meta that this tab expects on the disk. */
|
|
308
282
|
function setMeta(next) {
|
|
309
283
|
meta = next;
|
|
310
284
|
if (next?.version === version) versionSeen = true;
|
|
311
285
|
}
|
|
312
|
-
/** Writes `change` when the disk still has the generation `expected`. Runs in the queue. */
|
|
313
286
|
async function persist(target, expected, change) {
|
|
314
287
|
const found = await target.write(expected, change);
|
|
315
288
|
if (found?.generation === expected.generation || meta !== expected) return;
|
|
316
289
|
adopt(found);
|
|
317
290
|
}
|
|
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
291
|
function adopt(found) {
|
|
323
292
|
setMeta(found);
|
|
324
293
|
if (found?.account !== named) named = void 0;
|
|
325
294
|
invalidate();
|
|
326
295
|
}
|
|
327
|
-
/** Writes the pending changes in one write, after the operations that are already queued. */
|
|
328
296
|
function flush() {
|
|
329
297
|
if (flushQueued || !pending.size) return;
|
|
330
298
|
flushQueued = true;
|
|
@@ -345,7 +313,6 @@ function createDevice(options) {
|
|
|
345
313
|
await persist(target, expected, change);
|
|
346
314
|
}).catch(backgroundFailure);
|
|
347
315
|
}
|
|
348
|
-
/** Deletes the expired copies on the disk, and the oldest ones over the cap. The disk picks them in its transaction. */
|
|
349
316
|
function trim() {
|
|
350
317
|
enqueue(async () => {
|
|
351
318
|
if (storage && meta) await persist(storage, meta, {
|
|
@@ -356,15 +323,19 @@ function createDevice(options) {
|
|
|
356
323
|
});
|
|
357
324
|
}).catch(backgroundFailure);
|
|
358
325
|
}
|
|
359
|
-
|
|
326
|
+
function hold(entry) {
|
|
327
|
+
heldBytes += entry.bytes - (entries.get(entry.key)?.bytes ?? 0);
|
|
328
|
+
entries.set(entry.key, entry);
|
|
329
|
+
}
|
|
330
|
+
function release(key) {
|
|
331
|
+
heldBytes -= entries.get(key)?.bytes ?? 0;
|
|
332
|
+
entries.delete(key);
|
|
333
|
+
}
|
|
360
334
|
function evict(keep) {
|
|
361
|
-
|
|
362
|
-
for (const entry of entries.values()) total += entry.bytes;
|
|
335
|
+
if (heldBytes <= maxBytes) return;
|
|
363
336
|
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;
|
|
337
|
+
if (heldBytes <= maxBytes) return;
|
|
338
|
+
if (entry.key !== keep) release(entry.key);
|
|
368
339
|
}
|
|
369
340
|
}
|
|
370
341
|
function receive(message) {
|
|
@@ -416,7 +387,6 @@ function createDevice(options) {
|
|
|
416
387
|
const since = epoch;
|
|
417
388
|
enqueue(async () => epoch === since ? ensure(account, false, fromPage) : ok).then(([error]) => error && warnOnce("storage", error));
|
|
418
389
|
}
|
|
419
|
-
/** Takes the copies that the disk had when the device opened. */
|
|
420
390
|
function take(found, stored) {
|
|
421
391
|
setMeta(found);
|
|
422
392
|
if (!found || found.version !== version || found.account === null || requested !== void 0 && requested !== found.account) return;
|
|
@@ -431,7 +401,7 @@ function createDevice(options) {
|
|
|
431
401
|
}
|
|
432
402
|
total += entry.bytes;
|
|
433
403
|
if (!entries.has(entry.key)) {
|
|
434
|
-
|
|
404
|
+
hold({
|
|
435
405
|
...entry,
|
|
436
406
|
account: found.account
|
|
437
407
|
});
|
|
@@ -445,15 +415,6 @@ function createDevice(options) {
|
|
|
445
415
|
keys: loaded.filter((key) => entries.has(key))
|
|
446
416
|
});
|
|
447
417
|
}
|
|
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
418
|
async function ensure(account, announce, fromPage) {
|
|
458
419
|
if (fromPage && named !== account) return ok;
|
|
459
420
|
const target = storage;
|
|
@@ -501,7 +462,6 @@ function createDevice(options) {
|
|
|
501
462
|
flush();
|
|
502
463
|
return ok;
|
|
503
464
|
}
|
|
504
|
-
/** Names `account` in memory, at once, so that loader calls that started before it are never stored. */
|
|
505
465
|
function name(account) {
|
|
506
466
|
const previous = named;
|
|
507
467
|
named = account;
|
|
@@ -558,7 +518,7 @@ function createDevice(options) {
|
|
|
558
518
|
function claim(key, value, maxAge, since, account) {
|
|
559
519
|
checkAccount(account);
|
|
560
520
|
if (!opening) warnOnce("open", deviceErrors.DEVICE_NOT_OPEN());
|
|
561
|
-
if (since !== epoch
|
|
521
|
+
if (since !== epoch) return false;
|
|
562
522
|
const naming = account !== named;
|
|
563
523
|
if (naming) {
|
|
564
524
|
requested = account;
|
|
@@ -569,7 +529,6 @@ function createDevice(options) {
|
|
|
569
529
|
if (typeof account === "string") store(key, value, maxAge, account);
|
|
570
530
|
return true;
|
|
571
531
|
}
|
|
572
|
-
/** Keeps `value` as the copy of `key` for `account`, in memory and on the disk. */
|
|
573
532
|
function store(key, value, maxAge, account) {
|
|
574
533
|
let json;
|
|
575
534
|
try {
|
|
@@ -591,7 +550,7 @@ function createDevice(options) {
|
|
|
591
550
|
json = void 0;
|
|
592
551
|
}
|
|
593
552
|
if (json === void 0) {
|
|
594
|
-
|
|
553
|
+
release(key);
|
|
595
554
|
if (!diskless) pending.set(key, void 0);
|
|
596
555
|
return flush();
|
|
597
556
|
}
|
|
@@ -603,7 +562,7 @@ function createDevice(options) {
|
|
|
603
562
|
expiresAt: time + maxAge,
|
|
604
563
|
bytes
|
|
605
564
|
};
|
|
606
|
-
|
|
565
|
+
hold({
|
|
607
566
|
...entry,
|
|
608
567
|
account
|
|
609
568
|
});
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as currentDevice, t as checkAccount } from "./device-
|
|
1
|
+
import { i as currentDevice, t as checkAccount } from "./device-BTHQPRQv.js";
|
|
2
2
|
//#region 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
|
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-BTHQPRQv.js";
|
|
2
2
|
import { computed, getCurrentInstance, getCurrentScope, onMounted, onScopeDispose, shallowRef, toValue, watch } from "vue";
|
|
3
3
|
//#region 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);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vite-hub/device",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
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,7 +40,7 @@
|
|
|
40
40
|
"./package.json": "./package.json"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@vite-hub/runtime": "0.
|
|
43
|
+
"@vite-hub/runtime": "0.5.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@types/node": "^24.19.1",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
65
|
"engines": {
|
|
66
|
-
"node": ">=
|
|
66
|
+
"node": ">=24.15.0"
|
|
67
67
|
},
|
|
68
68
|
"scripts": {
|
|
69
69
|
"build": "vp pack",
|