Jake Vanderwerf
2025-09-30 2cb91676044ecd0abd9c45b4835abb8b0d042312
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/***
 * A simpler cache, using localStorage
 * Mainly used to store settings locally
 * example:
 *  -> dark/light mode switch
 *  -> view mode selection
 *  -> tab navigation direction (for table views)
 **/
class SimpleCache {
    /**
     *
     * @param {string} base
     * @param {object} config
     * @param {string} config.namespace
     * @param {number} config.TTL
     * @param {number} config.maxSize
     */
    constructor(base, config = {}) {
        this.base = base;
        this.config = {
            namespace: 'jvb_cache',
            TTL: 3600000,
            maxSize: 100,
            ...config
        };
 
        // Check if Cache API is available
        this.cacheAvailable = 'caches' in window;
 
        if(!this.cacheAvailable){
            console.warn('Browser Cache API unavailable, reverting to LocalStorage');
        }
 
        // Initialize memory cache
        this._memoryCache = new Map();
        this.subscribers = new Set();
    }
 
    /**
     * Clear all memory cache
     * @returns {number} Number of items cleared
     */
    clearMemoryCache() {
        const count = this._memoryCache.size;
        this._memoryCache.clear();
 
        console.log(`Cleared ${count} items from memory cache`);
        return count;
    }
 
    /**
     * Get a setting value
     */
    async get(key) {
        let cacheKey = `${this.base}_${key}`;
 
        const cachedItem = await this.getCacheItem(cacheKey);
 
        if (!cachedItem) {
            return null;
        }
 
        // Deserialize data back to original type
        return this.deserializeData(cachedItem.data, cachedItem.dataType);
    }
 
    /**
     * Set a setting value
     */
    async set(key, value) {
        let cacheKey = `${this.base}_${key}`;
        // Serialize data with type preservation
        const serializedData = this.serializeData(value);
 
        const cacheItem = {
            data: serializedData.data,
            dataType: serializedData.type,
            timestamp: Date.now(),
        };
 
        await this.setCacheItem(cacheKey, cacheItem);
 
        // Notify subscribers
        this.notify('cache-saved', { key, value });
    }
 
    remove(key) {
        let cacheKey = `${this.base}_${key}`;
        //TODO: Actually remove it
    }
 
    serializeData(data) {
        // Handle null/undefined
        if (data === null || data === undefined) {
            return { data, type: 'primitive' };
        }
 
        // Handle Maps
        if (data instanceof Map) {
            return {
                data: Array.from(data.entries()),
                type: 'Map'
            };
        }
 
        // Handle Sets
        if (data instanceof Set) {
            return {
                data: Array.from(data),
                type: 'Set'
            };
        }
 
        // Handle Dates
        if (data instanceof Date) {
            return {
                data: data.toISOString(),
                type: 'Date'
            };
        }
 
        // Handle RegExp
        if (data instanceof RegExp) {
            return {
                data: { source: data.source, flags: data.flags },
                type: 'RegExp'
            };
        }
 
        // Handle Arrays (check before general objects)
        if (Array.isArray(data)) {
            // Recursively serialize array elements that might contain Maps/Sets
            return {
                data: data.map(item => this.serializeData(item)),
                type: 'Array'
            };
        }
 
        // Handle plain objects
        if (data && typeof data === 'object' && data.constructor === Object) {
            const serializedObj = {};
            for (const [key, value] of Object.entries(data)) {
                serializedObj[key] = this.serializeData(value);
            }
            return {
                data: serializedObj,
                type: 'Object'
            };
        }
 
        // Handle primitives (string, number, boolean)
        return { data, type: 'primitive' };
    }
 
