appview-less bluesky client
24
fork

Configure Feed

Select the types of activity you want to include in your feed.

persist cache key ttl start time to storage so it doesn't get 'refreshed' everytime app starts

dawn 9a9e2294 8246019b

+37 -6
+37 -6
src/lib/cache.ts
··· 8 8 persistOptions?: CacheOptions; 9 9 } 10 10 11 + interface PersistedEntry<V> { 12 + value: V; 13 + addedAt: number; 14 + } 15 + 11 16 // eslint-disable-next-line @typescript-eslint/no-empty-object-type 12 17 export class PersistedLRU<K extends string, V extends {}> { 13 18 private memory: LRUCache<K, V>; ··· 34 39 const state = this.storage.getState(); 35 40 for (const [key, val] of Object.entries(state)) { 36 41 try { 37 - // console.log('restoring', key); 38 42 const k = this.unprefix(key) as unknown as K; 39 - const v = val as V; 40 - this.memory.set(k, v); 43 + 44 + if (this.isPersistedEntry(val)) { 45 + const entry = val as PersistedEntry<V>; 46 + this.memory.set(k, entry.value, { start: entry.addedAt }); 47 + } else { 48 + // Handle legacy data (before this update) 49 + this.memory.set(k, val as V); 50 + } 41 51 } catch (err) { 42 52 console.warn('skipping invalid persisted entry', key, err); 43 53 } ··· 47 57 get(key: K): V | undefined { 48 58 return this.memory.get(key); 49 59 } 60 + 50 61 getSignal(key: K): Promise<V> { 51 62 return new Promise<V>((resolve) => { 52 63 if (!this.signals.has(key)) { ··· 58 69 this.signals.set(key, signals); 59 70 }); 60 71 } 72 + 61 73 set(key: K, value: V): void { 62 - this.memory.set(key, value); 63 - this.storage.set(this.prefixed(key), value); 74 + const addedAt = performance.now(); 75 + this.memory.set(key, value, { start: addedAt }); 76 + 77 + const entry: PersistedEntry<V> = { value, addedAt }; 78 + this.storage.set(this.prefixed(key), entry); 79 + 64 80 const signals = this.signals.get(key); 65 81 let signal = signals?.pop(); 66 82 while (signal) { 67 83 signal(value); 68 84 signal = signals?.pop(); 69 85 } 70 - this.storage.flush(); // TODO: uh evil and fucked up (this whole file is evil honestly) 86 + this.storage.flush(); 71 87 } 88 + 72 89 has(key: K): boolean { 73 90 return this.memory.has(key); 74 91 } 92 + 75 93 delete(key: K): void { 76 94 this.memory.delete(key); 77 95 this.storage.delete(this.prefixed(key)); 78 96 this.storage.flush(); 79 97 } 98 + 80 99 clear(): void { 81 100 this.memory.clear(); 82 101 this.storage.purge(); ··· 86 105 private prefixed(key: K): string { 87 106 return this.prefix + key; 88 107 } 108 + 89 109 private unprefix(prefixed: string): string { 90 110 return prefixed.slice(this.prefix.length); 111 + } 112 + 113 + // Type guard to check if data is our new PersistedEntry format 114 + private isPersistedEntry(data: unknown): data is PersistedEntry<V> { 115 + return ( 116 + data !== null && 117 + typeof data === 'object' && 118 + 'value' in data && 119 + 'addedAt' in data && 120 + typeof data.addedAt === 'number' 121 + ); 91 122 } 92 123 }