Jake Vanderwerf
2025-09-30 19d20832e881112d197254b1c41edc4c0f03758e
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
/**
 * Handles GET Requests, storing responses by a key of filters, set with setFilter method
 * Stores:
 *      - Items: the individual item data, mapped by postID/termID
 *      - Cache: the cacheKey generated by filters, and the results in the value
 *      - httpHeaders: If Modified Since tracking
 *      - domCache: rendered DOM elements, to reduce on-page rendering
 */
class DataStore {
    constructor(config = {}) {
        this.config = {
            name: 'default',
            endpoint: false,
            apiBase: jvbSettings.api,
            TTL: 3600000, // 1 hour default
            showLoading: true,
            headers: {},
            filters: {},
            ...config
        };
        if (!this.config.endpoint) {
            console.warn('No endpoint set. Only saving locally');
        }
 
        this.body = document.body;
        this.loading = document.querySelector('dialog.loading');
 
        this.headers = {
            'X-WP-Nonce': jvbSettings.nonce,
            ...this.config.headers
        };
 
        // Data stores
        this.items = new Map();
        this.cache = new Map(); //TODO: call this resultsCache
        this.httpHeaders = new Map();
        this.domCache = new Map();
        this.forms = new Map();
 
        // State management
        this.filters = config.filters ?? {};
        this.subscribers = new Set();
        this.db = null;
        this.currentRequest = null;
 
        // Server Timestamps - needed?
        this.cachedContent = JSON.parse(cacheJVB.cache) || {};
        this.lastTimestampUpdate = Date.now();
 
        this.initDB();
        document.addEventListener('beforeUnload', () =>this.destroy());
    }
 
    async initDB() {
        if (!('indexedDB' in window)) return;
 
        const request = indexedDB.open(`jvb_${this.config.name}_db`, 1);
 
        request.onupgradeneeded = (e) => {
            const db = e.target.result;
            // Items store
            if (!db.objectStoreNames.contains('items')) {
                db.createObjectStore('items', { keyPath: 'id' });
            }
 
            // DOM cache for rendered elements
            if (!db.objectStoreNames.contains('dom')) {
                db.createObjectStore('dom', { keyPath: 'id' });
            }
 
            if (!db.objectStoreNames.contains('forms')) {
                let forms = db.createObjectStore('forms', {
                    keyPath: 'formId',
                });
                forms.createIndex('status', 'status', {unique:false});
                forms.createIndex('operationId', 'operationId', {unique:false});
                forms.createIndex('timestamp', 'timestamp', {unique:false});
            }
 
            // Cache store for GET requests with endpoint index
            if (!db.objectStoreNames.contains('cache')) {
                const cacheStore = db.createObjectStore('cache', { keyPath: 'key' });
                cacheStore.createIndex('timestamp', 'timestamp', { unique: false });
                cacheStore.createIndex('endpoint', 'endpoint', { unique: false });
                cacheStore.createIndex('filters', 'filters', { unique: false });
            }
 
            // HTTP headers store
            if (!db.objectStoreNames.contains('headers')) {
                db.createObjectStore('headers', { keyPath: 'key' });
            }
        };
 
        request.onsuccess = (e) => {
            this.db = e.target.result;
            this.loadFromDB();
        };
 
        request.onerror = (e) => {
            console.error('IndexedDB error:', e);
        };
    }
 
    async loadFromDB() {
        if (!this.db) return;
 
        try {
            await Promise.all([
                this.loadItems(),
                this.loadCache(),
                this.loadHeaders(),
                this.loadDOMCache(),
                this.loadForms()
            ]);
        } catch (error) {
            console.error('Error loading from DB:', error);
        }
    }
 
    async loadItems() {
        if (!this.db) return;
 
        return new Promise((resolve) => {
            const tx = this.db.transaction(['items'], 'readonly');
            const store = tx.objectStore('items');
            const request = store.getAll();
 
            request.onsuccess = (e) => {
                e.target.result.forEach(item => {
                    this.items.set(item.id, item);
                });
                this.notify('items-loaded', { items: Array.from(this.items.values()) });
                resolve();
            };
        });
    }
 