    deserializeData(data, type) {
        if (!type || type === 'primitive') {
            return data;
        }
 
        switch (type) {
            case 'Map':
                return new Map(data);
 
            case 'Set':
                return new Set(data);
 
            case 'Date':
                return new Date(data);
 
            case 'RegExp':
                return new RegExp(data.source, data.flags);
 
            case 'Array':
                // Recursively deserialize array elements
                return data.map(item =>
                    this.deserializeData(item.data, item.type)
                );
 
            case 'Object':
                const restoredObj = {};
                for (const [key, value] of Object.entries(data)) {
                    restoredObj[key] = this.deserializeData(value.data, value.type);
                }
                return restoredObj;
 
            default:
                console.warn(`Unknown data type: ${type}, returning as-is`);
                return data;
        }
    }
    /**********************************************************
     CACHE
    **********************************************************/
    /**
     * Main method to get an item from cache
     * Tries Browser Cache API first, falls back to localStorage
     *
     * @param {string} key - Cache key
     * @returns {Promise<any>} - Cached item or null
     */
    async getCacheItem(key) {
        //Check memory cache first
        if (this._memoryCache.has(key)) {
            return this._memoryCache.get(key);
        }
 
        // Then check persistent cache
        const item = this.cacheAvailable ?
            await this.getBrowserCacheItem(key) :
            this.getLocalStorageItem(key);
 
        // Store in memory cache if found
        if (item) {
            this._memoryCache.set(key, item);
        }
 
        return item;
    }
 
    /**
     * Main method to set an item in cache
     * Uses Browser Cache API if available, falls back to localStorage
     *
     * @param {string} key - Cache key
     * @param {any} item - Item to cache
     * @returns {Promise<void>}
     */
    async setCacheItem(key, item) {
        // Always store in memory cache
        this._memoryCache.set(key, item);
 
        return this.cacheAvailable ?
            await this.setBrowserCacheItem(key, item) :
            this.setLocalStorageItem(key, item);
    }
 
    /**
     * Remove an item from cache
     *
     * @param {string} key - Cache key
     * @returns {Promise<void>}
     */
    async removeCacheItem(key) {
        // Remove from memory cache
        this._memoryCache.delete(key);
 
        return this.cacheAvailable ?
            await this.removeBrowserCacheItem(key) :
            this.removeLocalStorageItem(key);
    }
    /*********************************************************************
     BROWSER CACHE
    *********************************************************************/
    /**
     * Get item from Browser Cache API
     *
     * @param {string} key - Cache key
     * @returns {Promise<any>} - Cached item or null
     */
    async getBrowserCacheItem(key) {
        try {
            const cache = await caches.open(this.config.namespace);
            const response = await cache.match(key);
 
            if (!response) {
                return null;
            }
 
            return await response.json();
        } catch (error) {
            console.warn('Error getting from Browser Cache API:', error);
            return null;
        }
    }
 
    /**
     * Set item in Browser Cache API
     *
     * @param {string} key - Cache key
     * @param {any} item - Item to cache
     * @returns {Promise<void>}
     */
    async setBrowserCacheItem(key, item) {
        try {
            const cache = await caches.open(this.config.namespace);
            const response = new Response(JSON.stringify(item), {
                headers: { 'Content-Type': 'application/json' }
            });
 
            await cache.put(key, response);
        } catch (error) {
            console.warn('Error setting in Browser Cache API:', error);
        }
    }
 
    /**
     * Remove item from Browser Cache API
     *
     * @param {string} key - Cache key
     * @returns {Promise<void>}
     */
    async removeBrowserCacheItem(key) {
        try {
            const cache = await caches.open(this.config.namespace);
            await cache.delete(key);
        } catch (error) {
            console.warn('Error removing from Browser Cache API:', error);
        }
    }
    /*************************************************************************
     LOCAL STORAGE
    *************************************************************************/
    /**
     * Get item from localStorage
     *
     * @param {string} key - Cache key
     * @returns {object|null} - Cached item or null
     */
    getLocalStorageItem(key) {
        try {
            const stored = localStorage.getItem(key);
 
            if (!stored) {
                return null;
            }
 
            return JSON.parse(stored);
        } catch (error) {
            console.warn('Error getting from localStorage:', error);
            return null;
        }
    }
 
    /**
     * Set item in localStorage
     *
     * @param {string} key - Cache key
     * @param {any} item - Item to cache
     */
    setLocalStorageItem(key, item) {
        try {
            localStorage.setItem(key, JSON.stringify(item));
        } catch (error) {
            // Handle quota exceeded
            if (error instanceof DOMException && error.code === 22) {
                this.clearOldestLocalStorageItems();
                try {
                    localStorage.setItem(key, JSON.stringify(item));
                } catch (retryError) {
                    console.warn('Still failed to set localStorage item after cleanup:', retryError);
                }
            } else {
                console.warn('Error setting localStorage item:', error);
            }
        }
    }
 
