@warp-drive/experiments 0.2.6-alpha.4 → 0.2.6-alpha.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +14 -17
  2. package/dist/data-worker.js +1 -0
  3. package/dist/unpkg/dev/data-worker.js +380 -0
  4. package/dist/unpkg/dev/document-storage.js +349 -0
  5. package/dist/unpkg/dev/image-fetch.js +74 -0
  6. package/dist/unpkg/dev/image-worker.js +99 -0
  7. package/dist/unpkg/dev/worker-fetch.js +158 -0
  8. package/dist/unpkg/dev-deprecated/data-worker.js +380 -0
  9. package/dist/unpkg/dev-deprecated/document-storage.js +349 -0
  10. package/dist/unpkg/dev-deprecated/image-fetch.js +74 -0
  11. package/dist/unpkg/dev-deprecated/image-worker.js +99 -0
  12. package/dist/unpkg/dev-deprecated/worker-fetch.js +158 -0
  13. package/dist/unpkg/prod/data-worker.js +366 -0
  14. package/dist/unpkg/prod/document-storage.js +339 -0
  15. package/dist/unpkg/prod/image-fetch.js +74 -0
  16. package/dist/unpkg/prod/image-worker.js +99 -0
  17. package/dist/unpkg/prod/worker-fetch.js +158 -0
  18. package/dist/unpkg/prod-deprecated/data-worker.js +366 -0
  19. package/dist/unpkg/prod-deprecated/document-storage.js +339 -0
  20. package/dist/unpkg/prod-deprecated/image-fetch.js +74 -0
  21. package/dist/unpkg/prod-deprecated/image-worker.js +99 -0
  22. package/dist/unpkg/prod-deprecated/worker-fetch.js +158 -0
  23. package/logos/README.md +2 -2
  24. package/logos/logo-yellow-slab.svg +1 -0
  25. package/logos/word-mark-black.svg +1 -0
  26. package/logos/word-mark-white.svg +1 -0
  27. package/package.json +25 -5
  28. package/logos/NCC-1701-a-blue.svg +0 -4
  29. package/logos/NCC-1701-a-gold.svg +0 -4
  30. package/logos/NCC-1701-a-gold_100.svg +0 -1
  31. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  32. package/logos/NCC-1701-a.svg +0 -4
  33. package/logos/docs-badge.svg +0 -2
  34. package/logos/ember-data-logo-dark.svg +0 -12
  35. package/logos/ember-data-logo-light.svg +0 -12
  36. package/logos/social1.png +0 -0
  37. package/logos/social2.png +0 -0
  38. package/logos/warp-drive-logo-dark.svg +0 -4
  39. package/logos/warp-drive-logo-gold.svg +0 -4