    async loadCache() {
        if (!this.db) return;
 
        return new Promise((resolve) => {
            const tx = this.db.transaction(['cache'], 'readonly');
            const store = tx.objectStore('cache');
            const request = store.getAll();
 
            request.onsuccess = (e) => {
                e.target.result.forEach(item => {
                    if (this.isCacheValid(item)) {
                        this.cache.set(item.key, item);
                    }
                });
                resolve();
            };
        });
    }
 
    async loadHeaders() {
        if (!this.db) return;
 
        return new Promise((resolve) => {
            const tx = this.db.transaction(['headers'], 'readonly');
            const store = tx.objectStore('headers');
            const request = store.getAll();
 
            request.onsuccess = (e) => {
                e.target.result.forEach(header => {
                    this.httpHeaders.set(header.key, header);
                });
                resolve();
            };
        });
    }
 
    async loadDOMCache() {
        if (!this.db) return;
 
        return new Promise((resolve) => {
            const tx = this.db.transaction(['dom'], 'readonly');
            const store = tx.objectStore('dom');
            const request = store.getAll();
 
            request.onsuccess = (e) => {
                e.target.result.forEach(domEntry => {
                    // Convert stored HTML back to DOM elements
                    const reconstructed = {};
                    Object.entries(domEntry.views).forEach(([viewName, html]) => {
                        const temp = document.createElement('div');
                        temp.innerHTML = html;
                        reconstructed[viewName] = temp.firstElementChild;
                    });
                    this.domCache.set(domEntry.id, reconstructed);
                });
                resolve();
            };
        });
    }
 
    async loadForms() {
        if (!this.db) return;
 
        return new Promise((resolve) => {
            const tx = this.db.transaction(['forms'], 'readonly');
            const store = tx.objectStore('forms');
            const request = store.getAll();
 
            request.onsuccess = (e) => {
                e.target.result.forEach(form => {
                    this.forms.set(form.key, form);
                });
                resolve();
            };
        });
    }
 
    setLoading(on) {
        this.body.classList.toggle('loading', on);
        if (on) {
            this.loading.showModal();
        } else {
            this.loading.close();
        }
 
    }
 
