tsdf 17.0.1 → 17.0.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/async-storage.d.ts +1 -1
- package/dist/async-storage.js +1 -1
- package/dist/{asyncStorageAdapter-CMohMeKm.d.ts → asyncStorageAdapter-DgsuE-T8.d.ts} +1 -1
- package/dist/{asyncStorageAdapter-BuGjbXYA.js → asyncStorageAdapter-URS9eulL.js} +165 -25
- package/dist/{asyncStorageAdapters-ClUvxNQA.js → asyncStorageAdapters-Bxrrj_Mt.js} +2 -2
- package/dist/indexed-db-storage.d.ts +1 -1
- package/dist/indexed-db-storage.js +1 -1
- package/dist/{indexedDbPersistentStorage-BSlgLveB.js → indexedDbPersistentStorage-BameJ4st.js} +11 -1
- package/dist/main.d.ts +34 -30
- package/dist/main.js +856 -257
- package/dist/opfs-storage.d.ts +1 -1
- package/dist/opfs-storage.js +1 -1
- package/dist/{opfsPersistentStorage-Ci0JcCo5.d.ts → opfsPersistentStorage-BngS1m0X.d.ts} +1 -1
- package/dist/{opfsPersistentStorage-D_k77Srq.js → opfsPersistentStorage-JsSCNBTr.js} +1 -1
- package/dist/{types-D0ttVGmj.d.ts → types-BlyGC_PO.d.ts} +3 -3
- package/docs/browser-tabs-sync.md +2 -0
- package/docs/fetch-scheduling.md +6 -1
- package/docs/hooks.md +39 -4
- package/docs/mutations.md +7 -3
- package/docs/persistent-storage.md +2 -0
- package/docs/shared-types.md +2 -1
- package/package.json +5 -2
package/dist/async-storage.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as createAsyncStorageAdapter } from "./asyncStorageAdapter-
|
|
1
|
+
import { t as createAsyncStorageAdapter } from "./asyncStorageAdapter-DgsuE-T8.js";
|
|
2
2
|
export { createAsyncStorageAdapter };
|
package/dist/async-storage.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { i as createAsyncStorageAdapter } from "./asyncStorageAdapter-
|
|
1
|
+
import { i as createAsyncStorageAdapter } from "./asyncStorageAdapter-URS9eulL.js";
|
|
2
2
|
export { createAsyncStorageAdapter };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { r as AsyncStorageDriver, t as AsyncStorageAdapter } from "./types-
|
|
1
|
+
import { r as AsyncStorageDriver, t as AsyncStorageAdapter } from "./types-BlyGC_PO.js";
|
|
2
2
|
|
|
3
3
|
//#region src/persistentStorage/asyncStorageAdapter.d.ts
|
|
4
4
|
declare function createAsyncStorageAdapter(driver: AsyncStorageDriver): AsyncStorageAdapter;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { filterAndMap } from "@ls-stack/utils/arrayUtils";
|
|
2
|
+
import { deepEqual } from "@ls-stack/utils/deepEqual";
|
|
1
3
|
import { __LEGIT_CAST__ } from "@ls-stack/utils/saferTyping";
|
|
2
4
|
import { klona } from "klona/json";
|
|
3
5
|
import { isObject } from "@ls-stack/utils/typeGuards";
|
|
4
6
|
import { createCache } from "@ls-stack/utils/cache";
|
|
5
|
-
import { deepEqual } from "@ls-stack/utils/deepEqual";
|
|
6
7
|
import { sleep } from "@ls-stack/utils/sleep";
|
|
7
8
|
import { rc_array, rc_discriminated_union, rc_literals, rc_number, rc_object, rc_parse, rc_parse_json, rc_record, rc_string, rc_tuple, rc_unknown } from "runcheck";
|
|
8
9
|
import { asPossiblyUndefined } from "@ls-stack/utils/typingFnUtils";
|
|
@@ -192,6 +193,10 @@ function compareMetadata(left, right, order) {
|
|
|
192
193
|
if (left.lastAccessAt !== right.lastAccessAt) return order === "lru-asc" ? left.lastAccessAt - right.lastAccessAt : right.lastAccessAt - left.lastAccessAt;
|
|
193
194
|
return left.key.localeCompare(right.key);
|
|
194
195
|
}
|
|
196
|
+
/** Whether an entry still has the metadata a conditional remove expects. */
|
|
197
|
+
function matchesConditionalRemove(remove, lastAccessAt, sizeBytes, version, offlineProtected) {
|
|
198
|
+
return remove.lastAccessAt === lastAccessAt && remove.sizeBytes === sizeBytes && remove.version === version && remove.offlineProtected === offlineProtected;
|
|
199
|
+
}
|
|
195
200
|
//#endregion
|
|
196
201
|
//#region src/persistentStorage/compactLocalStorageEntry.ts
|
|
197
202
|
function createCompactLocalStorageEntry(value, version) {
|
|
@@ -410,6 +415,41 @@ function serializeJsonForStorage(value) {
|
|
|
410
415
|
sizeBytes: getSerializedStringSize(rawValue)
|
|
411
416
|
};
|
|
412
417
|
}
|
|
418
|
+
/**
|
|
419
|
+
* Removes planned async maintenance entries in bounded batches. Each batch is
|
|
420
|
+
* a conditional remove, so entries other code or tabs changed since planning
|
|
421
|
+
* are kept as stale in the same write instead of being deleted through an
|
|
422
|
+
* outdated plan. The plan itself is never restarted, so every deadline makes
|
|
423
|
+
* progress. `shouldContinue` is checked around every batch so a plan never
|
|
424
|
+
* outlives the session it was built for; `onRemoved` only runs while it holds.
|
|
425
|
+
*/
|
|
426
|
+
async function removeAsyncMaintenanceEntriesIfUnchanged(plannedEntries, isOfflineProtected, removeIfUnchanged, onRemoved, batchSize, shouldContinue, idleContext) {
|
|
427
|
+
let hasStaleEntries = false;
|
|
428
|
+
for (let batchStart = 0; batchStart < plannedEntries.length; batchStart += batchSize) {
|
|
429
|
+
if (!await idleContext.yieldIfNeeded() || !shouldContinue()) return {
|
|
430
|
+
completed: false,
|
|
431
|
+
hasStaleEntries
|
|
432
|
+
};
|
|
433
|
+
const batch = plannedEntries.slice(batchStart, batchStart + batchSize).map((entry) => ({
|
|
434
|
+
key: entry.key,
|
|
435
|
+
lastAccessAt: entry.lastAccessAt,
|
|
436
|
+
sizeBytes: entry.sizeBytes,
|
|
437
|
+
version: entry.version,
|
|
438
|
+
offlineProtected: isOfflineProtected(entry.customMetadata)
|
|
439
|
+
}));
|
|
440
|
+
const staleKeys = await removeIfUnchanged(batch);
|
|
441
|
+
if (staleKeys === null || idleContext.isCanceled() || !shouldContinue()) return {
|
|
442
|
+
completed: false,
|
|
443
|
+
hasStaleEntries
|
|
444
|
+
};
|
|
445
|
+
if (staleKeys.size > 0) hasStaleEntries = true;
|
|
446
|
+
onRemoved(filterAndMap(batch, ({ key }) => staleKeys.has(key) ? false : key));
|
|
447
|
+
}
|
|
448
|
+
return {
|
|
449
|
+
completed: true,
|
|
450
|
+
hasStaleEntries
|
|
451
|
+
};
|
|
452
|
+
}
|
|
413
453
|
function keepEntriesWithinByteBudget(entries, getKey, getLastAccessAt, getSizeBytes, isPinned, isProtected, maxBytes) {
|
|
414
454
|
const keptKeys = /* @__PURE__ */ new Set();
|
|
415
455
|
let unprotectedBytes = 0;
|
|
@@ -458,7 +498,7 @@ function keepEntriesWithinByteBudget(entries, getKey, getLastAccessAt, getSizeBy
|
|
|
458
498
|
unprotectedBytes
|
|
459
499
|
};
|
|
460
500
|
}
|
|
461
|
-
/** Deadline-aware variant used by
|
|
501
|
+
/** Deadline-aware variant used by background storage maintenance. */
|
|
462
502
|
async function keepEntriesWithinByteBudgetDuringIdle(entries, getKey, getLastAccessAt, getSizeBytes, isPinned, isProtected, maxBytes, idleContext) {
|
|
463
503
|
const keptKeys = /* @__PURE__ */ new Set();
|
|
464
504
|
let unprotectedBytes = 0;
|
|
@@ -670,12 +710,12 @@ function getRecord(value) {
|
|
|
670
710
|
if (!isObject(value)) return null;
|
|
671
711
|
return value;
|
|
672
712
|
}
|
|
673
|
-
function
|
|
713
|
+
function isAsyncStorageMetadataOfflineProtected(customMetadata) {
|
|
674
714
|
return customMetadata?.o === true;
|
|
675
715
|
}
|
|
676
716
|
function getProtectedKeysFromMetadata(entries) {
|
|
677
717
|
const keys = /* @__PURE__ */ new Set();
|
|
678
|
-
for (const entry of entries) if (
|
|
718
|
+
for (const entry of entries) if (isAsyncStorageMetadataOfflineProtected(entry.customMetadata)) keys.add(entry.key);
|
|
679
719
|
return keys;
|
|
680
720
|
}
|
|
681
721
|
function isSessionOfflineInLocalStorage(sessionKey) {
|
|
@@ -703,8 +743,8 @@ function mergeManagedAsyncStorageCustomMetadata(currentCustomMetadata, nextCusto
|
|
|
703
743
|
if (protectedKeysSnapshotSet !== null) return setOfflineProtectionMetadata(mergedCustomMetadata, protectedKeysSnapshotSet?.has(serializeProtectedRef({
|
|
704
744
|
...scope,
|
|
705
745
|
key
|
|
706
|
-
})) === true ||
|
|
707
|
-
return setOfflineProtectionMetadata(mergedCustomMetadata,
|
|
746
|
+
})) === true || isAsyncStorageMetadataOfflineProtected(nextCustomMetadata) || isAsyncStorageMetadataOfflineProtected(currentCustomMetadata));
|
|
747
|
+
return setOfflineProtectionMetadata(mergedCustomMetadata, isAsyncStorageMetadataOfflineProtected(nextCustomMetadata) || isAsyncStorageMetadataOfflineProtected(currentCustomMetadata));
|
|
708
748
|
}
|
|
709
749
|
const ASYNC_METADATA_LAST_ACCESS_AT_KEY = "a";
|
|
710
750
|
const ASYNC_METADATA_VERSION_KEY = "v";
|
|
@@ -1033,8 +1073,8 @@ var ManagedAsyncStorageNamespaceHandle = class {
|
|
|
1033
1073
|
})) }).catch(() => {});
|
|
1034
1074
|
return keys.map((key) => entryByKey.get(key) ?? null);
|
|
1035
1075
|
}
|
|
1036
|
-
commit(args) {
|
|
1037
|
-
|
|
1076
|
+
async commit(args) {
|
|
1077
|
+
await this.#adapter.queueCommitToNamespace(this.#scope, args);
|
|
1038
1078
|
}
|
|
1039
1079
|
async listKeys() {
|
|
1040
1080
|
await this.#adapter.flushPendingNamespaceCommit(this.#scope);
|
|
@@ -1112,7 +1152,7 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
1112
1152
|
await this.#runWithSessionWriterLock(sessionKey, async () => {
|
|
1113
1153
|
await this.#flushPendingNamespaceCommitsForSessionUnlocked(sessionKey);
|
|
1114
1154
|
const scopesToClear = await this.#listDiscoveredScopes(sessionKey);
|
|
1115
|
-
await Promise.all(scopesToClear.map((scope) => this.#clearManagedNamespaceUnlocked(scope)));
|
|
1155
|
+
await Promise.all(scopesToClear.map((scope) => this.#clearManagedNamespaceUnlocked(scope, false)));
|
|
1116
1156
|
});
|
|
1117
1157
|
}
|
|
1118
1158
|
async readProtectedStorageKeys(sessionKey, driver = this.#driver) {
|
|
@@ -1127,7 +1167,7 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
1127
1167
|
order: "key"
|
|
1128
1168
|
});
|
|
1129
1169
|
const refs = [];
|
|
1130
|
-
for (const entry of metadataEntries) if (
|
|
1170
|
+
for (const entry of metadataEntries) if (isAsyncStorageMetadataOfflineProtected(entry.customMetadata)) refs.push(serializeProtectedRef({
|
|
1131
1171
|
...scope,
|
|
1132
1172
|
key: entry.key
|
|
1133
1173
|
}));
|
|
@@ -1178,7 +1218,7 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
1178
1218
|
for (const [key, shouldProtect] of protectionByKey.entries()) {
|
|
1179
1219
|
const existingMetadata = indexEntries.get(key);
|
|
1180
1220
|
if (existingMetadata === void 0) continue;
|
|
1181
|
-
if (
|
|
1221
|
+
if (isAsyncStorageMetadataOfflineProtected(existingMetadata.customMetadata) === shouldProtect) continue;
|
|
1182
1222
|
const nextCustomMetadata = setOfflineProtectionMetadata(existingMetadata.customMetadata, shouldProtect);
|
|
1183
1223
|
const nextMetadata = { ...existingMetadata };
|
|
1184
1224
|
if (nextCustomMetadata !== void 0) nextMetadata.customMetadata = nextCustomMetadata;
|
|
@@ -1221,23 +1261,37 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
1221
1261
|
}
|
|
1222
1262
|
#hasPendingBarrierForKeys(pending, keys) {
|
|
1223
1263
|
return keys.some((key) => {
|
|
1224
|
-
return pending.upserts.has(key) || pending.removes.has(key) || pending.inFlightBarrierKeys?.has(key) === true;
|
|
1264
|
+
return pending.upserts.has(key) || pending.removes.has(key) || pending.conditionalRemoves.has(key) || pending.inFlightBarrierKeys?.has(key) === true;
|
|
1225
1265
|
});
|
|
1226
1266
|
}
|
|
1267
|
+
/** Resolves with the `conditionalRemoves` keys kept because they changed. */
|
|
1227
1268
|
async queueCommitToNamespace(scope, args) {
|
|
1228
1269
|
const pending = this.#getOrCreatePendingNamespaceCommit(scope);
|
|
1229
1270
|
const now = Date.now();
|
|
1230
|
-
for (const touch of args.touches ?? [])
|
|
1271
|
+
for (const touch of args.touches ?? []) {
|
|
1272
|
+
this.#cancelPendingConditionalRemove(pending, touch.key);
|
|
1273
|
+
pending.touches.set(touch.key, touch.lastAccessAt ?? now);
|
|
1274
|
+
}
|
|
1231
1275
|
for (const remove of args.removes ?? []) {
|
|
1232
1276
|
pending.upserts.delete(remove);
|
|
1233
1277
|
pending.touches.delete(remove);
|
|
1278
|
+
pending.conditionalRemoves.delete(remove);
|
|
1234
1279
|
pending.removes.add(remove);
|
|
1235
1280
|
}
|
|
1281
|
+
for (const remove of args.conditionalRemoves ?? []) {
|
|
1282
|
+
if (pending.upserts.has(remove.key) || pending.touches.has(remove.key)) {
|
|
1283
|
+
pending.staleConditionalRemoveKeys.add(remove.key);
|
|
1284
|
+
continue;
|
|
1285
|
+
}
|
|
1286
|
+
if (pending.removes.has(remove.key)) continue;
|
|
1287
|
+
pending.conditionalRemoves.set(remove.key, remove);
|
|
1288
|
+
}
|
|
1236
1289
|
if ("staticPolicy" in args) {
|
|
1237
1290
|
pending.hasStaticPolicyUpdate = true;
|
|
1238
1291
|
pending.staticPolicy = args.staticPolicy ?? null;
|
|
1239
1292
|
}
|
|
1240
1293
|
for (const upsert of args.upserts ?? []) {
|
|
1294
|
+
this.#cancelPendingConditionalRemove(pending, upsert.key);
|
|
1241
1295
|
pending.removes.delete(upsert.key);
|
|
1242
1296
|
pending.upserts.set(upsert.key, __LEGIT_CAST__(upsert));
|
|
1243
1297
|
}
|
|
@@ -1249,6 +1303,19 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
1249
1303
|
});
|
|
1250
1304
|
});
|
|
1251
1305
|
}
|
|
1306
|
+
/** A later write wins over a queued conditional remove of the same key. */
|
|
1307
|
+
#cancelPendingConditionalRemove(pending, key) {
|
|
1308
|
+
if (pending.conditionalRemoves.delete(key)) pending.staleConditionalRemoveKeys.add(key);
|
|
1309
|
+
}
|
|
1310
|
+
getMaintenanceRemoveBatchSize() {
|
|
1311
|
+
return getManagedDriverCapabilities(this.#driver)?.applyManagedCommit !== void 0 ? PER_KEY_COMMIT_MAINTENANCE_REMOVE_BATCH_SIZE : INDEX_REWRITE_COMMIT_MAINTENANCE_REMOVE_BATCH_SIZE;
|
|
1312
|
+
}
|
|
1313
|
+
async commitToNamespaceNow(scope, args) {
|
|
1314
|
+
const committed = this.queueCommitToNamespace(scope, args);
|
|
1315
|
+
const flushed = this.flushPendingNamespaceCommit(scope).then(() => this.flushPendingNamespaceCommit(scope));
|
|
1316
|
+
const [staleConditionalRemoveKeys] = await Promise.all([committed, flushed]);
|
|
1317
|
+
return staleConditionalRemoveKeys;
|
|
1318
|
+
}
|
|
1252
1319
|
async flushPendingNamespaceCommit(scope, keys) {
|
|
1253
1320
|
const namespaceKey = getNamespaceId(scope);
|
|
1254
1321
|
const pending = this.#pendingNamespaceCommits.get(namespaceKey);
|
|
@@ -1271,37 +1338,47 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
1271
1338
|
pending.cancelFlush = null;
|
|
1272
1339
|
const upserts = [...pending.upserts.values()];
|
|
1273
1340
|
const removes = [...pending.removes];
|
|
1341
|
+
const conditionalRemoves = [...pending.conditionalRemoves.values()];
|
|
1342
|
+
const staleConditionalRemoveKeys = pending.staleConditionalRemoveKeys;
|
|
1274
1343
|
const hasStaticPolicyUpdate = pending.hasStaticPolicyUpdate;
|
|
1275
1344
|
const staticPolicy = pending.staticPolicy;
|
|
1276
1345
|
const touches = [...pending.touches.entries()].map(([key, lastAccessAt]) => ({
|
|
1277
1346
|
key,
|
|
1278
1347
|
lastAccessAt
|
|
1279
1348
|
}));
|
|
1280
|
-
const inFlightBarrierKeys = new Set([
|
|
1349
|
+
const inFlightBarrierKeys = new Set([
|
|
1350
|
+
...pending.upserts.keys(),
|
|
1351
|
+
...pending.removes,
|
|
1352
|
+
...pending.conditionalRemoves.keys()
|
|
1353
|
+
]);
|
|
1281
1354
|
const waiters = [...pending.waiters];
|
|
1282
1355
|
pending.hasStaticPolicyUpdate = false;
|
|
1283
1356
|
pending.upserts = /* @__PURE__ */ new Map();
|
|
1284
1357
|
pending.removes = /* @__PURE__ */ new Set();
|
|
1358
|
+
pending.conditionalRemoves = /* @__PURE__ */ new Map();
|
|
1359
|
+
pending.staleConditionalRemoveKeys = /* @__PURE__ */ new Set();
|
|
1285
1360
|
pending.staticPolicy = null;
|
|
1286
1361
|
pending.touches = /* @__PURE__ */ new Map();
|
|
1287
1362
|
pending.waiters = [];
|
|
1288
1363
|
pending.inFlightBarrierKeys = inFlightBarrierKeys.size > 0 ? inFlightBarrierKeys : null;
|
|
1289
|
-
if (upserts.length === 0 && removes.length === 0 && touches.length === 0 && !hasStaticPolicyUpdate) {
|
|
1364
|
+
if (upserts.length === 0 && removes.length === 0 && conditionalRemoves.length === 0 && touches.length === 0 && !hasStaticPolicyUpdate) {
|
|
1290
1365
|
this.#pendingNamespaceCommits.delete(namespaceKey);
|
|
1291
|
-
for (const waiter of waiters) waiter.resolve();
|
|
1366
|
+
for (const waiter of waiters) waiter.resolve(staleConditionalRemoveKeys);
|
|
1292
1367
|
return;
|
|
1293
1368
|
}
|
|
1294
1369
|
pending.flushPromise = this.#applyManagedCommit(scope, {
|
|
1295
1370
|
upserts,
|
|
1296
1371
|
removes,
|
|
1372
|
+
...conditionalRemoves.length > 0 ? { conditionalRemoves } : {},
|
|
1297
1373
|
...hasStaticPolicyUpdate ? { staticPolicy } : {},
|
|
1298
1374
|
touches
|
|
1299
|
-
}).then(() => {
|
|
1375
|
+
}).then((committedStaleKeys) => {
|
|
1376
|
+
for (const key of committedStaleKeys) staleConditionalRemoveKeys.add(key);
|
|
1300
1377
|
const currentBucket = getBucketId(Date.now());
|
|
1301
1378
|
for (const touch of touches) this.#recentTouchedBuckets.set(`${namespaceKey}::${touch.key}`, getBucketId(touch.lastAccessAt));
|
|
1302
1379
|
for (const upsert of upserts) this.#recentTouchedBuckets.set(`${namespaceKey}::${upsert.key}`, currentBucket);
|
|
1303
1380
|
this.#pruneRecentTouchedBuckets(currentBucket);
|
|
1304
|
-
for (const waiter of waiters) waiter.resolve();
|
|
1381
|
+
for (const waiter of waiters) waiter.resolve(staleConditionalRemoveKeys);
|
|
1305
1382
|
}).catch((error) => {
|
|
1306
1383
|
for (const waiter of waiters) waiter.reject(error);
|
|
1307
1384
|
throw error;
|
|
@@ -1444,10 +1521,16 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
1444
1521
|
}
|
|
1445
1522
|
async clearManagedNamespace(scope) {
|
|
1446
1523
|
await this.#runWithSessionWriterLock(scope.sessionKey, async () => {
|
|
1447
|
-
await this.#clearManagedNamespaceUnlocked(scope);
|
|
1524
|
+
await this.#clearManagedNamespaceUnlocked(scope, true);
|
|
1448
1525
|
});
|
|
1449
1526
|
}
|
|
1450
|
-
|
|
1527
|
+
/**
|
|
1528
|
+
* Commits still pending here were queued after the clear was requested,
|
|
1529
|
+
* because both clear paths flush first. A namespace clear keeps them so
|
|
1530
|
+
* they flush once the writer lock is released; a session clear discards
|
|
1531
|
+
* them with the session data.
|
|
1532
|
+
*/
|
|
1533
|
+
async #clearManagedNamespaceUnlocked(scope, keepPendingCommits) {
|
|
1451
1534
|
const managedCapabilities = getManagedDriverCapabilities(this.#driver);
|
|
1452
1535
|
if (managedCapabilities?.clearManagedNamespace !== void 0) await managedCapabilities.clearManagedNamespace(scope);
|
|
1453
1536
|
else await this.#driver.clear(scope);
|
|
@@ -1455,7 +1538,13 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
1455
1538
|
this.#broadcastNamespaceInvalidation(scope, "clear");
|
|
1456
1539
|
const namespaceKey = getNamespaceId(scope);
|
|
1457
1540
|
this.#observedScopes.delete(namespaceKey);
|
|
1458
|
-
this.#pendingNamespaceCommits.
|
|
1541
|
+
const pending = this.#pendingNamespaceCommits.get(namespaceKey);
|
|
1542
|
+
if (pending && keepPendingCommits) this.#schedulePendingNamespaceFlush(pending);
|
|
1543
|
+
else if (pending) {
|
|
1544
|
+
this.#pendingNamespaceCommits.delete(namespaceKey);
|
|
1545
|
+
pending.cancelFlush?.();
|
|
1546
|
+
for (const waiter of pending.waiters) waiter.resolve(pending.staleConditionalRemoveKeys);
|
|
1547
|
+
}
|
|
1459
1548
|
this.clearRecentTouchedBucketsForNamespace(scope);
|
|
1460
1549
|
}
|
|
1461
1550
|
#toPublicMetadata(key, metadata) {
|
|
@@ -1469,23 +1558,38 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
1469
1558
|
customMetadata: metadata.customMetadata ?? {}
|
|
1470
1559
|
};
|
|
1471
1560
|
}
|
|
1561
|
+
/** Resolves with the `conditionalRemoves` keys kept because they changed. */
|
|
1472
1562
|
async #applyManagedCommit(scope, args) {
|
|
1473
1563
|
const upserts = args.upserts ?? [];
|
|
1474
1564
|
const removes = [...new Set(args.removes ?? [])];
|
|
1565
|
+
const conditionalRemoves = args.conditionalRemoves ?? [];
|
|
1475
1566
|
const touches = args.touches ?? [];
|
|
1476
1567
|
const touchedKeys = [...new Set(touches.map((touch) => touch.key))];
|
|
1477
1568
|
const managedCapabilities = getManagedDriverCapabilities(this.#driver);
|
|
1478
1569
|
if (managedCapabilities?.applyManagedCommit !== void 0) {
|
|
1479
|
-
await managedCapabilities.applyManagedCommit(scope, args, { mergeOfflineProtectionMetadata: (key, nextCustomMetadata, currentCustomMetadata) => mergeManagedAsyncStorageCustomMetadata(currentCustomMetadata, nextCustomMetadata, getSessionProtectedKeysSnapshot(scope.sessionKey), scope, key) });
|
|
1570
|
+
const reportedStaleKeys = await managedCapabilities.applyManagedCommit(scope, args, { mergeOfflineProtectionMetadata: (key, nextCustomMetadata, currentCustomMetadata) => mergeManagedAsyncStorageCustomMetadata(currentCustomMetadata, nextCustomMetadata, getSessionProtectedKeysSnapshot(scope.sessionKey), scope, key) });
|
|
1480
1571
|
this.#advanceNamespaceReadCacheGeneration(scope);
|
|
1481
1572
|
this.#invalidateCachedNamespaceIndexState(scope);
|
|
1482
1573
|
for (const key of removes) this.#invalidateCachedPayloadValue(scope, key);
|
|
1574
|
+
for (const { key } of conditionalRemoves) this.#invalidateCachedPayloadValue(scope, key);
|
|
1483
1575
|
for (const upsert of upserts) this.#setCachedPayloadValue(scope, upsert.key, { value: upsert.value });
|
|
1484
1576
|
this.#broadcastNamespaceInvalidation(scope, "commit");
|
|
1485
|
-
return;
|
|
1577
|
+
return reportedStaleKeys ?? new Set(conditionalRemoves.map((remove) => remove.key));
|
|
1486
1578
|
}
|
|
1579
|
+
if (conditionalRemoves.length > 0) this.#invalidateCachedNamespaceIndexState(scope);
|
|
1487
1580
|
const currentIndexState = await this.#readNamespaceIndexStateUsingDriver(this.#driver, scope);
|
|
1488
1581
|
const indexEntries = currentIndexState.valid && currentIndexState.entries !== null ? currentIndexState.entries : /* @__PURE__ */ new Map();
|
|
1582
|
+
const staleConditionalRemoveKeys = /* @__PURE__ */ new Set();
|
|
1583
|
+
for (const remove of conditionalRemoves) {
|
|
1584
|
+
if (!currentIndexState.valid) {
|
|
1585
|
+
staleConditionalRemoveKeys.add(remove.key);
|
|
1586
|
+
continue;
|
|
1587
|
+
}
|
|
1588
|
+
const metadata = indexEntries.get(remove.key);
|
|
1589
|
+
if (metadata === void 0) continue;
|
|
1590
|
+
if (matchesConditionalRemove(remove, metadata.lastAccessAt, metadata.sizeBytes, metadata.version, isAsyncStorageMetadataOfflineProtected(metadata.customMetadata))) removes.push(remove.key);
|
|
1591
|
+
else staleConditionalRemoveKeys.add(remove.key);
|
|
1592
|
+
}
|
|
1489
1593
|
const now = Date.now();
|
|
1490
1594
|
const touchesByKey = new Map(touches.map((touch) => [touch.key, touch.lastAccessAt ?? now]));
|
|
1491
1595
|
const setEntries = [];
|
|
@@ -1553,6 +1657,7 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
1553
1657
|
valid: currentIndexState.valid
|
|
1554
1658
|
});
|
|
1555
1659
|
this.#broadcastNamespaceInvalidation(scope, "commit");
|
|
1660
|
+
return staleConditionalRemoveKeys;
|
|
1556
1661
|
}
|
|
1557
1662
|
async #removeManagedKeysUsingDriver(driver, scope, keys) {
|
|
1558
1663
|
const uniqueKeys = [...new Set(keys)];
|
|
@@ -1702,10 +1807,12 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
1702
1807
|
const created = {
|
|
1703
1808
|
scope,
|
|
1704
1809
|
cancelFlush: null,
|
|
1810
|
+
conditionalRemoves: /* @__PURE__ */ new Map(),
|
|
1705
1811
|
inFlightBarrierKeys: null,
|
|
1706
1812
|
flushPromise: null,
|
|
1707
1813
|
hasStaticPolicyUpdate: false,
|
|
1708
1814
|
removes: /* @__PURE__ */ new Set(),
|
|
1815
|
+
staleConditionalRemoveKeys: /* @__PURE__ */ new Set(),
|
|
1709
1816
|
staticPolicy: null,
|
|
1710
1817
|
touches: /* @__PURE__ */ new Map(),
|
|
1711
1818
|
upserts: /* @__PURE__ */ new Map(),
|
|
@@ -2002,7 +2109,7 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
2002
2109
|
const payloadRecordKeysToRemove = new Set(rawKeysToRemove);
|
|
2003
2110
|
const candidateEntries = [];
|
|
2004
2111
|
for (const [key, metadata] of indexState.entries.entries()) {
|
|
2005
|
-
const isProtected =
|
|
2112
|
+
const isProtected = isAsyncStorageMetadataOfflineProtected(metadata.customMetadata) || args.protectedRefs?.has(serializeProtectedRef({
|
|
2006
2113
|
...args.scope,
|
|
2007
2114
|
key
|
|
2008
2115
|
})) === true;
|
|
@@ -2107,8 +2214,41 @@ var ManagedAsyncStorageAdapter = class {
|
|
|
2107
2214
|
function createAsyncStorageAdapter(driver) {
|
|
2108
2215
|
return new ManagedAsyncStorageAdapter(driver);
|
|
2109
2216
|
}
|
|
2217
|
+
/**
|
|
2218
|
+
* Drivers with a per-key managed commit (IndexedDB) keep each maintenance
|
|
2219
|
+
* transaction short. Drivers without one rewrite the whole namespace index on
|
|
2220
|
+
* every commit, so larger batches avoid repeating that work for every few
|
|
2221
|
+
* removals while still bounding concurrent driver requests.
|
|
2222
|
+
*/
|
|
2223
|
+
const PER_KEY_COMMIT_MAINTENANCE_REMOVE_BATCH_SIZE = 25;
|
|
2224
|
+
const INDEX_REWRITE_COMMIT_MAINTENANCE_REMOVE_BATCH_SIZE = 250;
|
|
2225
|
+
/** Number of keys idle-deadline maintenance removes per commit. */
|
|
2226
|
+
function getAsyncStorageMaintenanceRemoveBatchSize(adapter) {
|
|
2227
|
+
return adapter instanceof ManagedAsyncStorageAdapter ? adapter.getMaintenanceRemoveBatchSize() : INDEX_REWRITE_COMMIT_MAINTENANCE_REMOVE_BATCH_SIZE;
|
|
2228
|
+
}
|
|
2229
|
+
/**
|
|
2230
|
+
* Removes entries only while they still have the metadata the caller read,
|
|
2231
|
+
* without waiting for the namespace batching debounce. Resolves with the keys
|
|
2232
|
+
* kept because they changed. Managed adapters check and remove atomically;
|
|
2233
|
+
* other adapters only get a best-effort metadata re-read before removing.
|
|
2234
|
+
*/
|
|
2235
|
+
async function removeAsyncStorageNamespaceEntriesIfUnchanged(adapter, scope, entries) {
|
|
2236
|
+
if (adapter instanceof ManagedAsyncStorageAdapter) return adapter.commitToNamespaceNow(scope, { conditionalRemoves: entries });
|
|
2237
|
+
const namespace = adapter.openNamespace(scope);
|
|
2238
|
+
const currentEntries = new Map((await namespace.listMetadata()).map((entry) => [entry.key, entry]));
|
|
2239
|
+
const removes = [];
|
|
2240
|
+
const staleKeys = /* @__PURE__ */ new Set();
|
|
2241
|
+
for (const remove of entries) {
|
|
2242
|
+
const current = currentEntries.get(remove.key);
|
|
2243
|
+
if (current === void 0) continue;
|
|
2244
|
+
if (matchesConditionalRemove(remove, current.lastAccessAt, current.sizeBytes, current.version, isAsyncStorageMetadataOfflineProtected(current.customMetadata))) removes.push(remove.key);
|
|
2245
|
+
else staleKeys.add(remove.key);
|
|
2246
|
+
}
|
|
2247
|
+
if (removes.length > 0) await namespace.commit({ removes });
|
|
2248
|
+
return staleKeys;
|
|
2249
|
+
}
|
|
2110
2250
|
function serializeProtectedRef(ref) {
|
|
2111
2251
|
return serializeProtectedRef$1(ref);
|
|
2112
2252
|
}
|
|
2113
2253
|
//#endregion
|
|
2114
|
-
export {
|
|
2254
|
+
export { parseAsyncStorageRecordKey as $, serializeJsonForStorage as A, getSessionProtectedKeysSnapshot as B, createShouldIgnoreItemPredicate as C, keepEntriesWithinByteBudget as D, isQuotaExceededError as E, isModeEffectivelyActive as F, parseCompactLocalStorageEntry as G, getNavigatorLockManager as H, isOfflineModeStatusValue as I, getNamespaceId as J, compareMetadata as K, offlineItemEntityRefSchema as L, compactOfflineStatusSnapshotSchema as M, defaultOfflineRuntimeConfig as N, keepEntriesWithinByteBudgetDuringIdle as O, getIsOfflineModeFromStatus as P, normalizeStaticPolicy as Q, offlineResolutionRecordSchema as R, getDefaultMaxBytesForScope as S, getSerializedStringSize as T, warnIfNavigatorLockUnavailable as U, setSessionProtectedKeysSnapshot as V, createCompactLocalStorageEntry as W, getPersistedNamespaceId as X, getPayloadRecordKey as Y, matchesConditionalRemove as Z, getOfflineSessionStatusStorageKey as _, estimateManagedAsyncStorageEntrySizeBytes as a, getTSDFDebugTimingMs as b, isAsyncStorageMetadataOfflineProtected as c, registerAsyncStartupStoreCleanup as d, parsePersistedAsyncNamespaceKind as et, removeAsyncStorageNamespaceEntriesIfUnchanged as f, OFFLINE_SESSION_STATUS_STORE_NAME as g, parseAsyncStorageNamespaceKind as h, createAsyncStorageAdapter as i, scheduleIdleCleanup as it, OfflineResolutionConflictParseError as j, removeAsyncMaintenanceEntriesIfUnchanged as k, mergeManagedAsyncStorageCustomMetadata as l, unregisterAsyncStartupStoreCleanup as m, ASYNC_STORAGE_MAX_AGE_MS as n, INITIAL_MAINTENANCE_CLEANUP_DELAY_MS as nt, getAsyncStorageMaintenanceRemoveBatchSize as o, serializeProtectedRef as p, encodePersistedAsyncNamespaceKind as q, buildPersistedStaticPolicy as r, createIdleCleanupContext as rt, getProtectedKeysFromMetadata as s, ASYNC_MAINTENANCE_LOCAL_STORAGE_KEY as t, serializeProtectedRef$1 as tt, readAsyncStorageNamespaceIndexStateUsingDriver as u, logPersistentStorageQuotaCleanup as v, createTimedKeySet as w, resolveTSDFLogger as x, emitTSDFDebugLog as y, clearSessionProtectedKeysSnapshot as z };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { t as opfsPersistentStorage } from "./opfsPersistentStorage-
|
|
2
|
-
import { n as indexedDbPersistentStorage } from "./indexedDbPersistentStorage-
|
|
1
|
+
import { t as opfsPersistentStorage } from "./opfsPersistentStorage-JsSCNBTr.js";
|
|
2
|
+
import { n as indexedDbPersistentStorage } from "./indexedDbPersistentStorage-BameJ4st.js";
|
|
3
3
|
export { indexedDbPersistentStorage, opfsPersistentStorage };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as AsyncStorageAdapter } from "./types-
|
|
1
|
+
import { t as AsyncStorageAdapter } from "./types-BlyGC_PO.js";
|
|
2
2
|
//#region src/persistentStorage/indexedDbAsyncStorageAdapter.d.ts
|
|
3
3
|
/** Options for creating the IndexedDB persistent storage adapter. */
|
|
4
4
|
type IndexedDbPersistentStorageOptions$1 = {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as indexedDbPersistentStorage, t as createIndexedDbPersistentStorage } from "./indexedDbPersistentStorage-
|
|
1
|
+
import { n as indexedDbPersistentStorage, t as createIndexedDbPersistentStorage } from "./indexedDbPersistentStorage-BameJ4st.js";
|
|
2
2
|
export { createIndexedDbPersistentStorage, indexedDbPersistentStorage };
|
package/dist/{indexedDbPersistentStorage-BSlgLveB.js → indexedDbPersistentStorage-BameJ4st.js}
RENAMED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { $ as parseAsyncStorageRecordKey, A as serializeJsonForStorage, J as getNamespaceId, K as compareMetadata, Q as normalizeStaticPolicy, X as getPersistedNamespaceId, Y as getPayloadRecordKey, Z as matchesConditionalRemove, a as estimateManagedAsyncStorageEntrySizeBytes, b as getTSDFDebugTimingMs, et as parsePersistedAsyncNamespaceKind, i as createAsyncStorageAdapter, q as encodePersistedAsyncNamespaceKind, tt as serializeProtectedRef, y as emitTSDFDebugLog } from "./asyncStorageAdapter-URS9eulL.js";
|
|
2
2
|
import { __LEGIT_CAST__ } from "@ls-stack/utils/saferTyping";
|
|
3
3
|
import { isObject } from "@ls-stack/utils/typeGuards";
|
|
4
4
|
//#region src/persistentStorage/indexedDbAsyncStorageAdapter.ts
|
|
@@ -508,10 +508,12 @@ var IndexedDbAsyncStorageDriver = class {
|
|
|
508
508
|
const policyStore = transaction.objectStore(INDEXED_DB_NAMESPACE_POLICY_STORE);
|
|
509
509
|
const upserts = args.upserts ?? [];
|
|
510
510
|
const removes = [...new Set(args.removes ?? [])];
|
|
511
|
+
const conditionalRemoves = args.conditionalRemoves ?? [];
|
|
511
512
|
const touches = args.touches ?? [];
|
|
512
513
|
const scopeId = createScopeId(scope.sessionKey, scope.storeName, scope.kind);
|
|
513
514
|
const uniqueKeys = [...new Set([
|
|
514
515
|
...removes,
|
|
516
|
+
...conditionalRemoves.map((remove) => remove.key),
|
|
515
517
|
...touches.map((touch) => touch.key),
|
|
516
518
|
...upserts.map((upsert) => upsert.key)
|
|
517
519
|
])];
|
|
@@ -524,6 +526,13 @@ var IndexedDbAsyncStorageDriver = class {
|
|
|
524
526
|
const record = await openRequestAsPromise(entryStore.get(createEntryPrimaryKey(scope, key)));
|
|
525
527
|
if (isValidEntryRecord(record)) existingEntries.set(key, toEntryRecordValue(record));
|
|
526
528
|
}));
|
|
529
|
+
const staleConditionalRemoveKeys = /* @__PURE__ */ new Set();
|
|
530
|
+
for (const remove of conditionalRemoves) {
|
|
531
|
+
const existing = existingEntries.get(remove.key);
|
|
532
|
+
if (existing === void 0) continue;
|
|
533
|
+
if (matchesConditionalRemove(remove, existing.a, existing.z, existing.v ?? 1, existing.o === 1)) removes.push(remove.key);
|
|
534
|
+
else staleConditionalRemoveKeys.add(remove.key);
|
|
535
|
+
}
|
|
527
536
|
for (const key of removes) entryStore.delete(createEntryPrimaryKey(scope, key));
|
|
528
537
|
const upsertKeySet = new Set(upserts.map((upsert) => upsert.key));
|
|
529
538
|
for (const upsert of upserts) {
|
|
@@ -585,6 +594,7 @@ var IndexedDbAsyncStorageDriver = class {
|
|
|
585
594
|
type: "applyManagedCommit",
|
|
586
595
|
upserts: upserts.map((upsert) => upsert.key).sort((left, right) => left.localeCompare(right))
|
|
587
596
|
});
|
|
597
|
+
return staleConditionalRemoveKeys;
|
|
588
598
|
}
|
|
589
599
|
async readNamespaceIndexState(scope, knownRecordKeys_) {
|
|
590
600
|
const transaction = (await this.#getDatabase()).transaction([INDEXED_DB_ENTRY_STORE, INDEXED_DB_NAMESPACE_POLICY_STORE], "readonly");
|