@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/cache.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export declare class Cache<K extends string | number | symbol, T> {
|
|
|
24
24
|
[key: string | number | symbol]: CachedValue<T> | any;
|
|
25
25
|
/** Whether cache is complete */
|
|
26
26
|
complete: boolean;
|
|
27
|
+
private _loading;
|
|
27
28
|
/** Await initial loading */
|
|
28
29
|
loading: Promise<void>;
|
|
29
30
|
/**
|
package/dist/index.cjs
CHANGED
|
@@ -427,8 +427,9 @@ ${opts.message || this.desc}`;
|
|
|
427
427
|
__publicField(this, "store", {});
|
|
428
428
|
/** Whether cache is complete */
|
|
429
429
|
__publicField(this, "complete", false);
|
|
430
|
+
__publicField(this, "_loading");
|
|
430
431
|
/** Await initial loading */
|
|
431
|
-
__publicField(this, "loading");
|
|
432
|
+
__publicField(this, "loading", new Promise((r) => this._loading = r));
|
|
432
433
|
/**
|
|
433
434
|
* Get all cached items
|
|
434
435
|
* @return {T[]} Array of items
|
|
@@ -437,8 +438,6 @@ ${opts.message || this.desc}`;
|
|
|
437
438
|
var _a, _b, _c, _d;
|
|
438
439
|
this.key = key;
|
|
439
440
|
this.options = options;
|
|
440
|
-
let done;
|
|
441
|
-
this.loading = new Promise((r) => done = r);
|
|
442
441
|
if (this.options.persistentStorage != null) {
|
|
443
442
|
if (typeof this.options.persistentStorage == "string")
|
|
444
443
|
this.options.persistentStorage = { storage: localStorage, key: this.options.persistentStorage };
|
|
@@ -448,7 +447,7 @@ ${opts.message || this.desc}`;
|
|
|
448
447
|
const table = await persists.storage.createTable({ name: persists.key, key: this.key });
|
|
449
448
|
const rows = await table.getAll();
|
|
450
449
|
Object.assign(this.store, rows.reduce((acc, row) => ({ ...acc, [this.getKey(row)]: row }), {}));
|
|
451
|
-
|
|
450
|
+
this._loading();
|
|
452
451
|
})();
|
|
453
452
|
} else if (((_d = (_c = this.options.persistentStorage) == null ? void 0 : _c.storage) == null ? void 0 : _d.getItem) != void 0) {
|
|
454
453
|
const stored = this.options.persistentStorage.storage.getItem(this.options.persistentStorage.key);
|
|
@@ -456,8 +455,10 @@ ${opts.message || this.desc}`;
|
|
|
456
455
|
Object.assign(this.store, JSON.parse(stored));
|
|
457
456
|
} catch {
|
|
458
457
|
}
|
|
459
|
-
|
|
458
|
+
this._loading();
|
|
460
459
|
}
|
|
460
|
+
} else {
|
|
461
|
+
this._loading();
|
|
461
462
|
}
|
|
462
463
|
return new Proxy(this, {
|
|
463
464
|
get: (target, prop) => {
|
|
@@ -1036,20 +1037,36 @@ ${opts.message || this.desc}`;
|
|
|
1036
1037
|
this.database = database;
|
|
1037
1038
|
this.version = version;
|
|
1038
1039
|
this.connection = new Promise((resolve, reject) => {
|
|
1039
|
-
|
|
1040
|
+
let req;
|
|
1041
|
+
try {
|
|
1042
|
+
req = indexedDB.open(this.database, this.version);
|
|
1043
|
+
} catch (err) {
|
|
1044
|
+
return reject(err);
|
|
1045
|
+
}
|
|
1040
1046
|
this.tables = !tables ? [] : tables.map((t) => {
|
|
1041
1047
|
t = typeof t == "object" ? t : { name: t };
|
|
1042
1048
|
return { ...t, name: t.name.toString() };
|
|
1043
1049
|
});
|
|
1044
1050
|
req.onerror = () => reject(req.error);
|
|
1045
1051
|
req.onsuccess = () => {
|
|
1046
|
-
|
|
1052
|
+
let db;
|
|
1053
|
+
try {
|
|
1054
|
+
db = req.result;
|
|
1055
|
+
} catch (err) {
|
|
1056
|
+
return reject(err);
|
|
1057
|
+
}
|
|
1047
1058
|
const existing = Array.from(db.objectStoreNames);
|
|
1048
|
-
if (!tables)
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1059
|
+
if (!tables) {
|
|
1060
|
+
this.tables = existing.map((t) => {
|
|
1061
|
+
try {
|
|
1062
|
+
const tx = db.transaction(t, "readonly");
|
|
1063
|
+
const store = tx.objectStore(t);
|
|
1064
|
+
return { name: t, key: store.keyPath };
|
|
1065
|
+
} catch {
|
|
1066
|
+
return { name: t };
|
|
1067
|
+
}
|
|
1068
|
+
});
|
|
1069
|
+
}
|
|
1053
1070
|
const desired = new ASet((tables || []).map((t) => typeof t == "string" ? t : t.name));
|
|
1054
1071
|
if (tables && desired.symmetricDifference(new ASet(existing)).length) {
|
|
1055
1072
|
db.close();
|
|
@@ -1063,18 +1080,26 @@ ${opts.message || this.desc}`;
|
|
|
1063
1080
|
};
|
|
1064
1081
|
req.onupgradeneeded = () => {
|
|
1065
1082
|
this.upgrading = true;
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1083
|
+
let db;
|
|
1084
|
+
try {
|
|
1085
|
+
db = req.result;
|
|
1086
|
+
} catch {
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
try {
|
|
1090
|
+
const existingTables = new ASet(Array.from(db.objectStoreNames));
|
|
1091
|
+
if (tables) {
|
|
1092
|
+
const desired = new ASet((tables || []).map((t) => typeof t == "string" ? t : t.name));
|
|
1093
|
+
existingTables.difference(desired).forEach((name) => db.deleteObjectStore(name));
|
|
1094
|
+
desired.difference(existingTables).forEach((name) => {
|
|
1095
|
+
const t = this.tables.find(findByProp("name", name));
|
|
1096
|
+
db.createObjectStore(name, {
|
|
1097
|
+
keyPath: t == null ? void 0 : t.key,
|
|
1098
|
+
autoIncrement: (t == null ? void 0 : t.autoIncrement) || !(t == null ? void 0 : t.key)
|
|
1099
|
+
});
|
|
1076
1100
|
});
|
|
1077
|
-
}
|
|
1101
|
+
}
|
|
1102
|
+
} catch {
|
|
1078
1103
|
}
|
|
1079
1104
|
};
|
|
1080
1105
|
});
|
|
@@ -1089,6 +1114,7 @@ ${opts.message || this.desc}`;
|
|
|
1089
1114
|
if (!this.includes(table.name)) {
|
|
1090
1115
|
const newDb = new Database(this.database, [...this.tables, table], (this.version ?? 0) + 1);
|
|
1091
1116
|
conn.close();
|
|
1117
|
+
await newDb.connection;
|
|
1092
1118
|
Object.assign(this, newDb);
|
|
1093
1119
|
await this.connection;
|
|
1094
1120
|
}
|
|
@@ -1102,6 +1128,7 @@ ${opts.message || this.desc}`;
|
|
|
1102
1128
|
const conn = await this.connection;
|
|
1103
1129
|
const newDb = new Database(this.database, this.tables.filter((t) => t.name != table.name), (this.version ?? 0) + 1);
|
|
1104
1130
|
conn.close();
|
|
1131
|
+
await newDb.connection;
|
|
1105
1132
|
Object.assign(this, newDb);
|
|
1106
1133
|
await this.connection;
|
|
1107
1134
|
});
|
|
@@ -1130,9 +1157,8 @@ ${opts.message || this.desc}`;
|
|
|
1130
1157
|
await this.database.waitForUpgrade();
|
|
1131
1158
|
const db = await this.database.connection;
|
|
1132
1159
|
const tx = db.transaction(table, readonly ? "readonly" : "readwrite");
|
|
1133
|
-
const store = tx.objectStore(table);
|
|
1134
1160
|
return new Promise((resolve, reject) => {
|
|
1135
|
-
const request = fn2(
|
|
1161
|
+
const request = fn2(tx.objectStore(table));
|
|
1136
1162
|
request.onsuccess = () => resolve(request.result);
|
|
1137
1163
|
request.onerror = () => reject(request.error);
|
|
1138
1164
|
});
|