    /**
     * Main fetch method with caching and conditional requests
     */
    async fetch(endpoint = null, options = {}) {
        const {
            filters = this.filters,
            headers = {},
        } = options;
 
        if (this.config.showLoading) {
            this.setLoading(true);
        }
 
 
        // Use provided endpoint or config endpoint
        const apiEndpoint = endpoint || this.config.endpoint;
        if (!apiEndpoint) {
            throw new Error('No endpoint specified');
        }
 
        // Generate cache key from endpoint and filters
        const cacheKey = this.generateCacheKey(apiEndpoint, filters);
        const cleanedFilters = this.cleanFilters(filters);
 
        // Build request URL
        const params = new URLSearchParams(cleanedFilters);
        const url = `${this.config.apiBase}${apiEndpoint}${params.toString() ? '?' + params : ''}`;
 
        // Prepare headers with conditional requests
        const requestHeaders = {
            ...this.headers,
            ...headers
        };
 
        // Add conditional headers from stored data
        const headerKey = this.generateHeaderKey(url);
        const storedHeaders = this.httpHeaders.get(headerKey);
        const cachedData = this.cache.get(cacheKey);
 
        if (storedHeaders && cachedData) {
            if (storedHeaders.etag) {
                requestHeaders['If-None-Match'] = storedHeaders.etag;
            }
            if (storedHeaders.lastModified) {
                requestHeaders['If-Modified-Since'] = storedHeaders.lastModified;
            }
        }
 
        try {
            const response = await fetch(url, {
                method: 'GET',
                headers: requestHeaders
            });
 
            console.log('DataStore response status: ',response.status);
            // Handle 304 Not Modified - return cached data
            if (response.status === 304) {
                console.debug(`304 Not Modified for ${url}`);
                if (cachedData) {
                    // Update timestamp but keep data
                    cachedData.timestamp = Date.now();
                    this.cache.set(cacheKey, cachedData);
                    await this.saveCacheToDB(cacheKey, cachedData);
 
                    // Store current request info
                    this.currentRequest = {
                        filters: cleanedFilters,
                        data: cachedData.data,
                        cached: true
                    };
 
                    //TODO: should this be items-loaded?
                    this.notify('data-cached', {
                        data: cachedData.data,
                        filters: cleanedFilters,
                        cached: true
                    });
                    return cachedData.data;
                }
            }
 
            if (!response.ok) {
                throw new Error(`HTTP ${response.status}: ${response.statusText}`);
            }
 
            // Store response headers for future conditional requests
            this.storeResponseHeaders(headerKey, response);
 
            const data = await response.json();
 
            console.log('Fetched data: ', data);
 
            // Cache the response
            const cacheEntry = {
                key: cacheKey,
                endpoint: apiEndpoint,
                data: data,
                timestamp: Date.now(),
                filters: cleanedFilters
            };
 
            this.cache.set(cacheKey, cacheEntry);
            await this.saveCacheToDB(cacheKey, cacheEntry);
 
            // Update items if data contains them
            if (data.items && this.config.endpoint === apiEndpoint) {
                this.updateItems(data.items);
            }
 
            // Store current request info
            this.currentRequest = {
                filters: cleanedFilters,
                data: data,
                cached: false
            };
 
            this.notify('data-fetched', {
                endpoint: apiEndpoint,
                data: data,
                filters: cleanedFilters
            });
            return data;
 
        } catch (error) {
            console.error('Fetch error:', error);
 
            // Try to return stale cache on error
            if (cachedData) {
                console.warn('Returning stale cache due to fetch error');
                this.currentRequest = {
                    filters: cleanedFilters,
                    data: cachedData.data,
                    cached: true,
                    stale: true
                };
                this.notify('stale-cache-used', {
                    data: cachedData.data,
                    filters: cleanedFilters
                });
                return cachedData.data;
            }
 
            this.notify('fetch-error', { error, filters: cleanedFilters });
            throw error;
        } finally {
            if (this.config.showLoading) {
                this.setLoading(false);
            }
        }
    }
 
    /**
     * Update items in local store
     */
    updateItems(items) {
        this.items.clear();
        items.forEach(item => {
            this.items.set(item.id, item);
        });
        this.saveItemsToDB();
        this.notify('items-updated', { items });
    }
 
    /**
     * Get current request data and state
     */
    getCurrentRequest() {
        return this.currentRequest;
    }
 
    /**
     * Get a specific item by ID
     */
    getItem(id) {
        let check = parseInt(id);
        id = isNaN(check) ? id : check;
        const item = this.items.get(id);
        return item ? this.unserializeData(item) : null;
    }
 
    setItem(id, data, mergeExisting = true) {
        if (mergeExisting && this.items.has(id)) {
            let existing = this.getItem(id); // Get unserialized version
            data = window.deepMerge(existing, data);
        }
 
        const serialized = this.serializeData(data);
        this.items.set(id, serialized); // Store serialized version
        this.saveItemsToDB();
        this.notify('item-stored', data); // Notify with original data
        return data;
    }
 
    hasUnrecoverableFiles(data) {
        if (!data || typeof data !== 'object') return false;
 
        if (data._wasFile || data._wasBlob) return true;
 
        if (Array.isArray(data)) {
            return data.some(item => this.hasUnrecoverableFiles(item));
        }
 
        if (data instanceof FormData) {
            for (const [key, value] of data.entries()) {
                if (value instanceof File || value instanceof Blob) return true;
            }
            return false;
        }
 
        return Object.values(data).some(value => this.hasUnrecoverableFiles(value));
    }
 
