Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
1ab8e6424b | |||
035a1d35cb | |||
e78120b067 |
@ -1,11 +1,9 @@
|
|||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {PathEventEmitter} from './dist/index.mjs';
|
import {PE} from './dist/index.mjs';
|
||||||
|
|
||||||
const emitter = new PathEventEmitter('data');
|
console.log('data/Ts:n', PE`${'data/Ts'}:n`.methods);
|
||||||
emitter.on('*', console.log);
|
|
||||||
emitter.emit('data/asd', {});
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.26.4",
|
"version": "0.26.7",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
12
src/cache.ts
12
src/cache.ts
@ -39,7 +39,7 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
if(typeof this.options.persistentStorage == 'string')
|
if(typeof this.options.persistentStorage == 'string')
|
||||||
this.options.persistentStorage = {storage: localStorage, key: this.options.persistentStorage};
|
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 () => {
|
(async () => {
|
||||||
const persists: any = this.options.persistentStorage;
|
const persists: any = this.options.persistentStorage;
|
||||||
const table: Table<any, any> = await persists.storage.createTable({name: persists.key, key: this.key});
|
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}), {}));
|
Object.assign(this.store, rows.reduce((acc, row) => ({...acc, [this.getKey(row)]: row}), {}));
|
||||||
done();
|
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);
|
const stored = (<Storage>this.options.persistentStorage.storage).getItem(this.options.persistentStorage.key);
|
||||||
if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { }
|
if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { }
|
||||||
done();
|
done();
|
||||||
@ -77,16 +77,18 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
private save(key?: K) {
|
private save(key?: K) {
|
||||||
const persists: {storage: any, key: string} = <any>this.options.persistentStorage;
|
const persists: {storage: any, key: string} = <any>this.options.persistentStorage;
|
||||||
if(!!persists?.storage) {
|
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 => {
|
(<Database>persists.storage).createTable({name: persists.key, key: <string>this.key}).then(table => {
|
||||||
if(key) {
|
if(key) {
|
||||||
table.set(this.get(key), key);
|
const value = this.get(key);
|
||||||
|
if(value != null) table.set(value, key);
|
||||||
|
else table.delete(key);
|
||||||
} else {
|
} else {
|
||||||
table.clear();
|
table.clear();
|
||||||
this.all().forEach(row => table.add(row));
|
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)));
|
persists.storage.setItem(persists.storage.key, JSONSanitize(this.all(true)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -325,10 +325,13 @@ export class PathEventEmitter implements IPathEventEmitter{
|
|||||||
}
|
}
|
||||||
|
|
||||||
on(event: Event | Event[], listener: PathListener): PathUnsubscribe {
|
on(event: Event | Event[], listener: PathListener): PathUnsubscribe {
|
||||||
makeArray(event).forEach(e => this.listeners.push([
|
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}`),
|
e instanceof PathEvent ? e : new PathEvent(`${this.prefix}/${e}`),
|
||||||
listener
|
listener
|
||||||
]));
|
])
|
||||||
|
});
|
||||||
return () => this.off(listener);
|
return () => this.off(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user