Compare commits

...

5 Commits

Author SHA1 Message Date
1ab8e6424b handle cache cleanup in db
Some checks failed
Build / Build NPM Project (push) Successful in 40s
Build / Publish Documentation (push) Failing after 5s
Build / Tag Version (push) Successful in 8s
2025-07-14 01:00:34 -04:00
035a1d35cb PathEventEmitter handles wildcards while prefixed
Some checks failed
Build / Build NPM Project (push) Successful in 52s
Build / Publish Documentation (push) Failing after 5s
Build / Tag Version (push) Successful in 8s
2025-07-14 00:31:11 -04:00
e78120b067 Trying to do a better job of detecting persistent caching strategy
Some checks failed
Build / Build NPM Project (push) Successful in 51s
Build / Publish Documentation (push) Failing after 5s
Build / Tag Version (push) Successful in 8s
2025-07-08 01:46:06 -04:00
71552aa243 Fixed cache save to db
Some checks failed
Build / Build NPM Project (push) Successful in 43s
Build / Publish Documentation (push) Failing after 5s
Build / Tag Version (push) Successful in 8s
2025-07-07 15:27:07 -04:00
ce3878e18b Added db safe guards
Some checks failed
Build / Build NPM Project (push) Successful in 44s
Build / Publish Documentation (push) Failing after 5s
Build / Tag Version (push) Successful in 9s
2025-07-07 14:53:14 -04:00
5 changed files with 25 additions and 18 deletions

View File

@ -1,11 +1,9 @@
<html>
<body>
<script type="module">
import {PathEventEmitter} from './dist/index.mjs';
import {PE} from './dist/index.mjs';
const emitter = new PathEventEmitter('data');
emitter.on('*', console.log);
emitter.emit('data/asd', {});
console.log('data/Ts:n', PE`${'data/Ts'}:n`.methods);
</script>
</body>
</html>

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.26.2",
"version": "0.26.7",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -39,7 +39,7 @@ export class Cache<K extends string | number | symbol, T> {
if(typeof this.options.persistentStorage == 'string')
this.options.persistentStorage = {storage: localStorage, key: this.options.persistentStorage};
if(this.options.persistentStorage?.storage?.constructor.name == 'Database') {
if(this.options.persistentStorage?.storage?.database != undefined) {
(async () => {
const persists: any = this.options.persistentStorage;
const table: Table<any, any> = await persists.storage.createTable({name: persists.key, key: this.key});
@ -47,7 +47,7 @@ export class Cache<K extends string | number | symbol, T> {
Object.assign(this.store, rows.reduce((acc, row) => ({...acc, [this.getKey(row)]: row}), {}));
done();
})();
} else if(this.options.persistentStorage?.storage?.constructor.name == 'Storage') {
} else if((<any>this.options.persistentStorage?.storage)?.getItem != undefined) {
const stored = (<Storage>this.options.persistentStorage.storage).getItem(this.options.persistentStorage.key);
if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { }
done();
@ -77,16 +77,18 @@ export class Cache<K extends string | number | symbol, T> {
private save(key?: K) {
const persists: {storage: any, key: string} = <any>this.options.persistentStorage;
if(!!persists?.storage) {
if(persists.storage?.constructor.name == 'Database') {
if(persists.storage?.database != undefined) {
(<Database>persists.storage).createTable({name: persists.key, key: <string>this.key}).then(table => {
if(key) {
table.set(key, this.get(key));
const value = this.get(key);
if(value != null) table.set(value, key);
else table.delete(key);
} else {
table.clear();
this.all().forEach(row => table.add(row));
}
});
} else if(persists.storage?.constructor.name == 'Storage') {
} else if(persists.storage?.setItem != undefined) {
persists.storage.setItem(persists.storage.key, JSONSanitize(this.all(true)));
}
}

View File

@ -166,8 +166,11 @@ export class Table<K extends IDBValidKey = any, T = any> {
return this.tx(this.name, store => store.getAllKeys(), true);
}
put(key: K, value: T): Promise<void> {
return this.tx(this.name, store => store.put(value, key));
put(value: T, key?: string): Promise<void> {
return this.tx(this.name, store => {
if (store.keyPath) return store.put(value);
return store.put(value, key);
});
}
read(): Promise<T[]>;
@ -177,8 +180,9 @@ export class Table<K extends IDBValidKey = any, T = any> {
}
set(value: T, key?: K): Promise<void> {
if(!key && !(<any>value)[this.key]) return this.add(value);
return this.put(key || (<any>value)[this.key], value);
if(key) (<any>value)[this.key] = key;
if(!(<any>value)[this.key]) return this.add(value);
return this.put(value);
}
update = this.set;

View File

@ -325,10 +325,13 @@ export class PathEventEmitter implements IPathEventEmitter{
}
on(event: Event | Event[], listener: PathListener): PathUnsubscribe {
makeArray(event).forEach(e => this.listeners.push([
e instanceof PathEvent ? e : new PathEvent(`${this.prefix}/${e}`),
listener
]));
makeArray(event).forEach(e => {
if(typeof e == 'string' && e[0] == '*' && this.prefix) e = e.slice(1);
this.listeners.push([
e instanceof PathEvent ? e : new PathEvent(`${this.prefix}/${e}`),
listener
])
});
return () => this.off(listener);
}