    serializeFormData(formData) {
        const obj = {};
 
        for (const [key, value] of formData.entries()) {
            // Handle file metadata (can't store actual file)
            if (value instanceof File) {
                continue;
            }
            // Check if key already exists (for multiple values)
            if (key in obj) {
                // Convert to array if not already
                if (!Array.isArray(obj[key])) {
                    obj[key] = [obj[key]];
                }
                obj[key].push(value);
            } else {
                obj[key] = value;
            }
        }
        return obj;
    }
 
    serializeData(data) {
        if (!data) return null;
 
        if (data instanceof HTMLElement) {
            return null;
        }
        if (typeof data !== 'object') return data;
 
        if (data === null) return null;
 
        if (data instanceof FormData) {
            return {
                _type: 'FormData',
                ... this.serializeFormData(data)
            };
        }
 
        // Handle Arrays
        if (Array.isArray(data)) {
            return data.map(item => this.serializeData(item));
        }
 
        // Handle Date objects
        if (data instanceof Date) {
            return {
                _type: 'Date',
                value: data.toISOString()
            };
        }
 
        // Handle plain objects
        const output = {};
        for (const [key, value] of Object.entries(data)) {
            output[key] = this.serializeData(value);
        }
        return output;
    }
 
    unserializeData(data) {
        if (!data || typeof data !== 'object') return data;
        if (data === null) return null;
 
        // Check for special types
        if (data._type) {
            switch (data._type) {
                case 'FormData':
                    return this.unserializeFormData(data);
                case 'File':
                    // Can't reconstruct File, return metadata with warning flag
                    return {
                        _wasFile: true,
                        _fileMetadata: data,
                        name: data.name,
                        type: data.type,
                        size: data.size
                    };
                case 'Blob':
                    // Can't reconstruct Blob
                    return {
                        _wasBlob: true,
                        _blobMetadata: data,
                        type: data.type,
                        size: data.size
                    };
                case 'Date':
                    return new Date(data.value);
            }
        }
 
        // Handle Arrays
        if (Array.isArray(data)) {
            return data.map(item => this.unserializeData(item));
        }
 
        // Handle plain objects
        const output = {};
        for (const [key, value] of Object.entries(data)) { // Fixed: 'of' not 'in'
            output[key] = this.unserializeData(value);
        }
        return output;
    }
    unserializeFormData(data) {
        const formData = new FormData();
 
        for (const [key, value] of Object.entries(data)) {
            if (Array.isArray(value)) {
                value.forEach(item => {
                    if (item?._isFile) {
                        console.warn(`Cannot restore file "${item.name}" from stored data`);
                        // Optionally append metadata as JSON string for reference
                        formData.append(key + '_was_file', JSON.stringify(item));
                    } else {
                        formData.append(key, item);
                    }
                });
            } else if (value?._isFile) {
                console.warn(`Cannot restore file "${value.name}" from stored data`);
                // Optionally append metadata as JSON string for reference
                formData.append(key + '_was_file', JSON.stringify(value));
            } else if (value !== null && value !== undefined) {
                formData.append(key, value);
            }
        }
 
        return formData;
    }
 
 
    clearItem(key) {
        this.items.delete(key);
        if (this.db) {
            const tx = this.db.transaction(['items'], 'readwrite');
            const store = tx.objectStore('items');
            store.delete(key);
        }
    }
 
    /**
     * Filter helpers
     */
    cleanFilters(filters) {
        const cleaned = {};
        Object.entries(filters).forEach(([key, value]) => {
            if (value !== null && value !== undefined && value !== '') {
                // Handle special cases based on existing patterns
                if (key === 'taxonomies' && typeof value === 'object') {
                    Object.entries(value).forEach(([taxName, terms]) => {
                        if (Array.isArray(terms) && terms.length > 0) {
                            cleaned[`tax_${taxName}`] = terms.join(',');
                        } else if (terms) {
                            cleaned[`tax_${taxName}`] = terms;
                        }
                    });
                } else if (key === 'date' && typeof value === 'object') {
                    if (value.after) cleaned.after = value.after;
                    if (value.before) cleaned.before = value.before;
                } else {
                    cleaned[key] = value;
                }
            }
        });
        return cleaned;
    }
 
