semifinished
This commit is contained in:
53
src/lib/stores/cache/utils.ts
vendored
53
src/lib/stores/cache/utils.ts
vendored
@@ -1,19 +1,26 @@
|
||||
import { EventEmitter } from '$lib/event';
|
||||
import { get, writable, type Writable } from 'svelte/store';
|
||||
|
||||
export type CacheEvent = 'add' | 'update' | 'remove';
|
||||
|
||||
export class Cache<I, T> {
|
||||
private data: Writable<Map<I, T>> = writable(new Map<I, T>());
|
||||
private runningCaches: Set<I> = new Set();
|
||||
private eventEmitter = new EventEmitter<CacheEvent, [I, T | null]>();
|
||||
|
||||
private name: string;
|
||||
|
||||
private resolver: (data: I) => Promise<T | null>;
|
||||
|
||||
constructor(resolver: (data: I) => Promise<T | null>) {
|
||||
constructor(name: string, resolver: (data: I) => Promise<T | null>) {
|
||||
this.name = name;
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
||||
async get(key: I): Promise<T | null> {
|
||||
const cached = get(this.data).get(key);
|
||||
if (cached) {
|
||||
console.log(`[Cache] Found in cache: `, cached);
|
||||
console.log(`[Cache] Found in cache ${key}/${this.name}: `, cached);
|
||||
return cached;
|
||||
}
|
||||
|
||||
@@ -34,24 +41,56 @@ export class Cache<I, T> {
|
||||
|
||||
this.runningCaches.delete(key);
|
||||
|
||||
if (data)
|
||||
console.log(`[Cache] Added to cache: `, data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
set(key: I, value: T) {
|
||||
console.log(`[Cache] Added to cache: `, value);
|
||||
const data = get(this.data);
|
||||
|
||||
if (data.has(key)) {
|
||||
console.log(`[Cache] Updated cache ${key}/${this.name}: `, value);
|
||||
this.eventEmitter.emit('update', [key, value]);
|
||||
}
|
||||
else {
|
||||
console.log(`[Cache] Added to cache ${key}/${this.name}: `, value);
|
||||
this.eventEmitter.emit('add', [key, value]);
|
||||
}
|
||||
|
||||
this.data.update((data) => data.set(key, value));
|
||||
}
|
||||
|
||||
remove(key: I) {
|
||||
console.log(`[Cache] Removed from cache: `, key);
|
||||
console.log(`[Cache] Removed from cache ${key}/${this.name}: `, key);
|
||||
|
||||
this.data.update((data) => {
|
||||
data.delete(key);
|
||||
return data;
|
||||
});
|
||||
|
||||
this.eventEmitter.emit('remove', [key, null]);
|
||||
}
|
||||
|
||||
subscribe(event: CacheEvent, callback: (data: [I, T | null]) => void) {
|
||||
return this.eventEmitter.on(event, callback);
|
||||
}
|
||||
|
||||
subscribeKey(key: I, event: CacheEvent, callback: (data: T | null) => void) {
|
||||
return this.subscribe(event, (data) => {
|
||||
if (data[0] === key) callback(data[1]);
|
||||
});
|
||||
}
|
||||
|
||||
unsubscribe(event: CacheEvent, callback: (data: [I, T | null]) => void) {
|
||||
return this.eventEmitter.off(event, callback);
|
||||
}
|
||||
|
||||
subscribe_autoDispose(event: CacheEvent, callback: (data: [I, T | null]) => void) {
|
||||
this.eventEmitter.on_autoDispose(event, callback);
|
||||
}
|
||||
|
||||
subscribeKey_autoDispose(key: I, event: CacheEvent, callback: (data: T | null) => void) {
|
||||
this.subscribe_autoDispose(event, (data) => {
|
||||
if (data[0] === key) callback(data[1]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user