@@ -0,0 +1,74 @@
1
+ import { createDeferred } from '@warp-drive/core/request';
2
+
3
+ // @ts-expect-error untyped global
4
+ const isServerEnv = typeof FastBoot !== 'undefined';
5
+ class ImageFetch {
6
+ constructor(worker) {
7
+ this.threadId = isServerEnv ? '' : crypto.randomUUID();
8
+ this.pending = new Map();
9
+ this.cache = new Map();
10
+ this.worker = worker;
11
+ if (!isServerEnv) {
12
+ const fn = event => {
13
+ const {
14
+ type,
15
+ url
16
+ } = event.data;
17
+ const deferred = this.cleanupRequest(url);
18
+ if (!deferred) {
19
+ return;
20
+ }
21
+ if (type === 'success-response') {
22
+ deferred.resolve(url);
23
+ return;
24
+ }
25
+ if (type === 'error-response') {
26
+ deferred.reject(null);
27
+ return;
28
+ }
29
+ };
30
+ if (worker instanceof SharedWorker) {
31
+ worker.port.postMessage({
32
+ type: 'connect',
33
+ thread: this.threadId
34
+ });
35
+ worker.port.onmessage = fn;
36
+ } else if (worker) {
37
+ this.channel = new MessageChannel();
38
+ worker.postMessage({
39
+ type: 'connect',
40
+ thread: this.threadId
41
+ }, [this.channel.port2]);
42
+ this.channel.port1.onmessage = fn;
43
+ }
44
+ }
45
+ }
46
+ cleanupRequest(url) {
47
+ const deferred = this.pending.get(url);
48
+ this.pending.delete(url);
49
+ return deferred;
50
+ }
51
+ _send(event) {
52
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
53
+ this.worker instanceof SharedWorker ? this.worker.port.postMessage(event) : this.channel.port1.postMessage(event);
54
+ }
55
+ load(url) {
56
+ if (isServerEnv) {
57
+ return Promise.resolve(url);
58
+ }
59
+ const objectUrl = this.cache.get(url);
60
+ if (objectUrl) {
61
+ return Promise.resolve(objectUrl);
62
+ }
63
+ const deferred = createDeferred();
64
+ this.pending.set(url, deferred);
65
+ this._send({
66
+ type: 'load',
67
+ thread: this.threadId,
68
+ url
69
+ });
70
+ return deferred.promise;
71
+ }
72
+ }
73
+
74
+ export { ImageFetch };
@@ -0,0 +1,99 @@
1
+ const WorkerScope = globalThis.SharedWorkerGlobalScope;
2
+ async function loadImage(url) {
3
+ const response = await fetch(url);
4
+ const fileBlob = await response.blob();
5
+ return URL.createObjectURL(fileBlob);
6
+ }
7
+ class ImageWorker {
8
+ constructor(options) {
9
+ // disable if running on main thread
10
+ if (typeof window !== 'undefined') {
11
+ return;
12
+ }
13
+ this.threads = new Map();
14
+ this.pendingImages = new Map();
15
+ this.cache = new Map();
16
+ this.options = options || {
17
+ persisted: false
18
+ };
19
+ this.isSharedWorker = WorkerScope && globalThis instanceof WorkerScope;
20
+ this.initialize();
21
+ }
22
+ fetch(url) {
23
+ const objectUrl = this.cache.get(url);
24
+ if (objectUrl) {
25
+ return Promise.resolve(objectUrl);
26
+ }
27
+ const pending = this.pendingImages.get(url);
28
+ if (pending) {
29
+ return pending;
30
+ }
31
+ const promise = loadImage(url);
32
+ this.pendingImages.set(url, promise);
33
+ return promise.finally(() => {
34
+ this.pendingImages.delete(url);
35
+ });
36
+ }
37
+ initialize() {
38
+ if (this.isSharedWorker) {
39
+ globalThis.onconnect = e => {
40
+ const port = e.ports[0];
41
+ port.onmessage = event => {
42
+ const {
43
+ type
44
+ } = event.data;
45
+ switch (type) {
46
+ case 'connect':
47
+ this.setupThread(event.data.thread, port);
48
+ break;
49
+ }
50
+ };
51
+ port.start();
52
+ };
53
+ } else {
54
+ globalThis.onmessage = event => {
55
+ const {
56
+ type
57
+ } = event.data;
58
+ switch (type) {
59
+ case 'connect':
60
+ this.setupThread(event.data.thread, event.ports[0]);
61
+ break;
62
+ }
63
+ };
64
+ }
65
+ }
66
+ setupThread(thread, port) {
67
+ this.threads.set(thread, port);
68
+ port.onmessage = event => {
69
+ if (event.type === 'close') {
70
+ this.threads.delete(thread);
71
+ return;
72
+ }
73
+ const {
74
+ type
75
+ } = event.data;
76
+ switch (type) {
77
+ case 'load':
78
+ void this.request(event.data);
79
+ break;
80
+ }
81
+ };
82
+ }
83
+ async request(event) {
84
+ const {
85
+ thread,
86
+ url
87
+ } = event;
88
+ const objectUrl = await this.fetch(url);
89
+ const port = this.threads.get(thread);
90
+ port.postMessage({
91
+ type: 'success-response',
92
+ thread,
93
+ url,
94
+ objectUrl
95
+ });
96
+ }
97
+ }
98
+
99
+ export { ImageWorker };
@@ -0,0 +1,158 @@
1
+ import { createDeferred } from '@warp-drive/core/request';
2
+
3
+ // @ts-expect-error untyped global
4
+ const isServerEnv = typeof FastBoot !== 'undefined';
5
+ function isAggregateError(error) {
6
+ return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
7
+ }
8
+ function stitchTrace(stack, origin) {
9
+ if (origin.startsWith('Error\n')) {
10
+ return origin.slice(6) + '\n' + stack;
11
+ }
12
+ return origin + '\n' + stack;
13
+ }
14
+ function cloneError(error, stack) {
15
+ const isAggregate = isAggregateError(error);
16
+ const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
17
+ cloned.stack = stitchTrace(error.stack || '', stack);
18
+ cloned.error = error.error;
19
+
20
+ // copy over enumerable properties
21
+ Object.assign(cloned, error);
22
+ return cloned;
23
+ }
24
+ class WorkerFetch {
25
+ constructor(worker) {
26
+ this.threadId = isServerEnv ? '' : crypto.randomUUID();
27
+ this.pending = new Map();
28
+ this.worker = worker;
29
+ if (!isServerEnv) {
30
+ const fn = event => {
31
+ const {
32
+ type,
33
+ id,
34
+ data
35
+ } = event.data;
36
+ const info = this.cleanupRequest(id);
37
+
38
+ // typically this means the request was aborted
39
+ if (!info) {
40
+ return;
41
+ }
42
+ if (type === 'success-response') {
43
+ const {
44
+ deferred
45
+ } = info;
46
+ const {
47
+ response,
48
+ content
49
+ } = data;
50
+ if (response) {
51
+ response.headers = new Headers(response.headers);
52
+ info.context.setResponse(new Response(null, response));
53
+ }
54
+ deferred.resolve(content);
55
+ return;
56
+ }
57
+ if (type === 'error-response') {
58
+ const {
59
+ deferred,
60
+ stack
61
+ } = info;
62
+ deferred.reject(cloneError(data, stack));
63
+ return;
64
+ }
65
+ };
66
+ if (worker instanceof SharedWorker) {
67
+ worker.port.postMessage({
68
+ type: 'connect',
69
+ thread: this.threadId
70
+ });
71
+ worker.port.onmessage = fn;
72
+ } else if (worker) {
73
+ this.channel = new MessageChannel();
74
+ worker.postMessage({
75
+ type: 'connect',
76
+ thread: this.threadId
77
+ }, [this.channel.port2]);
78
+ this.channel.port1.onmessage = fn;
79
+ }
80
+ }
81
+ }
82
+ cleanupRequest(id) {
83
+ const info = this.pending.get(id);
84
+ this.pending.delete(id);
85
+ if (info?.signal) {
86
+ info.signal.removeEventListener('abort', info.abortFn);
87
+ }
88
+ return info;
89
+ }
90
+ send(event) {
91
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
92
+ this.worker instanceof SharedWorker ? this.worker.port.postMessage(event) : this.channel.port1.postMessage(event);
93
+ }
94
+ request(context, next) {
95
+ if (isServerEnv) {
96
+ return next(context.request);
97
+ }
98
+ const deferred = createDeferred();
99
+ const {
100
+ signal,
101
+ request
102
+ } = prepareRequest(context.request);
103
+ const abortFn = signal ? () => {
104
+ deferred.reject(enhanceReason(signal.reason));
105
+ this.send({
106
+ type: 'abort',
107
+ thread: this.threadId,
108
+ id: context.id,
109
+ data: signal.reason
110
+ });
111
+ this.cleanupRequest(context.id);
112
+ } : () => {
113
+ return;
114
+ };
115
+ signal?.addEventListener('abort', abortFn);
116
+ try {
117
+ throw new Error();
118
+ } catch (e) {
119
+ this.pending.set(context.id, {
120
+ context,
121
+ deferred,
122
+ signal,
123
+ abortFn,
124
+ stack: e.stack
125
+ });
126
+ }
127
+ this.send({
128
+ type: 'request',
129
+ thread: this.threadId,
130
+ id: context.id,
131
+ data: request
132
+ });
133
+ return deferred.promise;
134
+ }
135
+ }
136
+ function enhanceReason(reason) {
137
+ return new DOMException(reason || 'The user aborted a request.', 'AbortError');
138
+ }
139
+ function prepareRequest(request) {
140
+ const {
141
+ signal,
142
+ headers
143
+ } = request;
144
+ const requestCopy = Object.assign({}, request);
145
+ delete requestCopy.store;
146
+ if (signal instanceof AbortSignal) {
147
+ delete requestCopy.signal;
148
+ }
149
+ if (headers instanceof Headers) {
150
+ requestCopy.headers = Array.from(headers);
151
+ }
152
+ return {
153
+ signal: signal || null,
154
+ request: requestCopy
155
+ };
156
+ }
157
+
158
+ export { WorkerFetch };
@@ -0,0 +1,366 @@
1
+ import { DocumentStorage } from './document-storage.js';
2
+ import { assertPrivateStore } from '@warp-drive/core/store/-private';
3
+ import { SkipCache } from '@warp-drive/core/types/request';
4
+
5
+ const WorkerScope = globalThis.SharedWorkerGlobalScope;
6
+ class DataWorker {
7
+ constructor(UserStore, options) {
8
+ // disable if running on main thread
9
+ if (typeof window !== 'undefined') {
10
+ return;
11
+ }
12
+ this.store = new UserStore();
13
+ this.threads = new Map();
14
+ this.pending = new Map();
15
+ this.options = Object.assign({
16
+ persisted: false,
17
+ scope: ''
18
+ }, options);
19
+ this.isSharedWorker = WorkerScope && globalThis instanceof WorkerScope;
20
+ this.initialize();
21
+ }
22
+ initialize() {
23
+ // enable the CacheHandler to access the worker
24
+ this.store._worker = this;
25
+ if (this.options.persisted) {
26
+ // will be accessed by the worker's CacheHandler off of store
27
+ this.storage = new DocumentStorage({
28
+ scope: this.options.scope
29
+ });
30
+ }
31
+ if (this.isSharedWorker) {
32
+ globalThis.onconnect = e => {
33
+ const port = e.ports[0];
34
+ port.onmessage = event => {
35
+ const {
36
+ type
37
+ } = event.data;
38
+ switch (type) {
39
+ case 'connect':
40
+ this.setupThread(event.data.thread, port);
41
+ break;
42
+ }
43
+ };
44
+ port.start();
45
+ };
46
+ } else {
47
+ globalThis.onmessage = event => {
48
+ const {
49
+ type
50
+ } = event.data;
51
+ switch (type) {
52
+ case 'connect':
53
+ this.setupThread(event.data.thread, event.ports[0]);
54
+ break;
55
+ }
56
+ };
57
+ }
58
+ }
59
+ setupThread(thread, port) {
60
+ this.threads.set(thread, port);
61
+ this.pending.set(thread, new Map());
62
+ port.onmessage = event => {
63
+ if (event.type === 'close') {
64
+ this.threads.delete(thread);
65
+ return;
66
+ }
67
+ const {
68
+ type
69
+ } = event.data;
70
+ switch (type) {
71
+ case 'abort':
72
+ this.abortRequest(event.data);
73
+ break;
74
+ case 'request':
75
+ void this.request(prepareRequest(event.data));
76
+ break;
77
+ }
78
+ };
79
+ }
80
+ abortRequest(event) {
81
+ const {
82
+ thread,
83
+ id
84
+ } = event;
85
+ const future = this.pending.get(thread).get(id);
86
+ if (future) {
87
+ future.abort();
88
+ this.pending.get(thread).delete(id);
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
+ }
120
+ function softCloneResponse(response) {
121
+ if (!response) return null;
122
+ const clone = {};
123
+ if (response.headers) {
124
+ clone.headers = Array.from(response.headers);
125
+ }
126
+ clone.ok = response.ok;
127
+ clone.redirected = response.redirected;
128
+ clone.status = response.status;
129
+ clone.statusText = response.statusText;
130
+ clone.type = response.type;
131
+ clone.url = response.url;
132
+ return clone;
133
+ }
134
+ function isAbortError(error) {
135
+ return error instanceof Error && error.name === 'AbortError';
136
+ }
137
+ function prepareResponse(result) {
138
+ const newResponse = {
139
+ response: softCloneResponse(result.response),
140
+ content: result.content
141
+ };
142
+ return newResponse;
143
+ }
144
+ function prepareRequest(event) {
145
+ if (event.data.headers) {
146
+ event.data.headers = new Headers(event.data.headers);
147
+ }
148
+ return event;
149
+ }
150
+
151
+ const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
152
+
153
+ /**
154
+ * In a Worker, any time we are asked to make a request, data needs to be returned.
155
+ * background requests are ergo no different than foreground requests.
156
+ * @internal
157
+ */
158
+ function calcShouldFetch(store, request, hasCachedValue, identifier) {
159
+ const {
160
+ cacheOptions
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);
163
+ }
164
+ function isMutation(request) {
165
+ return Boolean(request.op && MUTATION_OPS.has(request.op));
166
+ }
167
+ function isCacheAffecting(document) {
168
+ if (!isMutation(document.request)) {
169
+ return true;
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;
179
+ }
180
+ function isAggregateError(error) {
181
+ return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
182
+ }
183
+ // TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
184
+ function cloneError(error) {
185
+ const isAggregate = isAggregateError(error);
186
+ const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
187
+ cloned.stack = error.stack;
188
+ cloned.error = error.error;
189
+
190
+ // copy over enumerable properties
191
+ Object.assign(cloned, error);
192
+ return cloned;
193
+ }
194
+
195
+ /**
196
+ * A simplified CacheHandler that hydrates ResourceDataDocuments from the cache
197
+ * with their referenced resources.
198
+ *
199
+ */
200
+ const CacheHandler = {
201
+ request(context, next) {
202
+ // if we have no cache or no cache-key skip cache handling
203
+ if (!context.request.store || context.request.cacheOptions?.[SkipCache]) {
204
+ return next(context.request);
205
+ }
206
+ const {
207
+ store
208
+ } = context.request;
209
+ const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(context.request);
210
+ const peeked = identifier ? store.cache.peekRequest(identifier) : null;
211
+ if (identifier && !peeked) {
212
+ // if we are using persisted cache, we should attempt to populate the in-memory cache now
213
+ const worker = store._worker;
214
+ if (worker?.storage) {
215
+ return worker.storage.getDocument(identifier).then(document => {
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
+ };
228
+ function completeRequest(identifier, store, context, next) {
229
+ const peeked = identifier ? store.cache.peekRequest(identifier) : null;
230
+ // In a Worker, any time we are asked to make a request, data needs to be returned.
231
+ // background requests are ergo no different than foreground requests.
232
+ if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
233
+ return fetchContentAndHydrate(next, context, identifier);
234
+ }
235
+ context.setResponse(peeked.response);
236
+ if ('error' in peeked) {
237
+ throw peeked;
238
+ }
239
+ return maybeUpdateObjects(store, peeked.content);
240
+ }
241
+ function maybeUpdateObjects(store, document) {
242
+ if (!document) {
243
+ return document;
244
+ }
245
+ if (Array.isArray(document.data)) {
246
+ const data = document.data.map(identifier => {
247
+ return store.cache.peek(identifier);
248
+ });
249
+ return Object.assign({}, document, {
250
+ data
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
+ }
259
+ }
260
+ function maybeUpdatePersistedCache(store, document, resourceDocument) {
261
+ const worker = store._worker;
262
+ if (!worker?.storage) {
263
+ return;
264
+ }
265
+ if (!document && resourceDocument) {
266
+ // we have resources to update but not a full request to cache
267
+ void worker.storage.putResources(resourceDocument, resourceIdentifier => {
268
+ return store.cache.peek(resourceIdentifier);
269
+ });
270
+ } else if (document) {
271
+ void worker.storage.putDocument(document, resourceIdentifier => {
272
+ return store.cache.peek(resourceIdentifier);
273
+ });
274
+ }
275
+ }
276
+ function updateCacheForSuccess(store, request, document) {
277
+ let response = null;
278
+ if (isMutation(request)) {
279
+ const record = request.data?.record || request.records?.[0];
280
+ if (record) {
281
+ // @ts-expect-error while this is valid, we should update the CacheHandler for transactional saves
282
+ response = store.cache.didCommit(record, document);
283
+
284
+ // a mutation combined with a 204 has no cache impact when no known records were involved
285
+ // a createRecord with a 201 with an empty response and no known records should similarly
286
+ // have no cache impact
287
+ } else if (isCacheAffecting(document)) {
288
+ response = store.cache.put(document);
289
+ maybeUpdatePersistedCache(store, null, response);
290
+ }
291
+ } else {
292
+ response = store.cache.put(document);
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);
300
+ }
301
+ function handleFetchSuccess(store, request, identifier, document) {
302
+ let response;
303
+ assertPrivateStore(store);
304
+ store._join(() => {
305
+ response = updateCacheForSuccess(store, request, document);
306
+ });
307
+ if (store.lifetimes?.didRequest) {
308
+ store.lifetimes.didRequest(request, document.response, identifier, store);
309
+ }
310
+ return response;
311
+ }
312
+ function updateCacheForError(store, request, error) {
313
+ if (isMutation(request)) {
314
+ // TODO similar to didCommit we should spec this to be similar to cache.put for handling full response
315
+ // currently we let the response remain undefiend.
316
+ const errors = error && error.content && typeof error.content === 'object' && 'errors' in error.content && Array.isArray(error.content.errors) ? error.content.errors : undefined;
317
+ const record = request.data?.record || request.records?.[0];
318
+ store.cache.commitWasRejected(record, errors);
319
+ } else {
320
+ const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(request);
321
+ if (identifier) {
322
+ maybeUpdatePersistedCache(store, error);
323
+ }
324
+ return store.cache.put(error);
325
+ }
326
+ }
327
+ function handleFetchError(store, request, identifier, error) {
328
+ if (request.signal?.aborted) {
329
+ throw error;
330
+ }
331
+ assertPrivateStore(store);
332
+ let response;
333
+ store._join(() => {
334
+ response = updateCacheForError(store, request, error);
335
+ });
336
+ if (identifier && store.lifetimes?.didRequest) {
337
+ store.lifetimes.didRequest(request, error.response, identifier, store);
338
+ }
339
+ if (isMutation(request)) {
340
+ throw error;
341
+ }
342
+ const newError = cloneError(error);
343
+ newError.content = response;
344
+ throw newError;
345
+ }
346
+ function fetchContentAndHydrate(next, context, identifier) {
347
+ const {
348
+ request
349
+ } = context;
350
+ const {
351
+ store
352
+ } = context.request;
353
+ if (isMutation(request)) {
354
+ // TODO should we handle multiple records in request.records by iteratively calling willCommit for each
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));
364
+ }
365
+
366
+ export { CacheHandler, DataWorker };