    setFilter(key, value) {
        const oldValue = this.filters[key];
 
        if (value === '' || value === null || value === undefined) {
            delete this.filters[key];
        } else {
            this.filters[key] = value;
        }
 
        this.notify('filters-changed', {
            filters: this.filters,
            changed: { key, oldValue, newValue: value }
        });
 
        // Auto-fetch if endpoint is configured
        if (this.config.endpoint) {
            this.fetch();
        }
    }
 
    /**
     * Remove a filter
     */
    removeFilter(key) {
        const oldValue = this.filters[key];
 
        if (oldValue !== undefined) {
            delete this.filters[key];
            this.notify('filters-changed', {
                filters: this.filters,
                removed: { key, oldValue }
            });
 
            // Auto-fetch if endpoint is configured
            if (this.config.endpoint) {
                this.fetch();
            }
        }
    }
 
    /**
     * Clear all filters
     */
    clearFilters() {
        const oldFilters = { ...this.filters };
        //Restore baseline filters
        this.filters = this.config.filters;
 
        this.notify('filters-cleared', {
            oldFilters,
            filters: this.filters
        });
 
        // Auto-fetch if endpoint is configured
        if (this.config.endpoint) {
            this.fetch();
        }
    }
 
    /**
     * Cache management
     */
    generateCacheKey(endpoint, filters) {
        const sorted = Object.keys(filters).sort().reduce((obj, key) => {
            obj[key] = filters[key];
            return obj;
        }, {});
        return `${endpoint}_${JSON.stringify(sorted)}`;
    }
 
    generateHeaderKey(url) {
        return `headers_${url}`;
    }
 
    isCacheValid(cacheEntry, maxAge = this.config.TTL) {
        if (!cacheEntry || !cacheEntry.timestamp) return false;
        return (Date.now() - cacheEntry.timestamp) < maxAge;
    }
 
    storeResponseHeaders(key, response) {
        const headers = {
            key,
            etag: response.headers.get('ETag'),
            lastModified: response.headers.get('Last-Modified'),
            timestamp: Date.now()
        };
 
        this.httpHeaders.set(key, headers);
        this.saveHeadersToDB(key, headers);
    }
 
 
 
    clearCache() {
        this.cache.clear();
 
        if (this.db) {
            const tx = this.db.transaction(['cache'], 'readwrite');
            const store = tx.objectStore('cache');
            store.clear();
        }
 
        this.notify('cache-cleared');
    }
 
    invalidateCache(pattern) {
        const keysToDelete = [];
 
        this.cache.forEach((value, key) => {
            if (typeof pattern === 'string' && key.includes(pattern)) {
                keysToDelete.push(key);
            } else if (pattern instanceof RegExp && pattern.test(key)) {
                keysToDelete.push(key);
            }
        });
 
        keysToDelete.forEach(key => {
            this.cache.delete(key);
            if (this.db) {
                const tx = this.db.transaction(['cache'], 'readwrite');
                const store = tx.objectStore('cache');
                store.delete(key);
            }
        });
 
        this.notify('cache-invalidated', { count: keysToDelete.length });
    }
 
    /**
     * DOM Cache Management
     */
 
    /**
     * Store rendered DOM element for a specific item and view
     */
    storeDOMElement(itemId, viewName, element) {
        if (!this.domCache.has(itemId)) {
            this.domCache.set(itemId, {});
        }
 
        const itemCache = this.domCache.get(itemId);
        itemCache[viewName] = element.cloneNode(true);
        this.domCache.set(itemId, itemCache);
 
        // Save to IndexedDB
        this.saveDOMCacheToDB(itemId, itemCache);
    }
 
 
    /**
     * Retrieve cached DOM element for a specific item and view
     */
    getDOMElement(itemId, viewName) {
        const itemCache = this.domCache.get(itemId);
        if (itemCache && itemCache[viewName]) {
            return itemCache[viewName].cloneNode(true);
        }
        return null;
    }
 
