@ztimson/utils 0.26.19 → 0.26.21
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/cache.d.ts +1 -0
- package/dist/index.cjs +51 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +51 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -423,8 +423,9 @@ class Cache {
|
|
|
423
423
|
__publicField(this, "store", {});
|
|
424
424
|
/** Whether cache is complete */
|
|
425
425
|
__publicField(this, "complete", false);
|
|
426
|
+
__publicField(this, "_loading");
|
|
426
427
|
/** Await initial loading */
|
|
427
|
-
__publicField(this, "loading");
|
|
428
|
+
__publicField(this, "loading", new Promise((r) => this._loading = r));
|
|
428
429
|
/**
|
|
429
430
|
* Get all cached items
|
|
430
431
|
* @return {T[]} Array of items
|
|
@@ -433,8 +434,6 @@ class Cache {
|
|
|
433
434
|
var _a, _b, _c, _d;
|
|
434
435
|
this.key = key;
|
|
435
436
|
this.options = options;
|
|
436
|
-
let done;
|
|
437
|
-
this.loading = new Promise((r) => done = r);
|
|
438
437
|
if (this.options.persistentStorage != null) {
|
|
439
438
|
if (typeof this.options.persistentStorage == "string")
|
|
440
439
|
this.options.persistentStorage = { storage: localStorage, key: this.options.persistentStorage };
|
|
@@ -444,7 +443,7 @@ class Cache {
|
|
|
444
443
|
const table = await persists.storage.createTable({ name: persists.key, key: this.key });
|
|
445
444
|
const rows = await table.getAll();
|
|
446
445
|
Object.assign(this.store, rows.reduce((acc, row) => ({ ...acc, [this.getKey(row)]: row }), {}));
|
|
447
|
-
|
|
446
|
+
this._loading();
|
|
448
447
|
})();
|
|
449
448
|
} else if (((_d = (_c = this.options.persistentStorage) == null ? void 0 : _c.storage) == null ? void 0 : _d.getItem) != void 0) {
|
|
450
449
|
const stored = this.options.persistentStorage.storage.getItem(this.options.persistentStorage.key);
|
|
@@ -452,8 +451,10 @@ class Cache {
|
|
|
452
451
|
Object.assign(this.store, JSON.parse(stored));
|
|
453
452
|
} catch {
|
|
454
453
|
}
|
|
455
|
-
|
|
454
|
+
this._loading();
|
|
456
455
|
}
|
|
456
|
+
} else {
|
|
457
|
+
this._loading();
|
|
457
458
|
}
|
|
458
459
|
return new Proxy(this, {
|
|
459
460
|
get: (target, prop) => {
|
|
@@ -1032,20 +1033,36 @@ class Database {
|
|
|
1032
1033
|
this.database = database;
|
|
1033
1034
|
this.version = version;
|
|
1034
1035
|
this.connection = new Promise((resolve, reject) => {
|
|
1035
|
-
|
|
1036
|
+
let req;
|
|
1037
|
+
try {
|
|
1038
|
+
req = indexedDB.open(this.database, this.version);
|
|
1039
|
+
} catch (err) {
|
|
1040
|
+
return reject(err);
|
|
1041
|
+
}
|
|
1036
1042
|
this.tables = !tables ? [] : tables.map((t) => {
|
|
1037
1043
|
t = typeof t == "object" ? t : { name: t };
|
|
1038
1044
|
return { ...t, name: t.name.toString() };
|
|
1039
1045
|
});
|
|
1040
1046
|
req.onerror = () => reject(req.error);
|
|
1041
1047
|
req.onsuccess = () => {
|
|
1042
|
-
|
|
1048
|
+
let db;
|
|
1049
|
+
try {
|
|
1050
|
+
db = req.result;
|
|
1051
|
+
} catch (err) {
|
|
1052
|
+
return reject(err);
|
|
1053
|
+
}
|
|
1043
1054
|
const existing = Array.from(db.objectStoreNames);
|
|
1044
|
-
if (!tables)
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1055
|
+
if (!tables) {
|
|
1056
|
+
this.tables = existing.map((t) => {
|
|
1057
|
+
try {
|
|
1058
|
+
const tx = db.transaction(t, "readonly");
|
|
1059
|
+
const store = tx.objectStore(t);
|
|
1060
|
+
return { name: t, key: store.keyPath };
|
|
1061
|
+
} catch {
|
|
1062
|
+
return { name: t };
|
|
1063
|
+
}
|
|
1064
|
+
});
|
|
1065
|
+
}
|
|
1049
1066
|
const desired = new ASet((tables || []).map((t) => typeof t == "string" ? t : t.name));
|
|
1050
1067
|
if (tables && desired.symmetricDifference(new ASet(existing)).length) {
|
|
1051
1068
|
db.close();
|
|
@@ -1059,18 +1076,26 @@ class Database {
|
|
|
1059
1076
|
};
|
|
1060
1077
|
req.onupgradeneeded = () => {
|
|
1061
1078
|
this.upgrading = true;
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1079
|
+
let db;
|
|
1080
|
+
try {
|
|
1081
|
+
db = req.result;
|
|
1082
|
+
} catch {
|
|
1083
|
+
return;
|
|
1084
|
+
}
|
|
1085
|
+
try {
|
|
1086
|
+
const existingTables = new ASet(Array.from(db.objectStoreNames));
|
|
1087
|
+
if (tables) {
|
|
1088
|
+
const desired = new ASet((tables || []).map((t) => typeof t == "string" ? t : t.name));
|
|
1089
|
+
existingTables.difference(desired).forEach((name) => db.deleteObjectStore(name));
|
|
1090
|
+
desired.difference(existingTables).forEach((name) => {
|
|
1091
|
+
const t = this.tables.find(findByProp("name", name));
|
|
1092
|
+
db.createObjectStore(name, {
|
|
1093
|
+
keyPath: t == null ? void 0 : t.key,
|
|
1094
|
+
autoIncrement: (t == null ? void 0 : t.autoIncrement) || !(t == null ? void 0 : t.key)
|
|
1095
|
+
});
|
|
1072
1096
|
});
|
|
1073
|
-
}
|
|
1097
|
+
}
|
|
1098
|
+
} catch {
|
|
1074
1099
|
}
|
|
1075
1100
|
};
|
|
1076
1101
|
});
|
|
@@ -1085,6 +1110,7 @@ class Database {
|
|
|
1085
1110
|
if (!this.includes(table.name)) {
|
|
1086
1111
|
const newDb = new Database(this.database, [...this.tables, table], (this.version ?? 0) + 1);
|
|
1087
1112
|
conn.close();
|
|
1113
|
+
await newDb.connection;
|
|
1088
1114
|
Object.assign(this, newDb);
|
|
1089
1115
|
await this.connection;
|
|
1090
1116
|
}
|
|
@@ -1098,6 +1124,7 @@ class Database {
|
|
|
1098
1124
|
const conn = await this.connection;
|
|
1099
1125
|
const newDb = new Database(this.database, this.tables.filter((t) => t.name != table.name), (this.version ?? 0) + 1);
|
|
1100
1126
|
conn.close();
|
|
1127
|
+
await newDb.connection;
|
|
1101
1128
|
Object.assign(this, newDb);
|
|
1102
1129
|
await this.connection;
|
|
1103
1130
|
});
|
|
@@ -1126,9 +1153,8 @@ class Table {
|
|
|
1126
1153
|
await this.database.waitForUpgrade();
|
|
1127
1154
|
const db = await this.database.connection;
|
|
1128
1155
|
const tx = db.transaction(table, readonly ? "readonly" : "readwrite");
|
|
1129
|
-
const store = tx.objectStore(table);
|
|
1130
1156
|
return new Promise((resolve, reject) => {
|
|
1131
|
-
const request = fn2(
|
|
1157
|
+
const request = fn2(tx.objectStore(table));
|
|
1132
1158
|
request.onsuccess = () => resolve(request.result);
|
|
1133
1159
|
request.onerror = () => reject(request.error);
|
|
1134
1160
|
});
|