    /**
     * Remove item from localStorage
     *
     * @param {string} key - Cache key
     */
    removeLocalStorageItem(key) {
        try {
            localStorage.removeItem(key);
        } catch (error) {
            console.warn('Error removing localStorage item:', error);
        }
    }
 
    /**
     * Clear oldest items from localStorage when quota is exceeded
     */
    clearOldestLocalStorageItems() {
        try {
            const keysToRemove = [];
 
            // Find all our cache keys
            for (let i = 0; i < localStorage.length; i++) {
                const key = localStorage.key(i);
                if (key.startsWith(this.config.namespace)) {
                    try {
                        const item = JSON.parse(localStorage.getItem(key));
                        keysToRemove.push({ key, timestamp: item.timestamp || 0 });
                    } catch (e) {
                        // If it's not valid JSON or doesn't have a timestamp, prioritize for removal
                        keysToRemove.push({ key, timestamp: 0 });
                    }
                }
            }
 
            // Sort by timestamp (oldest first)
            keysToRemove.sort((a, b) => a.timestamp - b.timestamp);
 
            // Remove the oldest 20% of items
            const removeCount = Math.max(1, Math.ceil(keysToRemove.length * 0.2));
            for (let i = 0; i < removeCount; i++) {
                if (keysToRemove[i]) {
                    localStorage.removeItem(keysToRemove[i].key);
                }
            }
        } catch (error) {
            console.warn('Error cleaning up localStorage:', error);
        }
    }
    /************************************************************************
     CLEANUP
    ************************************************************************/
    /**
     * Clean expired items from cache
     *
     * @returns {Promise<void>}
     */
    async cleanExpired() {
        const now = Date.now();
        const maxAge = this.config.TTL;
 
        if (this.cacheAvailable) {
            try {
                const cache = await caches.open(this.options.namespace);
                const keys = await cache.keys();
 
                for (const request of keys) {
                    const response = await cache.match(request);
 
                    try {
                        const cacheItem = await response.json();
                        if (now - cacheItem.timestamp > maxAge) {
                            await cache.delete(request);
                        }
                    } catch (e) {
                        // If we can't parse it, just leave it alone
                    }
                }
            } catch (error) {
                console.warn('Error cleaning browser cache:', error);
            }
        } else {
            // Clean localStorage
            try {
                for (let i = 0; i < localStorage.length; i++) {
                    const key = localStorage.key(i);
 
                    if (key && key.startsWith(this.options.namespace)) {
                        try {
                            const item = JSON.parse(localStorage.getItem(key));
 
                            if (now - item.timestamp > maxAge) {
                                localStorage.removeItem(key);
                            }
                        } catch (e) {
                            // Skip invalid items
                        }
                    }
                }
            } catch (error) {
                console.warn('Error cleaning localStorage cache:', error);
            }
        }
 
        // Clean memory cache
        for (const [key, item] of this._memoryCache.entries()) {
            if (now - item.timestamp > maxAge) {
                this._memoryCache.delete(key);
            }
        }
    }
 
    /**
     * Clear all cache
     *
     * @returns {Promise<void>}
     */
    async clear() {
        // Clear memory cache
        this._memoryCache.clear();
 
        if (this.cacheAvailable) {
            try {
                await caches.delete(this.options.namespace);
            } catch (error) {
                console.warn('Error clearing browser cache:', error);
            }
        }
 
        // Also clear localStorage in case we've used it as fallback
        this.clearLocalStorage();
    }
 
    /**
     * Clear just localStorage cache
     */
    clearLocalStorage() {
        try {
            for (let i = localStorage.length - 1; i >= 0; i--) {
                const key = localStorage.key(i);
                if (key && key.startsWith(this.options.namespace)) {
                    localStorage.removeItem(key);
                }
            }
        } catch (error) {
            console.warn('Error clearing localStorage cache:', error);
        }
    }
    /************************** OLD **********************************/
 
    /**
     * Subscribe to setting changes
     */
    subscribe(callback) {
        this.subscribers.add(callback);
        return () => this.subscribers.delete(callback);
    }
 
    /**
     * Notify subscribers
     */
    notify(event, data) {
        this.subscribers.forEach(cb => cb(event, data));
    }
}
 
window.jvbCache = SimpleCache;