    /**
     * Check if DOM element exists in cache
     */
    hasDOMElement(itemId, viewName) {
        const itemCache = this.domCache.get(itemId);
        return itemCache && itemCache[viewName];
    }
 
    /**
     * Clear DOM cache for a specific item
     */
    clearDOMCache(itemId) {
        this.domCache.delete(itemId);
 
        if (this.db) {
            const tx = this.db.transaction(['dom'], 'readwrite');
            const store = tx.objectStore('dom');
            store.delete(itemId);
        }
    }
 
    /**
     * Clear all DOM cache
     */
    clearAllDOMCache() {
        this.domCache.clear();
 
        if (this.db) {
            const tx = this.db.transaction(['dom'], 'readwrite');
            const store = tx.objectStore('dom');
            store.clear();
        }
    }
 
    /**
     * Helper method to render or retrieve cached DOM elements
     */
    renderOrRetrieve(item, viewName, renderFunction) {
        // Check cache first
        const cached = this.getDOMElement(item.id, viewName);
        if (cached) {
            return cached;
        }
 
        // Render new element
        const element = renderFunction(item);
 
        // Cache the rendered element
        this.storeDOMElement(item.id, viewName, element);
 
        return element;
    }
    /**
     * Database operations
     */
    async saveItemsToDB() {
        if (!this.db) return;
 
        const tx = this.db.transaction(['items'], 'readwrite');
        const store = tx.objectStore('items');
 
        store.clear();
        this.items.forEach(item => {
            if (!item._deleted) {
                store.put(item);
            }
        });
    }
 
    async saveCacheToDB(key, data) {
        if (!this.db) return;
 
        const tx = this.db.transaction(['cache'], 'readwrite');
        const store = tx.objectStore('cache');
        store.put(data);
    }
 
    async saveHeadersToDB(key, headers) {
        if (!this.db) return;
 
        const tx = this.db.transaction(['headers'], 'readwrite');
        const store = tx.objectStore('headers');
        store.put(headers);
    }
 
    async saveDOMCacheToDB(itemId, domCache) {
        if (!this.db) return;
 
        // Convert DOM elements to HTML strings for storage
        const serialized = {
            id: itemId,
            views: {}
        };
 
        Object.entries(domCache).forEach(([viewName, element]) => {
            if (element && element.outerHTML) {
                serialized.views[viewName] = element.outerHTML;
            }
        });
 
        const tx = this.db.transaction(['dom'], 'readwrite');
        const store = tx.objectStore('dom');
        store.put(serialized);
    }
 
    async saveFormsToDB(key, form) {
        if (!this.db) return;
 
        const tx = this.db.transaction(['forms'], 'readwrite');
        const store = tx.objectStore('forms');
        store.put(form);
    }
 
    storeForm(key, form) {
        this.forms.set(key, form);
        this.saveFormsToDB(key, form);
    }
 
    getForm(key) {
        return this.forms.has(key) ? this.forms.get(key) : null;
    }
 
    getAllForms() {
        return this.forms;
    }
 
    clearForm(key) {
        this.forms.delete(key);
        if (this.db) {
            const tx = this.db.transaction(['forms'], 'readwrite');
            const store = tx.objectStore('forms');
            store.delete(key);
        }
    }
 
    clearAllForms() {
        this.forms.clear();
        if (this.db) {
            const tx = this.db.transaction(['forms'], 'readwrite');
            const store = tx.objectStore('dom');
            store.clear();
        }
    }
 
    /**
     * Event system
     */
    subscribe(callback) {
        this.subscribers.add(callback);
        return () => this.subscribers.delete(callback);
    }
 
    notify(event, data) {
        this.subscribers.forEach(cb => cb(event, data));
    }
 
    /**
     * Cleanup
     */
    destroy() {
        if (this.db) {
            this.db.close();
        }
        this.subscribers.clear();
        this.items.clear();
        this.cache.clear();
        this.domCache.clear();
        this.httpHeaders.clear();
    }
}
 
window.jvbStore = DataStore;