@ztimson/utils 0.25.0 → 0.25.2
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 +2 -1
- package/dist/collection.d.ts +17 -0
- package/dist/index.cjs +76 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +76 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -392,6 +392,59 @@ function makeUnique(arr) {
|
|
|
392
392
|
function makeArray(value) {
|
|
393
393
|
return Array.isArray(value) ? value : [value];
|
|
394
394
|
}
|
|
395
|
+
class Collection {
|
|
396
|
+
constructor(db, collection, version, setup) {
|
|
397
|
+
__publicField(this, "database");
|
|
398
|
+
this.db = db;
|
|
399
|
+
this.collection = collection;
|
|
400
|
+
this.version = version;
|
|
401
|
+
this.setup = setup;
|
|
402
|
+
this.database = new Promise((resolve, reject) => {
|
|
403
|
+
const req = indexedDB.open(this.db, this.version);
|
|
404
|
+
req.onerror = () => reject(req.error);
|
|
405
|
+
req.onsuccess = () => resolve(req.result);
|
|
406
|
+
req.onupgradeneeded = (event) => {
|
|
407
|
+
const db2 = req.result;
|
|
408
|
+
if (!db2.objectStoreNames.contains(collection)) db2.createObjectStore(collection, { keyPath: void 0 });
|
|
409
|
+
if (this.setup) this.setup(db2, event);
|
|
410
|
+
};
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
async tx(collection, fn2, readonly) {
|
|
414
|
+
const db = await this.database;
|
|
415
|
+
const tx = db.transaction(collection, readonly ? "readonly" : "readwrite");
|
|
416
|
+
const store = tx.objectStore(collection);
|
|
417
|
+
return new Promise((resolve, reject) => {
|
|
418
|
+
const request = fn2(store);
|
|
419
|
+
request.onsuccess = () => resolve(request.result);
|
|
420
|
+
request.onerror = () => reject(request.error);
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
add(value, key) {
|
|
424
|
+
return this.tx(this.collection, (store) => store.add(value, key));
|
|
425
|
+
}
|
|
426
|
+
count() {
|
|
427
|
+
return this.tx(this.collection, (store) => store.count(), true);
|
|
428
|
+
}
|
|
429
|
+
put(key, value) {
|
|
430
|
+
return this.tx(this.collection, (store) => store.put(value, key));
|
|
431
|
+
}
|
|
432
|
+
getAll() {
|
|
433
|
+
return this.tx(this.collection, (store) => store.getAll(), true);
|
|
434
|
+
}
|
|
435
|
+
getAllKeys() {
|
|
436
|
+
return this.tx(this.collection, (store) => store.getAllKeys(), true);
|
|
437
|
+
}
|
|
438
|
+
get(key) {
|
|
439
|
+
return this.tx(this.collection, (store) => store.get(key), true);
|
|
440
|
+
}
|
|
441
|
+
delete(key) {
|
|
442
|
+
return this.tx(this.collection, (store) => store.delete(key));
|
|
443
|
+
}
|
|
444
|
+
clear() {
|
|
445
|
+
return this.tx(this.collection, (store) => store.clear());
|
|
446
|
+
}
|
|
447
|
+
}
|
|
395
448
|
class Cache {
|
|
396
449
|
/**
|
|
397
450
|
* Create new cache
|
|
@@ -407,14 +460,19 @@ class Cache {
|
|
|
407
460
|
* @return {T[]} Array of items
|
|
408
461
|
*/
|
|
409
462
|
__publicField(this, "values", this.all());
|
|
463
|
+
var _a;
|
|
410
464
|
this.key = key;
|
|
411
465
|
this.options = options;
|
|
412
|
-
if (options.storageKey && !options.storage && typeof Storage !== "undefined")
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
466
|
+
if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
|
|
467
|
+
if (options.storage) {
|
|
468
|
+
if (options.storage instanceof Collection) {
|
|
469
|
+
(async () => {
|
|
470
|
+
var _a2;
|
|
471
|
+
(await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => this.add(v));
|
|
472
|
+
})();
|
|
473
|
+
} else if (options.storageKey) {
|
|
474
|
+
const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
|
|
475
|
+
if (stored != null) try {
|
|
418
476
|
Object.assign(this.store, JSON.parse(stored));
|
|
419
477
|
} catch {
|
|
420
478
|
}
|
|
@@ -436,9 +494,14 @@ class Cache {
|
|
|
436
494
|
if (!this.key) throw new Error("No key defined");
|
|
437
495
|
return value[this.key];
|
|
438
496
|
}
|
|
439
|
-
save() {
|
|
440
|
-
if (this.options.
|
|
441
|
-
this.options.storage
|
|
497
|
+
save(key) {
|
|
498
|
+
if (this.options.storage) {
|
|
499
|
+
if (this.options.storage instanceof Collection) {
|
|
500
|
+
this.options.storage.put(key, this.store[key]);
|
|
501
|
+
} else if (this.options.storageKey) {
|
|
502
|
+
this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
|
|
503
|
+
}
|
|
504
|
+
}
|
|
442
505
|
}
|
|
443
506
|
/**
|
|
444
507
|
* Get all cached items
|
|
@@ -484,7 +547,7 @@ class Cache {
|
|
|
484
547
|
*/
|
|
485
548
|
delete(key) {
|
|
486
549
|
delete this.store[key];
|
|
487
|
-
this.save();
|
|
550
|
+
this.save(key);
|
|
488
551
|
return this;
|
|
489
552
|
}
|
|
490
553
|
/**
|
|
@@ -542,10 +605,10 @@ class Cache {
|
|
|
542
605
|
set(key, value, ttl = this.options.ttl) {
|
|
543
606
|
if (this.options.expiryPolicy == "keep") delete value._expired;
|
|
544
607
|
this.store[key] = value;
|
|
545
|
-
this.save();
|
|
608
|
+
this.save(key);
|
|
546
609
|
if (ttl) setTimeout(() => {
|
|
547
610
|
this.expire(key);
|
|
548
|
-
this.save();
|
|
611
|
+
this.save(key);
|
|
549
612
|
}, (ttl || 0) * 1e3);
|
|
550
613
|
return this;
|
|
551
614
|
}
|
|
@@ -1935,6 +1998,7 @@ export {
|
|
|
1935
1998
|
CliBackground,
|
|
1936
1999
|
CliEffects,
|
|
1937
2000
|
CliForeground,
|
|
2001
|
+
Collection,
|
|
1938
2002
|
CustomError,
|
|
1939
2003
|
ForbiddenError,
|
|
1940
2004
|
GatewayTimeoutError,
|