@warp-drive/experiments 0.2.7-alpha.19 → 0.2.7-alpha.20

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