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