@warp-drive-mirror/experiments 0.2.8 → 5.9.1
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
|
@@ -1,366 +1,266 @@
|
|
|
1
|
-
import { DocumentStorage } from
|
|
2
|
-
import { assertPrivateStore } from
|
|
3
|
-
import { SkipCache } from
|
|
1
|
+
import { t as DocumentStorage } from "./document-storage-D0k_6GM9.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
|
-
if (document) {
|
|
217
|
-
store.cache.put(document);
|
|
218
|
-
}
|
|
219
|
-
return completeRequest(identifier, store, context, next);
|
|
220
|
-
}).catch(e => {
|
|
221
|
-
return completeRequest(identifier, store, context, next);
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
return completeRequest(identifier, store, context, next);
|
|
226
|
-
}
|
|
227
|
-
};
|
|
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
|
+
return completeRequest(identifier, store, context, next);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
return completeRequest(identifier, store, context, next);
|
|
173
|
+
} };
|
|
228
174
|
function completeRequest(identifier, store, context, next) {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
}
|
|
235
|
-
context.setResponse(peeked.response);
|
|
236
|
-
if ('error' in peeked) {
|
|
237
|
-
throw peeked;
|
|
238
|
-
}
|
|
239
|
-
return maybeUpdateObjects(store, peeked.content);
|
|
175
|
+
const peeked = identifier ? store.cache.peekRequest(identifier) : null;
|
|
176
|
+
if (calcShouldFetch(store, context.request, !!peeked, identifier)) return fetchContentAndHydrate(next, context, identifier);
|
|
177
|
+
context.setResponse(peeked.response);
|
|
178
|
+
if ("error" in peeked) throw peeked;
|
|
179
|
+
return maybeUpdateObjects(store, peeked.content);
|
|
240
180
|
}
|
|
241
181
|
function maybeUpdateObjects(store, document) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
} else {
|
|
253
|
-
// @ts-expect-error FIXME investigate why document.data won't accept the signature
|
|
254
|
-
const data = document.data ? store.cache.peek(document.data) : null;
|
|
255
|
-
return Object.assign({}, document, {
|
|
256
|
-
data
|
|
257
|
-
});
|
|
258
|
-
}
|
|
182
|
+
if (!document) return document;
|
|
183
|
+
if (Array.isArray(document.data)) {
|
|
184
|
+
const data = document.data.map((identifier) => {
|
|
185
|
+
return store.cache.peek(identifier);
|
|
186
|
+
});
|
|
187
|
+
return Object.assign({}, document, { data });
|
|
188
|
+
} else {
|
|
189
|
+
const data = document.data ? store.cache.peek(document.data) : null;
|
|
190
|
+
return Object.assign({}, document, { data });
|
|
191
|
+
}
|
|
259
192
|
}
|
|
260
193
|
function maybeUpdatePersistedCache(store, document, resourceDocument) {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
});
|
|
270
|
-
} else if (document) {
|
|
271
|
-
void worker.storage.putDocument(document, resourceIdentifier => {
|
|
272
|
-
return store.cache.peek(resourceIdentifier);
|
|
273
|
-
});
|
|
274
|
-
}
|
|
194
|
+
const worker = store._worker;
|
|
195
|
+
if (!worker?.storage) return;
|
|
196
|
+
if (!document && resourceDocument) worker.storage.putResources(resourceDocument, (resourceIdentifier) => {
|
|
197
|
+
return store.cache.peek(resourceIdentifier);
|
|
198
|
+
});
|
|
199
|
+
else if (document) worker.storage.putDocument(document, (resourceIdentifier) => {
|
|
200
|
+
return store.cache.peek(resourceIdentifier);
|
|
201
|
+
});
|
|
275
202
|
}
|
|
276
203
|
function updateCacheForSuccess(store, request, document) {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
if (response.lid) {
|
|
294
|
-
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(request);
|
|
295
|
-
const full = store.cache.peekRequest(identifier);
|
|
296
|
-
maybeUpdatePersistedCache(store, full);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
return maybeUpdateObjects(store, response);
|
|
204
|
+
let response = null;
|
|
205
|
+
if (isMutation(request)) {
|
|
206
|
+
const record = request.data?.record || request.records?.[0];
|
|
207
|
+
if (record) response = store.cache.didCommit(record, document);
|
|
208
|
+
else if (isCacheAffecting(document)) {
|
|
209
|
+
response = store.cache.put(document);
|
|
210
|
+
maybeUpdatePersistedCache(store, null, response);
|
|
211
|
+
}
|
|
212
|
+
} else {
|
|
213
|
+
response = store.cache.put(document);
|
|
214
|
+
if (response.lid) {
|
|
215
|
+
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(request);
|
|
216
|
+
maybeUpdatePersistedCache(store, store.cache.peekRequest(identifier));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return maybeUpdateObjects(store, response);
|
|
300
220
|
}
|
|
301
221
|
function handleFetchSuccess(store, request, identifier, document) {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
}
|
|
310
|
-
return response;
|
|
222
|
+
let response;
|
|
223
|
+
assertPrivateStore(store);
|
|
224
|
+
store._join(() => {
|
|
225
|
+
response = updateCacheForSuccess(store, request, document);
|
|
226
|
+
});
|
|
227
|
+
if (store.lifetimes?.didRequest) store.lifetimes.didRequest(request, document.response, identifier, store);
|
|
228
|
+
return response;
|
|
311
229
|
}
|
|
312
230
|
function updateCacheForError(store, request, error) {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
if (identifier) {
|
|
322
|
-
maybeUpdatePersistedCache(store, error);
|
|
323
|
-
}
|
|
324
|
-
return store.cache.put(error);
|
|
325
|
-
}
|
|
231
|
+
if (isMutation(request)) {
|
|
232
|
+
const errors = error && error.content && typeof error.content === "object" && "errors" in error.content && Array.isArray(error.content.errors) ? error.content.errors : void 0;
|
|
233
|
+
const record = request.data?.record || request.records?.[0];
|
|
234
|
+
store.cache.commitWasRejected(record, errors);
|
|
235
|
+
} else {
|
|
236
|
+
if (store.cacheKeyManager.getOrCreateDocumentIdentifier(request)) maybeUpdatePersistedCache(store, error);
|
|
237
|
+
return store.cache.put(error);
|
|
238
|
+
}
|
|
326
239
|
}
|
|
327
240
|
function handleFetchError(store, request, identifier, error) {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
if (isMutation(request)) {
|
|
340
|
-
throw error;
|
|
341
|
-
}
|
|
342
|
-
const newError = cloneError(error);
|
|
343
|
-
newError.content = response;
|
|
344
|
-
throw newError;
|
|
241
|
+
if (request.signal?.aborted) throw error;
|
|
242
|
+
assertPrivateStore(store);
|
|
243
|
+
let response;
|
|
244
|
+
store._join(() => {
|
|
245
|
+
response = updateCacheForError(store, request, error);
|
|
246
|
+
});
|
|
247
|
+
if (identifier && store.lifetimes?.didRequest) store.lifetimes.didRequest(request, error.response, identifier, store);
|
|
248
|
+
if (isMutation(request)) throw error;
|
|
249
|
+
const newError = cloneError(error);
|
|
250
|
+
newError.content = response;
|
|
251
|
+
throw newError;
|
|
345
252
|
}
|
|
346
253
|
function fetchContentAndHydrate(next, context, identifier) {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
const record = request.data?.record || request.records?.[0];
|
|
356
|
-
if (record) {
|
|
357
|
-
store.cache.willCommit(record, context);
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
if (store.lifetimes?.willRequest) {
|
|
361
|
-
store.lifetimes.willRequest(request, identifier, store);
|
|
362
|
-
}
|
|
363
|
-
return next(request).then(document => handleFetchSuccess(store, request, identifier, document), error => handleFetchError(store, request, identifier, error));
|
|
254
|
+
const { request } = context;
|
|
255
|
+
const { store } = context.request;
|
|
256
|
+
if (isMutation(request)) {
|
|
257
|
+
const record = request.data?.record || request.records?.[0];
|
|
258
|
+
if (record) store.cache.willCommit(record, context);
|
|
259
|
+
}
|
|
260
|
+
if (store.lifetimes?.willRequest) store.lifetimes.willRequest(request, identifier, store);
|
|
261
|
+
return next(request).then((document) => handleFetchSuccess(store, request, identifier, document), (error) => handleFetchError(store, request, identifier, error));
|
|
364
262
|
}
|
|
365
263
|
|
|
264
|
+
//#endregion
|
|
366
265
|
export { CacheHandler, DataWorker };
|
|
266
|
+
//# sourceMappingURL=data-worker.js.map
|