@jqhtml/core 2.3.10 → 2.3.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,13 @@
10
10
  * - **Quota management**: Auto-clears storage when full and retries operation
11
11
  * - **Scope validation**: Clears storage when cache key changes
12
12
  * - **Developer-friendly keys**: Scoped suffix allows easy inspection in dev tools
13
+ * - **Class-aware serialization**: ES6 class instances serialize and restore properly
14
+ *
15
+ * Class-Aware Serialization:
16
+ * ES6 class instances can be serialized and deserialized if registered via
17
+ * register_cache_class(). Classes are wrapped as {__jqhtml_class__: "Name", __jqhtml_props__: {...}}
18
+ * and restored to proper instances on retrieval. Nested class instances in objects
19
+ * and arrays are handled recursively.
13
20
  *
14
21
  * Scoping Strategy:
15
22
  * Storage is scoped by a user-provided cache key (typically a session identifier,
@@ -31,13 +38,20 @@
31
38
  * once. This ensures the application continues functioning even when storage is full.
32
39
  *
33
40
  * Usage:
41
+ * // Register classes that need to be cached (call once at startup)
42
+ * jqhtml.register_cache_class(Contact_Model);
43
+ * jqhtml.register_cache_class(User_Profile);
44
+ *
34
45
  * // Must set cache key first (typically done once on page load)
35
46
  * Jqhtml_Local_Storage.set_cache_key('user_123');
36
47
  *
37
- * // Then use storage normally
38
- * Jqhtml_Local_Storage.set('cached_component', {html: '...', timestamp: Date.now()});
48
+ * // Then use storage normally - ES6 classes serialize automatically
49
+ * Jqhtml_Local_Storage.set('cached_component', {
50
+ * contact: new Contact_Model(),
51
+ * timestamp: Date.now()
52
+ * });
39
53
  * const cached = Jqhtml_Local_Storage.get('cached_component');
40
- * Jqhtml_Local_Storage.remove('cached_component');
54
+ * // cached.contact instanceof Contact_Model === true
41
55
  *
42
56
  * IMPORTANT - Volatile Storage:
43
57
  * Storage can be cleared at any time due to:
@@ -53,21 +67,53 @@
53
67
  *
54
68
  * @internal This class is not exposed in the public API
55
69
  */
70
+ /**
71
+ * Register a class for cache serialization/deserialization.
72
+ * Must be called before attempting to cache instances of this class.
73
+ *
74
+ * @param klass - The class constructor to register
75
+ * @throws Error if klass is not a named function/class
76
+ */
77
+ export declare function register_cache_class(klass: new (...args: any[]) => any): void;
78
+ /**
79
+ * Check if a class is registered for caching
80
+ */
81
+ export declare function is_cache_class_registered(class_name: string): boolean;
82
+ /**
83
+ * Cache mode determines how component data is cached and restored.
84
+ *
85
+ * - 'data': (Recommended) Caches this.data with class-aware serialization.
86
+ * Requires ES6 classes to be registered via register_cache_class().
87
+ * On cache hit, this.data is hydrated before first render.
88
+ *
89
+ * - 'html': Caches rendered DOM HTML after children are ready.
90
+ * Does not require class registration. On cache hit, HTML is injected
91
+ * directly. Note: this.data is NOT populated during on_render() with
92
+ * cached HTML - use on_ready() for any DOM manipulation that depends on data.
93
+ */
94
+ export type CacheMode = 'data' | 'html';
56
95
  export declare class Jqhtml_Local_Storage {
57
96
  private static _cache_key;
97
+ private static _cache_mode;
58
98
  private static _storage_available;
59
99
  private static _initialized;
60
100
  /**
61
101
  * Set the cache key and initialize storage
62
102
  * Must be called before any get/set operations
63
103
  * @param {string} cache_key - Unique identifier for this cache scope
104
+ * @param {CacheMode} cache_mode - Cache strategy: 'data' (default, recommended) or 'html'
64
105
  */
65
- static set_cache_key(cache_key: string): void;
106
+ static set_cache_key(cache_key: string, cache_mode?: CacheMode): void;
66
107
  /**
67
108
  * Check if cache key has been set
68
109
  * @returns {boolean} True if cache key is configured
69
110
  */
70
111
  static has_cache_key(): boolean;
112
+ /**
113
+ * Get the current cache mode
114
+ * @returns {CacheMode} Current cache mode ('data' or 'html')
115
+ */
116
+ static get_cache_mode(): CacheMode;
71
117
  /**
72
118
  * Initialize storage system and validate scope
73
119
  * Called automatically after cache key is set
@@ -80,6 +126,11 @@ export declare class Jqhtml_Local_Storage {
80
126
  * @private
81
127
  */
82
128
  private static _is_storage_available;
129
+ /**
130
+ * Check if verbose mode is enabled
131
+ * @private
132
+ */
133
+ private static _is_verbose;
83
134
  /**
84
135
  * Validate storage scope and clear JQHTML keys if cache key changed
85
136
  * Only clears keys prefixed with 'jqhtml::' to preserve other libraries' data
@@ -106,15 +157,22 @@ export declare class Jqhtml_Local_Storage {
106
157
  */
107
158
  private static _is_ready;
108
159
  /**
109
- * Set item in localStorage
160
+ * Set item in localStorage with class-aware serialization.
161
+ *
162
+ * If serialization fails (e.g., unregistered class instances, circular refs),
163
+ * the existing cache entry is removed and nothing is stored.
164
+ *
110
165
  * @param {string} key - Storage key
111
- * @param {*} value - Value to store (will be JSON serialized)
166
+ * @param {*} value - Value to store (primitives, objects, arrays, or registered class instances)
112
167
  */
113
168
  static set(key: string, value: any): void;
114
169
  /**
115
- * Get item from localStorage
170
+ * Get item from localStorage with class-aware deserialization.
171
+ *
172
+ * If deserialization fails, returns null (as if no cache exists).
173
+ *
116
174
  * @param {string} key - Storage key
117
- * @returns {*|null} Parsed value or null if not found/unavailable
175
+ * @returns {*|null} Deserialized value with class instances restored, or null if not found/failed
118
176
  */
119
177
  static get(key: string): any | null;
120
178
  /**
@@ -122,21 +180,27 @@ export declare class Jqhtml_Local_Storage {
122
180
  * @param {string} key - Storage key
123
181
  */
124
182
  static remove(key: string): void;
183
+ /**
184
+ * Perform a serialize/deserialize round-trip on a value.
185
+ *
186
+ * This ensures "hot" data (fresh from on_load) behaves identically to "cold" data
187
+ * (restored from cache). Unregistered class instances will be converted to plain
188
+ * objects, exactly as they would be if restored from cache.
189
+ *
190
+ * Use this to normalize data after on_load() so developers catch missing class
191
+ * registrations immediately rather than only after a page reload.
192
+ *
193
+ * @param {any} value - The value to normalize
194
+ * @returns {any} The value after serialize/deserialize round-trip, or original if serialization fails
195
+ */
196
+ static normalize_for_cache(value: any): any;
125
197
  /**
126
198
  * Internal set implementation with scope validation and quota handling
127
199
  * @param {string} key
128
- * @param {*} value - Original value (not used, kept for clarity)
129
200
  * @param {string} serialized - Pre-serialized JSON string
130
201
  * @private
131
202
  */
132
203
  private static _set_item;
133
- /**
134
- * Internal get implementation
135
- * @param {string} key
136
- * @returns {*|null}
137
- * @private
138
- */
139
- private static _get_item;
140
204
  /**
141
205
  * Internal remove implementation
142
206
  * @param {string} key
@@ -1 +1 @@
1
- {"version":3,"file":"local-storage.d.ts","sourceRoot":"","sources":["../src/local-storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AACH,qBAAa,oBAAoB;IAC7B,OAAO,CAAC,MAAM,CAAC,UAAU,CAAuB;IAChD,OAAO,CAAC,MAAM,CAAC,kBAAkB,CAAwB;IACzD,OAAO,CAAC,MAAM,CAAC,YAAY,CAAkB;IAE7C;;;;OAIG;IACH,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAK7C;;;OAGG;IACH,MAAM,CAAC,aAAa,IAAI,OAAO;IAI/B;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,KAAK;IAepB;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,qBAAqB;IAYpC;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,eAAe;IA4B9B;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,kBAAkB;IA2BjC;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,UAAU;IAIzB;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,SAAS;IAIxB;;;;OAIG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI;IAqBzC;;;;OAIG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,IAAI;IAQnC;;;OAGG;IACH,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAQhC;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,CAAC,SAAS;IA4BxB;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,SAAS;IAexB;;;;OAIG;IACH,O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AC,MAAM,CAAC,SAAS;IAIxB;;;;;;;;OAQG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI;IAkDzC;;;;;;;OAOG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,IAAI;IAoCnC;;;OAGG;IACH,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAQhC;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,mBAAmB,CAAC,KAAK,EAAE,GAAG,GAAG,GAAG;IAiC3C;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,SAAS;IA4BxB;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,YAAY;CAS9B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jqhtml/core",
3
- "version": "2.3.10",
3
+ "version": "2.3.13",
4
4
  "description": "Core runtime library for JQHTML",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",