@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/cache.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { Collection } from './collection.ts';
|
|
1
2
|
export type CacheOptions = {
|
|
2
3
|
/** Delete keys automatically after x amount of seconds */
|
|
3
4
|
ttl?: number;
|
|
4
5
|
/** Storage to persist cache */
|
|
5
|
-
storage?: Storage
|
|
6
|
+
storage?: Storage | Collection<any, any>;
|
|
6
7
|
/** Key cache will be stored under */
|
|
7
8
|
storageKey?: string;
|
|
8
9
|
/** Keep or delete cached items once expired, defaults to delete */
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare class Collection<K extends IDBValidKey, T> {
|
|
2
|
+
readonly db: string;
|
|
3
|
+
readonly collection: string;
|
|
4
|
+
readonly version?: number | undefined;
|
|
5
|
+
private setup?;
|
|
6
|
+
private database;
|
|
7
|
+
constructor(db: string, collection: string, version?: number | undefined, setup?: ((db: IDBDatabase, event: IDBVersionChangeEvent) => void) | undefined);
|
|
8
|
+
tx<T>(collection: string, fn: ((store: IDBObjectStore) => IDBRequest), readonly?: boolean): Promise<T>;
|
|
9
|
+
add(value: T, key?: K): Promise<void>;
|
|
10
|
+
count(): Promise<number>;
|
|
11
|
+
put(key: K, value: T): Promise<void>;
|
|
12
|
+
getAll(): Promise<T[]>;
|
|
13
|
+
getAllKeys(): Promise<K[]>;
|
|
14
|
+
get(key: K): Promise<T>;
|
|
15
|
+
delete(key: K): Promise<void>;
|
|
16
|
+
clear(): Promise<void>;
|
|
17
|
+
}
|
package/dist/index.cjs
CHANGED
|
@@ -396,6 +396,59 @@ ${opts.message || this.desc}`;
|
|
|
396
396
|
function makeArray(value) {
|
|
397
397
|
return Array.isArray(value) ? value : [value];
|
|
398
398
|
}
|
|
399
|
+
class Collection {
|
|
400
|
+
constructor(db, collection, version, setup) {
|
|
401
|
+
__publicField(this, "database");
|
|
402
|
+
this.db = db;
|
|
403
|
+
this.collection = collection;
|
|
404
|
+
this.version = version;
|
|
405
|
+
this.setup = setup;
|
|
406
|
+
this.database = new Promise((resolve, reject) => {
|
|
407
|
+
const req = indexedDB.open(this.db, this.version);
|
|
408
|
+
req.onerror = () => reject(req.error);
|
|
409
|
+
req.onsuccess = () => resolve(req.result);
|
|
410
|
+
req.onupgradeneeded = (event) => {
|
|
411
|
+
const db2 = req.result;
|
|
412
|
+
if (!db2.objectStoreNames.contains(collection)) db2.createObjectStore(collection, { keyPath: void 0 });
|
|
413
|
+
if (this.setup) this.setup(db2, event);
|
|
414
|
+
};
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
async tx(collection, fn2, readonly) {
|
|
418
|
+
const db = await this.database;
|
|
419
|
+
const tx = db.transaction(collection, readonly ? "readonly" : "readwrite");
|
|
420
|
+
const store = tx.objectStore(collection);
|
|
421
|
+
return new Promise((resolve, reject) => {
|
|
422
|
+
const request = fn2(store);
|
|
423
|
+
request.onsuccess = () => resolve(request.result);
|
|
424
|
+
request.onerror = () => reject(request.error);
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
add(value, key) {
|
|
428
|
+
return this.tx(this.collection, (store) => store.add(value, key));
|
|
429
|
+
}
|
|
430
|
+
count() {
|
|
431
|
+
return this.tx(this.collection, (store) => store.count(), true);
|
|
432
|
+
}
|
|
433
|
+
put(key, value) {
|
|
434
|
+
return this.tx(this.collection, (store) => store.put(value, key));
|
|
435
|
+
}
|
|
436
|
+
getAll() {
|
|
437
|
+
return this.tx(this.collection, (store) => store.getAll(), true);
|
|
438
|
+
}
|
|
439
|
+
getAllKeys() {
|
|
440
|
+
return this.tx(this.collection, (store) => store.getAllKeys(), true);
|
|
441
|
+
}
|
|
442
|
+
get(key) {
|
|
443
|
+
return this.tx(this.collection, (store) => store.get(key), true);
|
|
444
|
+
}
|
|
445
|
+
delete(key) {
|
|
446
|
+
return this.tx(this.collection, (store) => store.delete(key));
|
|
447
|
+
}
|
|
448
|
+
clear() {
|
|
449
|
+
return this.tx(this.collection, (store) => store.clear());
|
|
450
|
+
}
|
|
451
|
+
}
|
|
399
452
|
class Cache {
|
|
400
453
|
/**
|
|
401
454
|
* Create new cache
|
|
@@ -411,14 +464,19 @@ ${opts.message || this.desc}`;
|
|
|
411
464
|
* @return {T[]} Array of items
|
|
412
465
|
*/
|
|
413
466
|
__publicField(this, "values", this.all());
|
|
467
|
+
var _a;
|
|
414
468
|
this.key = key;
|
|
415
469
|
this.options = options;
|
|
416
|
-
if (options.storageKey && !options.storage && typeof Storage !== "undefined")
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
470
|
+
if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
|
|
471
|
+
if (options.storage) {
|
|
472
|
+
if (options.storage instanceof Collection) {
|
|
473
|
+
(async () => {
|
|
474
|
+
var _a2;
|
|
475
|
+
(await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => this.add(v));
|
|
476
|
+
})();
|
|
477
|
+
} else if (options.storageKey) {
|
|
478
|
+
const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
|
|
479
|
+
if (stored != null) try {
|
|
422
480
|
Object.assign(this.store, JSON.parse(stored));
|
|
423
481
|
} catch {
|
|
424
482
|
}
|
|
@@ -440,9 +498,14 @@ ${opts.message || this.desc}`;
|
|
|
440
498
|
if (!this.key) throw new Error("No key defined");
|
|
441
499
|
return value[this.key];
|
|
442
500
|
}
|
|
443
|
-
save() {
|
|
444
|
-
if (this.options.
|
|
445
|
-
this.options.storage
|
|
501
|
+
save(key) {
|
|
502
|
+
if (this.options.storage) {
|
|
503
|
+
if (this.options.storage instanceof Collection) {
|
|
504
|
+
this.options.storage.put(key, this.store[key]);
|
|
505
|
+
} else if (this.options.storageKey) {
|
|
506
|
+
this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
|
|
507
|
+
}
|
|
508
|
+
}
|
|
446
509
|
}
|
|
447
510
|
/**
|
|
448
511
|
* Get all cached items
|
|
@@ -488,7 +551,7 @@ ${opts.message || this.desc}`;
|
|
|
488
551
|
*/
|
|
489
552
|
delete(key) {
|
|
490
553
|
delete this.store[key];
|
|
491
|
-
this.save();
|
|
554
|
+
this.save(key);
|
|
492
555
|
return this;
|
|
493
556
|
}
|
|
494
557
|
/**
|
|
@@ -546,10 +609,10 @@ ${opts.message || this.desc}`;
|
|
|
546
609
|
set(key, value, ttl = this.options.ttl) {
|
|
547
610
|
if (this.options.expiryPolicy == "keep") delete value._expired;
|
|
548
611
|
this.store[key] = value;
|
|
549
|
-
this.save();
|
|
612
|
+
this.save(key);
|
|
550
613
|
if (ttl) setTimeout(() => {
|
|
551
614
|
this.expire(key);
|
|
552
|
-
this.save();
|
|
615
|
+
this.save(key);
|
|
553
616
|
}, (ttl || 0) * 1e3);
|
|
554
617
|
return this;
|
|
555
618
|
}
|
|
@@ -1938,6 +2001,7 @@ ${opts.message || this.desc}`;
|
|
|
1938
2001
|
exports2.CliBackground = CliBackground;
|
|
1939
2002
|
exports2.CliEffects = CliEffects;
|
|
1940
2003
|
exports2.CliForeground = CliForeground;
|
|
2004
|
+
exports2.Collection = Collection;
|
|
1941
2005
|
exports2.CustomError = CustomError;
|
|
1942
2006
|
exports2.ForbiddenError = ForbiddenError;
|
|
1943
2007
|
exports2.GatewayTimeoutError = GatewayTimeoutError;
|