@ztimson/utils 0.25.0 → 0.25.1
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/database.d.ts +17 -0
- package/dist/index.cjs +77 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +77 -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,60 @@ 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
|
+
debugger;
|
|
431
|
+
return this.tx(this.collection, (store) => store.put(value, key));
|
|
432
|
+
}
|
|
433
|
+
getAll() {
|
|
434
|
+
return this.tx(this.collection, (store) => store.getAll(), true);
|
|
435
|
+
}
|
|
436
|
+
getAllKeys() {
|
|
437
|
+
return this.tx(this.collection, (store) => store.getAllKeys(), true);
|
|
438
|
+
}
|
|
439
|
+
get(key) {
|
|
440
|
+
return this.tx(this.collection, (store) => store.get(key), true);
|
|
441
|
+
}
|
|
442
|
+
delete(key) {
|
|
443
|
+
return this.tx(this.collection, (store) => store.delete(key));
|
|
444
|
+
}
|
|
445
|
+
clear() {
|
|
446
|
+
return this.tx(this.collection, (store) => store.clear());
|
|
447
|
+
}
|
|
448
|
+
}
|
|
395
449
|
class Cache {
|
|
396
450
|
/**
|
|
397
451
|
* Create new cache
|
|
@@ -407,14 +461,19 @@ class Cache {
|
|
|
407
461
|
* @return {T[]} Array of items
|
|
408
462
|
*/
|
|
409
463
|
__publicField(this, "values", this.all());
|
|
464
|
+
var _a;
|
|
410
465
|
this.key = key;
|
|
411
466
|
this.options = options;
|
|
412
|
-
if (options.storageKey && !options.storage && typeof Storage !== "undefined")
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
467
|
+
if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
|
|
468
|
+
if (options.storage) {
|
|
469
|
+
if (options.storage instanceof Collection) {
|
|
470
|
+
(async () => {
|
|
471
|
+
var _a2;
|
|
472
|
+
(await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => this.add(v));
|
|
473
|
+
})();
|
|
474
|
+
} else if (options.storageKey) {
|
|
475
|
+
const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
|
|
476
|
+
if (stored != null) try {
|
|
418
477
|
Object.assign(this.store, JSON.parse(stored));
|
|
419
478
|
} catch {
|
|
420
479
|
}
|
|
@@ -436,9 +495,14 @@ class Cache {
|
|
|
436
495
|
if (!this.key) throw new Error("No key defined");
|
|
437
496
|
return value[this.key];
|
|
438
497
|
}
|
|
439
|
-
save() {
|
|
440
|
-
if (this.options.
|
|
441
|
-
this.options.storage
|
|
498
|
+
save(key) {
|
|
499
|
+
if (this.options.storage) {
|
|
500
|
+
if (this.options.storage instanceof Collection) {
|
|
501
|
+
this.options.storage.put(key, this.store[key]);
|
|
502
|
+
} else if (this.options.storageKey) {
|
|
503
|
+
this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
|
|
504
|
+
}
|
|
505
|
+
}
|
|
442
506
|
}
|
|
443
507
|
/**
|
|
444
508
|
* Get all cached items
|
|
@@ -484,7 +548,7 @@ class Cache {
|
|
|
484
548
|
*/
|
|
485
549
|
delete(key) {
|
|
486
550
|
delete this.store[key];
|
|
487
|
-
this.save();
|
|
551
|
+
this.save(key);
|
|
488
552
|
return this;
|
|
489
553
|
}
|
|
490
554
|
/**
|
|
@@ -542,10 +606,10 @@ class Cache {
|
|
|
542
606
|
set(key, value, ttl = this.options.ttl) {
|
|
543
607
|
if (this.options.expiryPolicy == "keep") delete value._expired;
|
|
544
608
|
this.store[key] = value;
|
|
545
|
-
this.save();
|
|
609
|
+
this.save(key);
|
|
546
610
|
if (ttl) setTimeout(() => {
|
|
547
611
|
this.expire(key);
|
|
548
|
-
this.save();
|
|
612
|
+
this.save(key);
|
|
549
613
|
}, (ttl || 0) * 1e3);
|
|
550
614
|
return this;
|
|
551
615
|
}
|
|
@@ -1935,6 +1999,7 @@ export {
|
|
|
1935
1999
|
CliBackground,
|
|
1936
2000
|
CliEffects,
|
|
1937
2001
|
CliForeground,
|
|
2002
|
+
Collection,
|
|
1938
2003
|
CustomError,
|
|
1939
2004
|
ForbiddenError,
|
|
1940
2005
|
GatewayTimeoutError,
|