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