@warp-drive/experiments 0.2.7-alpha.14 → 0.2.7-alpha.16

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.
@@ -0,0 +1,1073 @@
1
+ import { defineSignal as defineSignal$1, makeInitializer, memoized } from '@warp-drive/core/signals/-leaked';
2
+ import { defineSignal, withSignalStore, entangleSignal, getOrCreateInternalSignal, notifyInternalSignal } from '@warp-drive/core/signals/-private';
3
+
4
+ /**
5
+ * A reactive wrapper around the browser's Map API that provides
6
+ * granular per-key reactivity via WarpDrive's signal system.
7
+ */
8
+ class SignalMap {
9
+ _map = {};
10
+ // _size is signal-backed via defineSignal below
11
+
12
+ _signals = withSignalStore(this._map);
13
+ subscribe(key) {
14
+ const existing = this._signals.has(key);
15
+ entangleSignal(this._signals, this._map, key, undefined);
16
+ const size = this._size;
17
+ this._size = existing ? size : size + 1;
18
+ return existing;
19
+ }
20
+ notify(key) {
21
+ const signal = getOrCreateInternalSignal(this._signals, this._map, key, 0);
22
+ notifyInternalSignal(signal);
23
+ }
24
+ clear() {
25
+ for (const value of this._signals.values()) {
26
+ notifyInternalSignal(value);
27
+ }
28
+ this._size = 0;
29
+ }
30
+ get size() {
31
+ return this._size;
32
+ }
33
+ }
34
+ defineSignal(SignalMap.prototype, '_size', 0);
35
+
36
+ const DEFAULT_CACHE_ID = 'reactive-cache';
37
+ const CACHES = new Map();
38
+ const StorageEvents = new BroadcastChannel('reactive-cache-events');
39
+ const CACHE_URL = `${location.origin}/api/reactive-cache.json`;
40
+ /**
41
+ * Emits a {@link StorageEvent} on the local context to
42
+ * match the broadcast event.
43
+ */
44
+ function emitStorageEvent(event) {
45
+ window.dispatchEvent(new CustomEvent('storage', {
46
+ detail: event
47
+ }));
48
+ }
49
+
50
+ /**
51
+ * Updates the cache and re-emits the event on the local context as a StorageEvent
52
+ * This is necessary to trigger the reactive updates in the current tab.
53
+ */
54
+ function handleCacheEvent(event) {
55
+ const {
56
+ data
57
+ } = event;
58
+ const cache = CACHES.get(data.storageArea);
59
+ // if we don't have an instance of the cache in this context
60
+ // then we don't need to do anything since there are no reactive
61
+ // subscribers to update
62
+ if (!cache) {
63
+ return;
64
+ }
65
+
66
+ // a StorageEvent with a key of null
67
+ // is a signal that the entire cache should be cleared.
68
+ if (data.key === null) {
69
+ cache._data = new Map();
70
+ emitStorageEvent({
71
+ storageArea: cache,
72
+ key: null,
73
+ oldValue: null,
74
+ newValue: null
75
+ });
76
+ return;
77
+ }
78
+ if (data.newValue === null) {
79
+ // remove the key from the cache
80
+ cache._data.delete(data.key);
81
+ } else {
82
+ // update the cache with the new value
83
+ cache._data.set(data.key, data.newValue);
84
+ }
85
+
86
+ // emit a StorageEvent to trigger reactive updates in this context
87
+ emitStorageEvent({
88
+ storageArea: cache,
89
+ key: data.key,
90
+ oldValue: data.oldValue,
91
+ newValue: data.newValue
92
+ });
93
+ }
94
+
95
+ // subscribe to all storage events from other contexts to
96
+ // keep caches in sync
97
+ StorageEvents.addEventListener('message', handleCacheEvent);
98
+
99
+ /**
100
+ * A reactive interface for json stored in the browser [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) API.
101
+ *
102
+ * This is a good option for larger data sets than can be efficiently stored in localStorage
103
+ * but should not be used as a permanent DB or storage solution.
104
+ */
105
+ class CacheStorage {
106
+ #cache;
107
+ #cacheId;
108
+ #inititalization;
109
+ _data = new Map();
110
+ _nextUpdate = null;
111
+ _bufferedEvents = [];
112
+ async #initialize() {
113
+ const cache = await caches.open(this.#cacheId);
114
+ this.#cache = cache;
115
+
116
+ // Load all existing entries into memory
117
+ this._data = await deserializeFromCache(cache);
118
+ return this;
119
+ }
120
+ constructor(cacheId) {
121
+ this.#cacheId = cacheId;
122
+ this.#cache = null;
123
+ this.#inititalization = this.#initialize();
124
+ }
125
+ get length() {
126
+ return this._data.size;
127
+ }
128
+ #update(key, oldValue, newValue) {
129
+ const cache = this.#cache;
130
+ if (!cache) {
131
+ throw new Error('Cache not initialized');
132
+ }
133
+ this._bufferedEvents.push({
134
+ storageArea: this.#cacheId,
135
+ key: key,
136
+ oldValue: oldValue,
137
+ newValue: newValue
138
+ });
139
+ if (!this._nextUpdate) {
140
+ this._nextUpdate = window.setTimeout(() => {
141
+ const events = this._bufferedEvents;
142
+ this._bufferedEvents = [];
143
+ this._nextUpdate = null;
144
+ void serializeToCache(cache, this._data);
145
+ for (const event of events) {
146
+ StorageEvents.postMessage(event);
147
+ }
148
+ }, 0);
149
+ }
150
+ }
151
+ clear() {
152
+ this._data = new Map();
153
+ this.#update(null, null, null);
154
+ }
155
+ getItem(key) {
156
+ return this._data.get(key) ?? null;
157
+ }
158
+ key(index) {
159
+ let i = 0;
160
+ for (const value of this._data.keys()) {
161
+ if (i === index) {
162
+ return value;
163
+ }
164
+ i++;
165
+ }
166
+ return null;
167
+ }
168
+ removeItem(key) {
169
+ if (this._data.has(key)) {
170
+ const oldValue = this._data.get(key) ?? null;
171
+ this._data.delete(key);
172
+ this.#update(key, oldValue, null);
173
+ }
174
+ }
175
+ setItem(key, value) {
176
+ const oldValue = this._data.get(key) ?? null;
177
+ if (oldValue === value) {
178
+ return;
179
+ }
180
+ this._data.set(key, value);
181
+ this.#update(key, oldValue, value);
182
+ }
183
+
184
+ /**
185
+ * Get the singleton CacheStorage instance.
186
+ *
187
+ */
188
+ static get(cacheId = DEFAULT_CACHE_ID) {
189
+ let cache = CACHES.get(cacheId);
190
+ if (!cache) {
191
+ cache = new CacheStorage(cacheId);
192
+ CACHES.set(cacheId, cache);
193
+ }
194
+ return cache.#inititalization;
195
+ }
196
+ static expectCache(cacheId = DEFAULT_CACHE_ID) {
197
+ const cache = CACHES.get(cacheId);
198
+ if (!cache) {
199
+ throw new Error(`Cache with id ${cacheId} not found. Make sure to call CacheStorage.get(${cacheId}) first.`);
200
+ }
201
+ return cache;
202
+ }
203
+
204
+ /**
205
+ * Returns the IDs of all {@link CacheStorage} instances that have been
206
+ * opened in this context via {@link CacheStorage.get}.
207
+ */
208
+ static getAllCacheIds() {
209
+ return Array.from(CACHES.keys());
210
+ }
211
+ }
212
+ function serializeToCache(cache, data) {
213
+ const entries = Array.from(data.entries());
214
+ const blob = JSON.stringify({
215
+ version: 1,
216
+ data: entries
217
+ });
218
+ const response = new Response(blob);
219
+ return cache.put(CACHE_URL, response);
220
+ }
221
+ function deserializeFromCache(cache) {
222
+ return cache.match(CACHE_URL).then(response => {
223
+ if (!response) {
224
+ return new Map();
225
+ }
226
+ return response.json().then(json => {
227
+ if (json.version !== 1) {
228
+ throw new Error(`Unsupported cache version: ${json.version}`);
229
+ }
230
+ return new Map(json.data);
231
+ });
232
+ });
233
+ }
234
+
235
+ let localStorageInstance = null;
236
+ let sessionStorageInstance = null;
237
+ let localStorageOptions = {};
238
+ let sessionStorageOptions = {};
239
+
240
+ // This pattern enables us to have a singleton service-like
241
+ // instance that also functions as a global singleton that
242
+ // can be imported and used outside of Ember's DI system.
243
+ /**
244
+ * Retrieves the singleton instance of the LocalStorage service.
245
+ *
246
+ * If the instance does not already exist, it is created.
247
+ */
248
+ function getLocalStorage() {
249
+ if (!localStorageInstance) {
250
+ localStorageInstance = new ReactiveStorage(localStorage, localStorageOptions);
251
+ }
252
+ return localStorageInstance;
253
+ }
254
+ function getSessionStorage() {
255
+ if (!sessionStorageInstance) {
256
+ sessionStorageInstance = new ReactiveStorage(sessionStorage, sessionStorageOptions);
257
+ }
258
+ return sessionStorageInstance;
259
+ }
260
+ const CacheStorages = new Map();
261
+ function getCacheStorage(namespace = null) {
262
+ const key = namespace ?? DEFAULT_CACHE_ID;
263
+ let cache = CacheStorages.get(key);
264
+ if (!cache) {
265
+ const storage = CacheStorage.expectCache(key);
266
+ cache = new ReactiveStorage(storage);
267
+ CacheStorages.set(key, cache);
268
+ }
269
+ return cache;
270
+ }
271
+
272
+ /**
273
+ * Configure options for the localStorage singleton.
274
+ * Must be called before getLocalStorage() is first invoked.
275
+ */
276
+ function configureLocalStorage(options) {
277
+ localStorageOptions = options;
278
+ }
279
+
280
+ /**
281
+ * Configure options for the sessionStorage singleton.
282
+ * Must be called before getSessionStorage() is first invoked.
283
+ */
284
+ function configureSessionStorage(options) {
285
+ sessionStorageOptions = options;
286
+ }
287
+
288
+ /**
289
+ * Check if an error is a quota exceeded error
290
+ */
291
+ function isQuotaExceededError(error) {
292
+ if (error instanceof DOMException) {
293
+ // Most browsers
294
+ if (error.name === 'QuotaExceededError') return true;
295
+ // Firefox
296
+ if (error.name === 'NS_ERROR_DOM_QUOTA_REACHED') return true;
297
+ }
298
+ return false;
299
+ }
300
+
301
+ /**
302
+ * Check if an error indicates storage is unavailable (private browsing, etc.)
303
+ */
304
+ function isStorageUnavailableError(error) {
305
+ if (error instanceof DOMException) {
306
+ // Safari private browsing, some security restrictions
307
+ if (error.name === 'SecurityError') return true;
308
+ // Some browsers throw this in private mode
309
+ if (error.name === 'InvalidStateError') return true;
310
+ }
311
+ return false;
312
+ }
313
+ /**
314
+ * A reactive wrapper around the Web Storage API (localStorage/sessionStorage)
315
+ * that provides signal-based access to storage items and length.
316
+ *
317
+ * Will automatically update when storage events occur in other tabs/windows.
318
+ */
319
+ class ReactiveStorage {
320
+ _storage;
321
+ _options;
322
+ _memoryOnly = false;
323
+ _values = {};
324
+ _effects = new Map();
325
+ setEffect(key, fn) {
326
+ this._effects.set(key, fn);
327
+ }
328
+ constructor(storage, options = {}) {
329
+ this._storage = storage;
330
+ this._options = options;
331
+ if (!(storage instanceof CacheStorage)) {
332
+ // Test if storage is accessible
333
+ try {
334
+ const testKey = '__storage_test__';
335
+ storage.setItem(testKey, testKey);
336
+ storage.removeItem(testKey);
337
+ this._length = storage.length;
338
+ } catch (error) {
339
+ if (options.fallbackToMemory && isStorageUnavailableError(error)) {
340
+ this._memoryOnly = true;
341
+ this._length = 0;
342
+ } else {
343
+ throw error;
344
+ }
345
+ }
346
+ }
347
+
348
+ // bind to localStorage events to trigger reactivity
349
+ if (!this._memoryOnly) {
350
+ window.addEventListener('storage', event => {
351
+ const data = 'detail' in event ? event.detail : event;
352
+
353
+ // Only react to changes in the same storage area
354
+ if (data.storageArea === storage) {
355
+ this._values[data.key] = data.newValue;
356
+ this._signals.notify(data.key);
357
+ this._length = this._storage.length;
358
+ const effect = this._effects.get(data.key);
359
+ if (effect) {
360
+ effect(data);
361
+ }
362
+ }
363
+ });
364
+ }
365
+ }
366
+
367
+ /**
368
+ * Reactive access to the number of keys in Storage
369
+ */
370
+ get length() {
371
+ return this._length;
372
+ }
373
+
374
+ /**
375
+ * Non-reactive way to peek the current value of a key in Storage
376
+ */
377
+ peekItem(key) {
378
+ const keyStr = String(key);
379
+ if (this._memoryOnly) {
380
+ return this._values[keyStr] ?? null;
381
+ }
382
+ const value = this._values[keyStr];
383
+ if (value !== undefined) {
384
+ return value;
385
+ }
386
+
387
+ // Not yet loaded, fetch from storage, but don't track access
388
+ const item = this._storage.getItem(keyStr);
389
+ // this._values[keyStr] = item;
390
+ return item;
391
+ }
392
+
393
+ /**
394
+ * Reactive access to Storage contents
395
+ */
396
+ getItem(key) {
397
+ const keyStr = String(key);
398
+ if (this._memoryOnly) {
399
+ this._signals.subscribe(keyStr);
400
+ return this._values[keyStr] ?? null;
401
+ }
402
+ const value = this._values[keyStr];
403
+ if (value !== undefined) {
404
+ this._signals.subscribe(keyStr);
405
+ return value;
406
+ }
407
+
408
+ // Not yet loaded, fetch from storage
409
+ const item = this._storage.getItem(keyStr);
410
+ this._values[keyStr] = item;
411
+ this._signals.subscribe(keyStr);
412
+ return item;
413
+ }
414
+
415
+ /**
416
+ * Set a value in Storage, triggering reactivity
417
+ */
418
+ setItem(key, value) {
419
+
420
+ // Coerce to strings to match Storage API behavior
421
+ const keyStr = String(key);
422
+ const valueStr = String(value);
423
+ const current = this._values[keyStr];
424
+ if (current === valueStr) {
425
+ // No change
426
+ return;
427
+ }
428
+
429
+ // console.trace(`set field ${key} => ${String(value)}`);
430
+
431
+ if (this._memoryOnly) {
432
+ const currentValue = this._values[keyStr];
433
+ if (currentValue === null || currentValue === undefined) {
434
+ this._length += 1;
435
+ }
436
+ this._values[keyStr] = valueStr;
437
+ this._signals.notify(keyStr);
438
+ return;
439
+ }
440
+ try {
441
+ this._storage.setItem(keyStr, valueStr);
442
+ this._values[keyStr] = valueStr;
443
+ this._signals.notify(keyStr);
444
+ this._length = this._storage.length;
445
+ } catch (error) {
446
+ if (isQuotaExceededError(error)) {
447
+ if (this._options.updateOnQuotaExceeded) {
448
+ // Update signal state even though write failed
449
+ this._values[keyStr] = valueStr;
450
+ this._signals.notify(keyStr);
451
+ }
452
+ if (this._options.onQuotaExceeded) {
453
+ const result = this._options.onQuotaExceeded(keyStr, valueStr);
454
+ const retry = result instanceof Promise ? false : result;
455
+ if (retry) {
456
+ // Retry the write after callback freed space
457
+ this._storage.setItem(keyStr, valueStr);
458
+ this._length = this._storage.length;
459
+ }
460
+ } else {
461
+ throw error;
462
+ }
463
+ } else {
464
+ throw error;
465
+ }
466
+ }
467
+ }
468
+
469
+ /**
470
+ * Remove a value from Storage, triggering reactivity
471
+ */
472
+ removeItem(key) {
473
+ const keyStr = String(key);
474
+ if (this._memoryOnly) {
475
+ const value = this._values[keyStr];
476
+ if (value !== null && value !== undefined) {
477
+ this._length -= 1;
478
+ }
479
+ this._values[keyStr] = null;
480
+ return;
481
+ }
482
+ this._storage.removeItem(keyStr);
483
+ this._values[keyStr] = null;
484
+ this._signals.notify(keyStr);
485
+ this._length = this._storage.length;
486
+ }
487
+
488
+ /**
489
+ * Clears all keys from Storage, triggering reactivity
490
+ */
491
+ clear() {
492
+ this._values = {};
493
+ this._signals.clear();
494
+ this._length = 0;
495
+ if (this._memoryOnly) {
496
+ return;
497
+ }
498
+ this._storage.clear();
499
+ }
500
+
501
+ /**
502
+ * Reactive access to the key at the given index
503
+ */
504
+ key(index) {
505
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
506
+ this._length; // track length access
507
+
508
+ if (this._memoryOnly) {
509
+ const keys = Object.keys(this._values).filter(k => this._values[k] !== null);
510
+ return keys[index] ?? null;
511
+ }
512
+ return this._storage.key(index);
513
+ }
514
+ }
515
+ defineSignal$1(ReactiveStorage.prototype, '_length', 0);
516
+ defineSignal$1(ReactiveStorage.prototype, '_signals', makeInitializer(() => new SignalMap()));
517
+
518
+ /**
519
+ * Configuration options for fields that are also query parameters
520
+ */
521
+
522
+ /**
523
+ * Metadata attached to persisted resource classes
524
+ */
525
+ /**
526
+ * A function which generates a unique primary-key
527
+ * string for a given LocalResource or SessionResource
528
+ * instance.
529
+ *
530
+ * Use functions when you want to create more than
531
+ * one instance of a resource type, each with its own
532
+ * persisted data.
533
+ */
534
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
535
+ /**
536
+ * Symbol used to store persistence metadata on classes
537
+ */
538
+ const PERSISTED_RESOURCE_META = Symbol('StorageResourceMeta');
539
+
540
+ /**
541
+ * Setup persisted resource metadata on target
542
+ * if not already present
543
+ *
544
+ * @private
545
+ */
546
+ function initMeta(target) {
547
+ let meta = target[PERSISTED_RESOURCE_META];
548
+ if (!meta) {
549
+ meta = {
550
+ id: '',
551
+ namespace: null,
552
+ pkFn: null,
553
+ type: '',
554
+ fields: new Map(),
555
+ initializers: new Map(),
556
+ paramConfigs: null,
557
+ paramCompanion: null,
558
+ instances: null,
559
+ typeOverrides: null
560
+ };
561
+ target[PERSISTED_RESOURCE_META] = meta;
562
+ }
563
+ return meta;
564
+ }
565
+ function useMeta(meta, instance) {
566
+ // If a primary key function is defined, use it to generate
567
+ // a unique ID for this instance
568
+ if (meta.id === '' && meta.pkFn) {
569
+ // we are in an instance context
570
+ meta.instances = meta.instances ?? new WeakMap();
571
+ let instanceMeta = meta.instances.get(instance);
572
+ if (!instanceMeta) {
573
+ instanceMeta = {
574
+ ...meta
575
+ };
576
+ const pk = meta.pkFn(instance);
577
+ instanceMeta.id = pk;
578
+ meta.instances.set(instance, instanceMeta);
579
+ }
580
+ return instanceMeta;
581
+ }
582
+ return meta;
583
+ }
584
+
585
+ /**
586
+ * Load storage field
587
+ */
588
+ function getField(instance, meta, key, overrideType) {
589
+ const type = overrideType || meta.type;
590
+ const storage = getStorage(type, meta.namespace);
591
+ const stored = storage.getItem(keyFor(meta, key));
592
+ if (stored) {
593
+ return JSON.parse(stored);
594
+ } else {
595
+ // No stored value, check for initializer
596
+ const initializer = meta.initializers.get(key);
597
+ if (initializer) {
598
+ const value = initializer.call(instance);
599
+ if (value !== undefined) {
600
+ return value;
601
+ }
602
+ }
603
+ }
604
+ return null;
605
+ }
606
+ function keyFor(meta, key) {
607
+ return `persisted:${meta.id}:${key}`;
608
+ }
609
+ function getStorage(type, namespace) {
610
+ if (type === 'local-resource') {
611
+ return getLocalStorage();
612
+ } else if (type === 'session-resource') {
613
+ return getSessionStorage();
614
+ } else {
615
+ return getCacheStorage(namespace);
616
+ }
617
+ }
618
+
619
+ /**
620
+ * Update storage field
621
+ */
622
+ function setField(meta, key, value, overrideType) {
623
+ const type = overrideType || meta.type;
624
+ const storage = getStorage(type, meta.namespace);
625
+ storage.setItem(keyFor(meta, key), JSON.stringify(value));
626
+ }
627
+ function getResourceMeta(target) {
628
+ const meta = target[PERSISTED_RESOURCE_META];
629
+ return meta;
630
+ }
631
+ function _createStorageResource(id, type, namespace) {
632
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
633
+ return function (target) {
634
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
635
+ const meta = getResourceMeta(target.prototype);
636
+ meta.id = typeof id === 'string' ? id : '';
637
+ meta.pkFn = typeof id === 'function' ? id : null;
638
+ meta.type = type;
639
+ meta.namespace = namespace;
640
+ if (meta.pkFn !== null) {
641
+ // for dynamic instances, we install effects only once
642
+ // fields have been defined and a key created for the
643
+ // instance.
644
+ // for this, we defer effect installation until
645
+ // until object instantiation by wrapping the constructor
646
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
647
+ const originalConstructor = target.prototype.constructor;
648
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
649
+ target.prototype.constructor = function DynamicStorageInitializer(...args) {
650
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call, new-cap
651
+ const instance = new originalConstructor(...args);
652
+ void installEffectsForDynamicInstance(meta, instance);
653
+ return instance;
654
+ };
655
+ }
656
+ if (!('toJSON' in target.prototype)) {
657
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
658
+ target.prototype.toJSON = function () {
659
+ const instanceMeta = useMeta(meta, this);
660
+ // we only serialize the id and any localStorage fields, not sessionStorage fields.
661
+ const isLocalResource = meta.type === 'local-resource' || meta.type === 'cache-resource';
662
+ const fields = isLocalResource ? new Set(meta.fields.keys()) : new Set();
663
+ for (const [key, overrideType] of meta.typeOverrides?.entries() ?? []) {
664
+ if (overrideType === 'local-storage') {
665
+ fields.add(key);
666
+ } else if (overrideType === 'session-storage' && isLocalResource) {
667
+ fields.delete(key);
668
+ }
669
+ }
670
+ let idKey = '$key';
671
+ while (fields.has(idKey)) {
672
+ idKey = `_${idKey}`;
673
+ }
674
+ const data = {
675
+ [idKey]: instanceMeta.id
676
+ };
677
+ for (const key of fields) {
678
+ const value = getField(this, instanceMeta, key, meta.typeOverrides?.get(key) ?? null);
679
+ if (value !== null) {
680
+ data[key] = value;
681
+ }
682
+ }
683
+ return data;
684
+ };
685
+ }
686
+ };
687
+ }
688
+ async function installEffectsForDynamicInstance(meta, instance) {
689
+ await Promise.resolve();
690
+ for (const [key, value] of meta.fields.entries()) {
691
+ if (typeof value === 'function') {
692
+ void installEffect(useMeta(meta, instance), key);
693
+ }
694
+ }
695
+ }
696
+
697
+ /**
698
+ * Returns the descriptor but is cast
699
+ * to void to satisfy Typescript's incorrect
700
+ * typing for legacy decorators.
701
+ */
702
+ function setupField(target, key, orgDesc, type) {
703
+ const meta = initMeta(target);
704
+ meta.fields.set(key, null);
705
+ const overrideType = type === 'local' ? 'local-storage' : type === 'session' ? 'session-storage' : type === 'cache' ? 'cache-storage' : null;
706
+ if (overrideType) {
707
+ meta.typeOverrides = meta.typeOverrides || new Map();
708
+ meta.typeOverrides.set(key, overrideType);
709
+ }
710
+ // @ts-expect-error initializer does exist
711
+ const initializer = orgDesc?.initializer || null;
712
+ meta.initializers.set(key, initializer);
713
+ const desc = {
714
+ configurable: true,
715
+ enumerable: true,
716
+ get() {
717
+ return getField(this, useMeta(meta, this), key, overrideType);
718
+ },
719
+ set(value) {
720
+ setField(useMeta(meta, this), key, value, overrideType);
721
+ }
722
+ };
723
+ return memoized(target, key, desc);
724
+ }
725
+ async function installEffect(meta, key) {
726
+ await Promise.resolve();
727
+ const effect = meta.fields.get(key);
728
+ const storage = getStorage(meta.type, meta.namespace);
729
+ storage.setEffect(keyFor(meta, key), event => {
730
+ const oldValue = event.oldValue ? JSON.parse(event.oldValue) : null;
731
+ const newValue = event.newValue ? JSON.parse(event.newValue) : null;
732
+ effect({
733
+ key,
734
+ from: oldValue,
735
+ to: newValue
736
+ });
737
+ });
738
+ }
739
+
740
+ /**
741
+ * Get or create the param companion object for a StorageResource instance.
742
+ *
743
+ * The companion object contains URL-serialized versions of all @param decorated fields.
744
+ * Each param field gets a corresponding property on the companion that:
745
+ * - Reads: Serializes the storage value to a URL string (returns null if not active)
746
+ * - Writes: Deserializes the URL string and updates the storage value
747
+ *
748
+ * The companion object is reactive using trackedObject, ensuring that changes
749
+ * to the underlying storage fields trigger updates in the query param system.
750
+ *
751
+ * This companion object is what QPRoute will bind to for URL query params.
752
+ *
753
+ * @private
754
+ * @param instance - The StorageResource instance
755
+ * @param groupControlMap - Optional map of fieldName -> controlFieldName for grouped params
756
+ * @returns The companion object with serialized param properties
757
+ */
758
+ function getParamCompanion(instance, groupControlMap) {
759
+ const meta = getResourceMeta(instance);
760
+ const instanceMeta = useMeta(meta, instance);
761
+
762
+ // Return existing companion if already created
763
+ if (instanceMeta.paramCompanion) {
764
+ return instanceMeta.paramCompanion;
765
+ }
766
+
767
+ // Create new companion object
768
+ const companion = {};
769
+ instanceMeta.paramCompanion = companion;
770
+
771
+ // Install a property for each param
772
+ const paramConfigs = instanceMeta.paramConfigs;
773
+ if (!paramConfigs || paramConfigs.size === 0) {
774
+ return companion;
775
+ }
776
+ for (const [fieldName, config] of paramConfigs.entries()) {
777
+ const controlFieldName = groupControlMap?.[fieldName];
778
+ createParamField(companion, fieldName, config, controlFieldName, instance, meta);
779
+ }
780
+ return companion;
781
+ }
782
+ function isActiveGroupParam(companion, controlFieldName) {
783
+ // if the url value for the control param is `null` we are inactive
784
+ const controlValue = companion[controlFieldName];
785
+ return controlValue !== null;
786
+ }
787
+
788
+ /**
789
+ * The default value of a param is determined by:
790
+ * - the getDefault() function if provided, and its return is not undefined
791
+ * - the initializer value provided to the field, if not undefined
792
+ * - null otherwise
793
+ *
794
+ * The value is the deserialized form (i.e., the local storage value type)
795
+ */
796
+ function getParamFieldDefaultValue(meta, config, fieldName, instance) {
797
+ const defaultValue = config.getDefault?.(instance);
798
+ if (defaultValue !== undefined) {
799
+ return defaultValue;
800
+ }
801
+ const initializer = meta.initializers.get(fieldName);
802
+ if (initializer) {
803
+ const initValue = initializer.call(instance);
804
+ if (initValue !== undefined) {
805
+ return initValue;
806
+ }
807
+ }
808
+ return null;
809
+ }
810
+ function createParamField(companion, fieldName, config, controlFieldName, instance, meta) {
811
+ const desc = {
812
+ configurable: true,
813
+ enumerable: true,
814
+ get() {
815
+ // Mark as initialized on first access, but continue with normal logic
816
+ if (!config.initialized) {
817
+ config.initialized = true;
818
+ return null;
819
+ }
820
+
821
+ // Next, check if this param is part of a group and if its control param is active
822
+ // If not active, return null to indicate inactive state
823
+ if (controlFieldName && !isActiveGroupParam(companion, controlFieldName)) {
824
+ return null;
825
+ }
826
+
827
+ // Next, check if the currently stored value is the default
828
+ // value. If so, return null to indicate inactive state
829
+ const defaultValue = getParamFieldDefaultValue(meta, config, fieldName, instance);
830
+ const rawValue = instance[fieldName];
831
+ if (rawValue === defaultValue) {
832
+ return null;
833
+ }
834
+
835
+ // Serialize to URL format
836
+ const serialized = config.serialize(rawValue, instance);
837
+
838
+ // Return null if serialization returns empty, indicating no URL representation
839
+ if (!serialized) {
840
+ return null;
841
+ }
842
+ return serialized;
843
+ },
844
+ set(urlValue) {
845
+ /**
846
+ * set will never come from anything except URL deserialization.
847
+ * which is managed by Ember.
848
+ *
849
+ * Our job is to not unnecessarily update the storage resource
850
+ * so we need to run the compare function to see if the
851
+ * incoming URL value is different from the existing local value.
852
+ */
853
+
854
+ // If null or empty, skip deserialization
855
+ if (!urlValue) {
856
+ return;
857
+ }
858
+ const rawValue = instance[fieldName];
859
+
860
+ // Compare incoming URL value with existing local value
861
+ const rawValueSerialized = config.serialize(rawValue, instance);
862
+ if (rawValueSerialized === urlValue) {
863
+ // No change
864
+ return;
865
+ }
866
+
867
+ // Deserialize from URL format
868
+ const newRawValue = config.deserialize(urlValue, instance);
869
+
870
+ // Update the storage resource
871
+ instance[fieldName] = newRawValue;
872
+ }
873
+ };
874
+ memoized(companion, fieldName, desc);
875
+ Object.defineProperty(companion, fieldName, desc);
876
+ }
877
+
878
+ /**
879
+ * Decorator which transforms a class into a StorageResource
880
+ * persisted in localStorage.
881
+ *
882
+ * LocalResources must either be singletons or expect all instances
883
+ * to share state unless a primary key function is provided.
884
+ *
885
+ * When a primary key function is provided, each instance
886
+ * will have its own persisted data based on the key generated
887
+ * by the function.
888
+ *
889
+ * The function will be called once per instance during
890
+ * initialization to determine the unique ID for that instance.
891
+ */
892
+ function LocalResource(id) {
893
+ return _createStorageResource(id, 'local-resource', null);
894
+ }
895
+
896
+ /**
897
+ * Decorator which transforms a class into a StorageResource
898
+ * persisted in sessionStorage.
899
+ *
900
+ * SessionResources must either be singletons or expect all instances
901
+ * to share state unless a primary key function is provided.
902
+ *
903
+ * When a primary key function is provided, each instance
904
+ * will have its own persisted data based on the key generated
905
+ * by the function.
906
+ *
907
+ * The function will be called once per instance during
908
+ * initialization to determine the unique ID for that instance.
909
+ */
910
+ function SessionResource(id) {
911
+ return _createStorageResource(id, 'session-resource', null);
912
+ }
913
+
914
+ /**
915
+ * Decorator which transforms a class into a StorageResource
916
+ * persisted via the [Cache API](https://developer.mozilla.org/en-US/docs/Web/API/Cache).
917
+ * api and shared across all tabs/windows under the same origin.
918
+ *
919
+ * CacheResources must either be singletons or expect all instances
920
+ * to share state unless a primary key function is provided.
921
+ *
922
+ * When a primary key function is provided, each instance
923
+ * will have its own persisted data based on the key generated
924
+ * by the function.
925
+ *
926
+ * The function will be called once per instance during
927
+ * initialization to determine the unique ID for that instance.
928
+ *
929
+ * All object cached in the same `namespace` share the namespace's storage context,
930
+ * so partitioning can be achieved by using different namespaces for different groups
931
+ * of data.
932
+ */
933
+ function CacheResource(id, namespace = null) {
934
+ return _createStorageResource(id, 'cache-resource', namespace);
935
+ }
936
+
937
+ /**
938
+ * Decorator which marks a property as a field on
939
+ * a LocalResource or SessionResource
940
+ *
941
+ * The field's value will be initialized from the persisted resource data
942
+ * if available, falling back to the property's default value otherwise.
943
+ *
944
+ * Fields can be of any type that is serializable to and restorable from JSON,
945
+ * but complex types (like objects or arrays) should be handled with care to avoid
946
+ * unintended mutations or reactivity issues.
947
+ *
948
+ * By default, fields are persisted in the storage type defined by the resource decorator
949
+ * (@LocalResource or @SessionResource). However, you can override this behavior
950
+ * by passing 'local' or 'session' as an argument to the decorator.
951
+ *
952
+ * ---
953
+ *
954
+ * **Example:**
955
+ *
956
+ * ```ts
957
+ * @LocalResource('user-settings')
958
+ * class UserSettings {
959
+ * @field
960
+ * theme: 'light' | 'dark' = 'light';
961
+ *
962
+ * @field('session')
963
+ * sessionToken: string | null = null;
964
+ * }
965
+ * ```
966
+ *
967
+ */
968
+
969
+ function field(...args) {
970
+ if (args.length === 0) {
971
+ return setupField;
972
+ }
973
+ if (args.length === 1) {
974
+ const type = args[0];
975
+ return (target, key, desc) => setupField(target, key, desc, type);
976
+ }
977
+ if (args.length === 2 || args.length === 3) {
978
+ const [target, key, descriptor] = args;
979
+ return setupField(target, key, descriptor);
980
+ }
981
+ }
982
+ function input(type) {
983
+ return function (target, key, desc) {
984
+ // eslint-disable-next-line @typescript-eslint/unbound-method
985
+ const originalSet = desc.set;
986
+ desc.set = function (value) {
987
+ switch (type) {
988
+ case 'number':
989
+ value = Number(value);
990
+ break;
991
+ case 'boolean':
992
+ value = value === 'false' || value === '' || value === '0' || value === 'undefined' || value === 'null' || value === false || value === 0 || value === null || value === undefined ? 0 : 1;
993
+ break;
994
+ }
995
+ originalSet.call(this, value);
996
+ };
997
+ return desc;
998
+ };
999
+ }
1000
+
1001
+ /**
1002
+ * Effects are fields that run a side-effecting function.
1003
+ *
1004
+ * Effects are intended to enable synchronizing get states between
1005
+ * tabs or windows that result in needing to synchronize other
1006
+ * non-reactive state.
1007
+ *
1008
+ * For example, when a user selects a light/dark mode theme preference
1009
+ * that differs from the system preference, effects can be used to trigger
1010
+ * DOM updates on the documentElement necessary to ensure its state is
1011
+ * consistent with the persisted resource state and reactive application state.
1012
+ *
1013
+ * To do this without an effect would require either setting up your own
1014
+ * storage event listeners or consuming the reactive state of the property
1015
+ * in another effect-like API such as an Ember modifier or React useEffect,
1016
+ *
1017
+ * Effects *only* run when the stored value changes due to storage events
1018
+ * emitted from other tabs or windows. They do not run when the property
1019
+ * is updated in the same context.
1020
+ *
1021
+ * ---
1022
+ *
1023
+ * **Example:**
1024
+ *
1025
+ * ```ts
1026
+ * @LocalResource('user-preferences')
1027
+ * class UserPreferences {
1028
+ * @effect(syncThemeToDOM)
1029
+ * explicitThemePreference: 'light' | 'dark' | null = null;
1030
+ *
1031
+ * @matchMedia('(prefers-color-scheme: dark)')
1032
+ * systemPrefersDarkMode: boolean = false;
1033
+ * }
1034
+ *
1035
+ * function syncThemeToDOM(update: ValueTransition<'light' | 'dark' | null>): void {
1036
+ * const newTheme = update.to;
1037
+ * document.documentElement.style.colorScheme = newTheme ?? 'light dark';
1038
+ *
1039
+ * if (newTheme === 'dark') {
1040
+ * document.documentElement.classList.add('dark-theme');
1041
+ * document.documentElement.classList.remove('light-theme');
1042
+ * } else if (newTheme === 'light') {
1043
+ * document.documentElement.classList.add('light-theme');
1044
+ * document.documentElement.classList.remove('dark-theme');
1045
+ * } else {
1046
+ * document.documentElement.classList.remove('light-theme');
1047
+ * document.documentElement.classList.remove('dark-theme');
1048
+ * }
1049
+ * }
1050
+ * ```
1051
+ */
1052
+ function effect(fn, type) {
1053
+ const overrideType = type === 'local' ? 'local-storage' : type === 'session' ? 'session-storage' : null;
1054
+ return function effectField(target, key, _descriptor) {
1055
+ const meta = initMeta(target);
1056
+ meta.fields.set(key, fn);
1057
+
1058
+ // only install the effect if we are not in a primary-keyed instance context
1059
+ if (meta.pkFn === null) void installEffect(meta, key);
1060
+ return {
1061
+ configurable: true,
1062
+ enumerable: true,
1063
+ get() {
1064
+ return getField(this, useMeta(meta, this), key, overrideType);
1065
+ },
1066
+ set(value) {
1067
+ setField(useMeta(meta, this), key, value, overrideType);
1068
+ }
1069
+ };
1070
+ };
1071
+ }
1072
+
1073
+ export { CacheResource, CacheStorage, DEFAULT_CACHE_ID, LocalResource, SessionResource, getParamCompanion as _getParamCompanion, initMeta as _initMeta, configureLocalStorage, configureSessionStorage, effect, field, getCacheStorage, getLocalStorage, getSessionStorage, input };