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