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