@warp-drive-mirror/experiments 0.2.7-beta.0 → 0.2.7-beta.2
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/CHANGELOG.md +70 -0
- package/README.md +2 -1
- package/dist/data-worker.d.ts +35 -0
- package/dist/data-worker.d.ts.map +1 -0
- package/dist/data-worker.js +235 -339
- package/dist/data-worker.js.map +1 -0
- package/dist/document-storage-CwwTQ49-.d.ts +91 -0
- package/dist/document-storage-CwwTQ49-.d.ts.map +1 -0
- package/dist/document-storage-LM925Stz.js +270 -0
- package/dist/document-storage-LM925Stz.js.map +1 -0
- package/dist/document-storage.d.ts +2 -0
- package/dist/document-storage.js +3 -1
- package/dist/image-fetch.d.ts +60 -0
- package/dist/image-fetch.d.ts.map +1 -0
- package/dist/image-fetch.js +108 -79
- package/dist/image-fetch.js.map +1 -0
- package/dist/image-worker.d.ts +40 -0
- package/dist/image-worker.d.ts.map +1 -0
- package/dist/image-worker.js +95 -95
- package/dist/image-worker.js.map +1 -0
- package/dist/pagination.d.ts +2 -0
- package/dist/pagination.js +3 -0
- package/dist/storage.d.ts +354 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +914 -0
- package/dist/storage.js.map +1 -0
- package/dist/types-CRs-Qww6.d.ts +17 -0
- package/dist/types-CRs-Qww6.d.ts.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/pagination.js +3 -0
- package/dist/unpkg/dev/storage.js +913 -0
- 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/pagination.js +3 -0
- package/dist/unpkg/dev-deprecated/storage.js +913 -0
- 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/pagination.js +3 -0
- package/dist/unpkg/prod/storage.js +844 -0
- 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/pagination.js +3 -0
- package/dist/unpkg/prod-deprecated/storage.js +844 -0
- 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.d.ts +23 -0
- package/dist/worker-fetch.d.ts.map +1 -0
- package/dist/worker-fetch.js +116 -153
- package/dist/worker-fetch.js.map +1 -0
- package/package.json +27 -23
- 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/data-worker.d.ts +0 -2
- package/declarations/document-storage/index.d.ts +0 -90
- package/declarations/document-storage.d.ts +0 -1
- package/declarations/image-fetch.d.ts +0 -1
- 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/image-worker.d.ts +0 -1
- package/declarations/worker-fetch.d.ts +0 -1
- package/dist/index-CGCX7hY2.js +0 -349
|
@@ -1,380 +1,273 @@
|
|
|
1
|
-
import { DocumentStorage } from
|
|
2
|
-
import { assertPrivateStore } from
|
|
3
|
-
import { SkipCache } from
|
|
1
|
+
import { t as DocumentStorage } from "./document-storage-DkiEg9rY.js";
|
|
2
|
+
import { assertPrivateStore } from "@warp-drive-mirror/core/store/-private";
|
|
3
|
+
import { SkipCache } from "@warp-drive-mirror/core/types/request";
|
|
4
4
|
|
|
5
|
+
//#region src/data-worker/worker.ts
|
|
5
6
|
const WorkerScope = globalThis.SharedWorkerGlobalScope;
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
async request(event) {
|
|
92
|
-
const {
|
|
93
|
-
thread,
|
|
94
|
-
id,
|
|
95
|
-
data
|
|
96
|
-
} = event;
|
|
97
|
-
try {
|
|
98
|
-
const future = this.store.request(data);
|
|
99
|
-
this.pending.get(thread).set(id, future);
|
|
100
|
-
const result = await future;
|
|
101
|
-
this.threads.get(thread)?.postMessage({
|
|
102
|
-
type: 'success-response',
|
|
103
|
-
id,
|
|
104
|
-
thread,
|
|
105
|
-
data: prepareResponse(result)
|
|
106
|
-
});
|
|
107
|
-
} catch (error) {
|
|
108
|
-
if (isAbortError(error)) return;
|
|
109
|
-
this.threads.get(thread)?.postMessage({
|
|
110
|
-
type: 'error-response',
|
|
111
|
-
id,
|
|
112
|
-
thread,
|
|
113
|
-
data: error
|
|
114
|
-
});
|
|
115
|
-
} finally {
|
|
116
|
-
this.pending.get(thread).delete(id);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
7
|
+
var DataWorker = class {
|
|
8
|
+
constructor(UserStore, options) {
|
|
9
|
+
if (typeof window !== "undefined") return;
|
|
10
|
+
this.store = new UserStore();
|
|
11
|
+
this.threads = /* @__PURE__ */ new Map();
|
|
12
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
13
|
+
this.options = Object.assign({
|
|
14
|
+
persisted: false,
|
|
15
|
+
scope: ""
|
|
16
|
+
}, options);
|
|
17
|
+
this.isSharedWorker = WorkerScope && globalThis instanceof WorkerScope;
|
|
18
|
+
this.initialize();
|
|
19
|
+
}
|
|
20
|
+
initialize() {
|
|
21
|
+
this.store._worker = this;
|
|
22
|
+
if (this.options.persisted) this.storage = new DocumentStorage({ scope: this.options.scope });
|
|
23
|
+
if (this.isSharedWorker) globalThis.onconnect = (e) => {
|
|
24
|
+
const port = e.ports[0];
|
|
25
|
+
port.onmessage = (event) => {
|
|
26
|
+
const { type } = event.data;
|
|
27
|
+
switch (type) {
|
|
28
|
+
case "connect": this.setupThread(event.data.thread, port);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
port.start();
|
|
32
|
+
};
|
|
33
|
+
else globalThis.onmessage = (event) => {
|
|
34
|
+
const { type } = event.data;
|
|
35
|
+
switch (type) {
|
|
36
|
+
case "connect": this.setupThread(event.data.thread, event.ports[0]);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
setupThread(thread, port) {
|
|
41
|
+
this.threads.set(thread, port);
|
|
42
|
+
this.pending.set(thread, /* @__PURE__ */ new Map());
|
|
43
|
+
port.onmessage = (event) => {
|
|
44
|
+
if (event.type === "close") {
|
|
45
|
+
this.threads.delete(thread);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const { type } = event.data;
|
|
49
|
+
switch (type) {
|
|
50
|
+
case "abort":
|
|
51
|
+
this.abortRequest(event.data);
|
|
52
|
+
break;
|
|
53
|
+
case "request": this.request(prepareRequest(event.data));
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
abortRequest(event) {
|
|
58
|
+
const { thread, id } = event;
|
|
59
|
+
const future = this.pending.get(thread).get(id);
|
|
60
|
+
if (future) {
|
|
61
|
+
future.abort();
|
|
62
|
+
this.pending.get(thread).delete(id);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async request(event) {
|
|
66
|
+
const { thread, id, data } = event;
|
|
67
|
+
try {
|
|
68
|
+
const future = this.store.request(data);
|
|
69
|
+
this.pending.get(thread).set(id, future);
|
|
70
|
+
const result = await future;
|
|
71
|
+
this.threads.get(thread)?.postMessage({
|
|
72
|
+
type: "success-response",
|
|
73
|
+
id,
|
|
74
|
+
thread,
|
|
75
|
+
data: prepareResponse(result)
|
|
76
|
+
});
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (isAbortError(error)) return;
|
|
79
|
+
this.threads.get(thread)?.postMessage({
|
|
80
|
+
type: "error-response",
|
|
81
|
+
id,
|
|
82
|
+
thread,
|
|
83
|
+
data: error
|
|
84
|
+
});
|
|
85
|
+
} finally {
|
|
86
|
+
this.pending.get(thread).delete(id);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
120
90
|
function softCloneResponse(response) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
clone.url = response.url;
|
|
132
|
-
return clone;
|
|
91
|
+
if (!response) return null;
|
|
92
|
+
const clone = {};
|
|
93
|
+
if (response.headers) clone.headers = Array.from(response.headers);
|
|
94
|
+
clone.ok = response.ok;
|
|
95
|
+
clone.redirected = response.redirected;
|
|
96
|
+
clone.status = response.status;
|
|
97
|
+
clone.statusText = response.statusText;
|
|
98
|
+
clone.type = response.type;
|
|
99
|
+
clone.url = response.url;
|
|
100
|
+
return clone;
|
|
133
101
|
}
|
|
134
102
|
function isAbortError(error) {
|
|
135
|
-
|
|
103
|
+
return error instanceof Error && error.name === "AbortError";
|
|
136
104
|
}
|
|
137
105
|
function prepareResponse(result) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return newResponse;
|
|
106
|
+
return {
|
|
107
|
+
response: softCloneResponse(result.response),
|
|
108
|
+
content: result.content
|
|
109
|
+
};
|
|
143
110
|
}
|
|
144
111
|
function prepareRequest(event) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
return event;
|
|
112
|
+
if (event.data.headers) event.data.headers = new Headers(event.data.headers);
|
|
113
|
+
return event;
|
|
149
114
|
}
|
|
150
115
|
|
|
151
|
-
|
|
152
|
-
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/data-worker/utils.ts
|
|
118
|
+
const MUTATION_OPS = /* @__PURE__ */ new Set([
|
|
119
|
+
"createRecord",
|
|
120
|
+
"updateRecord",
|
|
121
|
+
"deleteRecord"
|
|
122
|
+
]);
|
|
153
123
|
/**
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
124
|
+
* In a Worker, any time we are asked to make a request, data needs to be returned.
|
|
125
|
+
* background requests are ergo no different than foreground requests.
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
158
128
|
function calcShouldFetch(store, request, hasCachedValue, identifier) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
} = request;
|
|
162
|
-
return request.op && MUTATION_OPS.has(request.op) || cacheOptions?.reload || cacheOptions?.backgroundReload || !hasCachedValue || (store.lifetimes && identifier ? store.lifetimes.isHardExpired(identifier, store) || store.lifetimes.isSoftExpired(identifier, store) : false);
|
|
129
|
+
const { cacheOptions } = request;
|
|
130
|
+
return request.op && MUTATION_OPS.has(request.op) || cacheOptions?.reload || cacheOptions?.backgroundReload || !hasCachedValue || (store.lifetimes && identifier ? store.lifetimes.isHardExpired(identifier, store) || store.lifetimes.isSoftExpired(identifier, store) : false);
|
|
163
131
|
}
|
|
164
132
|
function isMutation(request) {
|
|
165
|
-
|
|
133
|
+
return Boolean(request.op && MUTATION_OPS.has(request.op));
|
|
166
134
|
}
|
|
167
135
|
function isCacheAffecting(document) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
// a mutation combined with a 204 has no cache impact when no known records were involved
|
|
172
|
-
// a createRecord with a 201 with an empty response and no known records should similarly
|
|
173
|
-
// have no cache impact
|
|
174
|
-
|
|
175
|
-
if (document.request.op === 'createRecord' && document.response?.status === 201) {
|
|
176
|
-
return document.content ? Object.keys(document.content).length > 0 : false;
|
|
177
|
-
}
|
|
178
|
-
return document.response?.status !== 204;
|
|
136
|
+
if (!isMutation(document.request)) return true;
|
|
137
|
+
if (document.request.op === "createRecord" && document.response?.status === 201) return document.content ? Object.keys(document.content).length > 0 : false;
|
|
138
|
+
return document.response?.status !== 204;
|
|
179
139
|
}
|
|
180
140
|
function isAggregateError(error) {
|
|
181
|
-
|
|
141
|
+
return error instanceof AggregateError || error.name === "AggregateError" && Array.isArray(error.errors);
|
|
182
142
|
}
|
|
183
|
-
// TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
|
|
184
143
|
function cloneError(error) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
// copy over enumerable properties
|
|
191
|
-
Object.assign(cloned, error);
|
|
192
|
-
return cloned;
|
|
144
|
+
const cloned = isAggregateError(error) ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
|
|
145
|
+
cloned.stack = error.stack;
|
|
146
|
+
cloned.error = error.error;
|
|
147
|
+
Object.assign(cloned, error);
|
|
148
|
+
return cloned;
|
|
193
149
|
}
|
|
194
150
|
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/data-worker/cache-handler.ts
|
|
195
153
|
/**
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const CacheHandler = {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
store.cache.put(document);
|
|
218
|
-
}
|
|
219
|
-
return completeRequest(identifier, store, context, next);
|
|
220
|
-
}).catch(e => {
|
|
221
|
-
{
|
|
222
|
-
// eslint-disable-next-line no-console
|
|
223
|
-
console.log('Unable to retrieve document from persisted storage', e);
|
|
224
|
-
}
|
|
225
|
-
return completeRequest(identifier, store, context, next);
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return completeRequest(identifier, store, context, next);
|
|
230
|
-
}
|
|
231
|
-
};
|
|
154
|
+
* A simplified CacheHandler that hydrates ResourceDataDocuments from the cache
|
|
155
|
+
* with their referenced resources.
|
|
156
|
+
*
|
|
157
|
+
*/
|
|
158
|
+
const CacheHandler = { request(context, next) {
|
|
159
|
+
if (!context.request.store || context.request.cacheOptions?.[SkipCache]) return next(context.request);
|
|
160
|
+
const { store } = context.request;
|
|
161
|
+
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(context.request);
|
|
162
|
+
const peeked = identifier ? store.cache.peekRequest(identifier) : null;
|
|
163
|
+
if (identifier && !peeked) {
|
|
164
|
+
const worker = store._worker;
|
|
165
|
+
if (worker?.storage) return worker.storage.getDocument(identifier).then((document) => {
|
|
166
|
+
if (document) store.cache.put(document);
|
|
167
|
+
return completeRequest(identifier, store, context, next);
|
|
168
|
+
}).catch((e) => {
|
|
169
|
+
console.log("Unable to retrieve document from persisted storage", e);
|
|
170
|
+
return completeRequest(identifier, store, context, next);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return completeRequest(identifier, store, context, next);
|
|
174
|
+
} };
|
|
232
175
|
function completeRequest(identifier, store, context, next) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
throw new Error(`Expected a peeked request to be present`);
|
|
242
|
-
}
|
|
243
|
-
})(peeked);
|
|
244
|
-
context.setResponse(peeked.response);
|
|
245
|
-
if ('error' in peeked) {
|
|
246
|
-
throw peeked;
|
|
247
|
-
}
|
|
248
|
-
return maybeUpdateObjects(store, peeked.content);
|
|
176
|
+
const peeked = identifier ? store.cache.peekRequest(identifier) : null;
|
|
177
|
+
if (calcShouldFetch(store, context.request, !!peeked, identifier)) return fetchContentAndHydrate(next, context, identifier);
|
|
178
|
+
((test) => {
|
|
179
|
+
if (!test) throw new Error(`Expected a peeked request to be present`);
|
|
180
|
+
})(peeked);
|
|
181
|
+
context.setResponse(peeked.response);
|
|
182
|
+
if ("error" in peeked) throw peeked;
|
|
183
|
+
return maybeUpdateObjects(store, peeked.content);
|
|
249
184
|
}
|
|
250
185
|
function maybeUpdateObjects(store, document) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
} else {
|
|
262
|
-
// @ts-expect-error FIXME investigate why document.data won't accept the signature
|
|
263
|
-
const data = document.data ? store.cache.peek(document.data) : null;
|
|
264
|
-
return Object.assign({}, document, {
|
|
265
|
-
data
|
|
266
|
-
});
|
|
267
|
-
}
|
|
186
|
+
if (!document) return document;
|
|
187
|
+
if (Array.isArray(document.data)) {
|
|
188
|
+
const data = document.data.map((identifier) => {
|
|
189
|
+
return store.cache.peek(identifier);
|
|
190
|
+
});
|
|
191
|
+
return Object.assign({}, document, { data });
|
|
192
|
+
} else {
|
|
193
|
+
const data = document.data ? store.cache.peek(document.data) : null;
|
|
194
|
+
return Object.assign({}, document, { data });
|
|
195
|
+
}
|
|
268
196
|
}
|
|
269
197
|
function maybeUpdatePersistedCache(store, document, resourceDocument) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
});
|
|
279
|
-
} else if (document) {
|
|
280
|
-
void worker.storage.putDocument(document, resourceIdentifier => {
|
|
281
|
-
return store.cache.peek(resourceIdentifier);
|
|
282
|
-
});
|
|
283
|
-
}
|
|
198
|
+
const worker = store._worker;
|
|
199
|
+
if (!worker?.storage) return;
|
|
200
|
+
if (!document && resourceDocument) worker.storage.putResources(resourceDocument, (resourceIdentifier) => {
|
|
201
|
+
return store.cache.peek(resourceIdentifier);
|
|
202
|
+
});
|
|
203
|
+
else if (document) worker.storage.putDocument(document, (resourceIdentifier) => {
|
|
204
|
+
return store.cache.peek(resourceIdentifier);
|
|
205
|
+
});
|
|
284
206
|
}
|
|
285
207
|
function updateCacheForSuccess(store, request, document) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
if (response.lid) {
|
|
303
|
-
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(request);
|
|
304
|
-
const full = store.cache.peekRequest(identifier);
|
|
305
|
-
maybeUpdatePersistedCache(store, full);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
return maybeUpdateObjects(store, response);
|
|
208
|
+
let response = null;
|
|
209
|
+
if (isMutation(request)) {
|
|
210
|
+
const record = request.data?.record || request.records?.[0];
|
|
211
|
+
if (record) response = store.cache.didCommit(record, document);
|
|
212
|
+
else if (isCacheAffecting(document)) {
|
|
213
|
+
response = store.cache.put(document);
|
|
214
|
+
maybeUpdatePersistedCache(store, null, response);
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
response = store.cache.put(document);
|
|
218
|
+
if (response.lid) {
|
|
219
|
+
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(request);
|
|
220
|
+
maybeUpdatePersistedCache(store, store.cache.peekRequest(identifier));
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return maybeUpdateObjects(store, response);
|
|
309
224
|
}
|
|
310
225
|
function handleFetchSuccess(store, request, identifier, document) {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
return response;
|
|
226
|
+
let response;
|
|
227
|
+
assertPrivateStore(store);
|
|
228
|
+
store._join(() => {
|
|
229
|
+
response = updateCacheForSuccess(store, request, document);
|
|
230
|
+
});
|
|
231
|
+
if (store.lifetimes?.didRequest) store.lifetimes.didRequest(request, document.response, identifier, store);
|
|
232
|
+
return response;
|
|
320
233
|
}
|
|
321
234
|
function updateCacheForError(store, request, error) {
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
if (identifier) {
|
|
331
|
-
maybeUpdatePersistedCache(store, error);
|
|
332
|
-
}
|
|
333
|
-
return store.cache.put(error);
|
|
334
|
-
}
|
|
235
|
+
if (isMutation(request)) {
|
|
236
|
+
const errors = error && error.content && typeof error.content === "object" && "errors" in error.content && Array.isArray(error.content.errors) ? error.content.errors : void 0;
|
|
237
|
+
const record = request.data?.record || request.records?.[0];
|
|
238
|
+
store.cache.commitWasRejected(record, errors);
|
|
239
|
+
} else {
|
|
240
|
+
if (store.cacheKeyManager.getOrCreateDocumentIdentifier(request)) maybeUpdatePersistedCache(store, error);
|
|
241
|
+
return store.cache.put(error);
|
|
242
|
+
}
|
|
335
243
|
}
|
|
336
244
|
function handleFetchError(store, request, identifier, error) {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
if (isMutation(request)) {
|
|
349
|
-
throw error;
|
|
350
|
-
}
|
|
351
|
-
const newError = cloneError(error);
|
|
352
|
-
newError.content = response;
|
|
353
|
-
throw newError;
|
|
245
|
+
if (request.signal?.aborted) throw error;
|
|
246
|
+
assertPrivateStore(store);
|
|
247
|
+
let response;
|
|
248
|
+
store._join(() => {
|
|
249
|
+
response = updateCacheForError(store, request, error);
|
|
250
|
+
});
|
|
251
|
+
if (identifier && store.lifetimes?.didRequest) store.lifetimes.didRequest(request, error.response, identifier, store);
|
|
252
|
+
if (isMutation(request)) throw error;
|
|
253
|
+
const newError = cloneError(error);
|
|
254
|
+
newError.content = response;
|
|
255
|
+
throw newError;
|
|
354
256
|
}
|
|
355
257
|
function fetchContentAndHydrate(next, context, identifier) {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
throw new Error(`Expected to receive a list of records included in the ${request.op} request`);
|
|
368
|
-
}
|
|
369
|
-
})(record);
|
|
370
|
-
if (record) {
|
|
371
|
-
store.cache.willCommit(record, context);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
if (store.lifetimes?.willRequest) {
|
|
375
|
-
store.lifetimes.willRequest(request, identifier, store);
|
|
376
|
-
}
|
|
377
|
-
return next(request).then(document => handleFetchSuccess(store, request, identifier, document), error => handleFetchError(store, request, identifier, error));
|
|
258
|
+
const { request } = context;
|
|
259
|
+
const { store } = context.request;
|
|
260
|
+
if (isMutation(request)) {
|
|
261
|
+
const record = request.data?.record || request.records?.[0];
|
|
262
|
+
((test) => {
|
|
263
|
+
if (!test) throw new Error(`Expected to receive a list of records included in the ${request.op} request`);
|
|
264
|
+
})(record);
|
|
265
|
+
if (record) store.cache.willCommit(record, context);
|
|
266
|
+
}
|
|
267
|
+
if (store.lifetimes?.willRequest) store.lifetimes.willRequest(request, identifier, store);
|
|
268
|
+
return next(request).then((document) => handleFetchSuccess(store, request, identifier, document), (error) => handleFetchError(store, request, identifier, error));
|
|
378
269
|
}
|
|
379
270
|
|
|
271
|
+
//#endregion
|
|
380
272
|
export { CacheHandler, DataWorker };
|
|
273
|
+
//# sourceMappingURL=data-worker.js.map
|