@warp-drive-mirror/experiments 0.2.7 → 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.
Files changed (104) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/README.md +2 -1
  3. package/dist/data-worker.d.ts +35 -0
  4. package/dist/data-worker.d.ts.map +1 -0
  5. package/dist/data-worker.js +235 -339
  6. package/dist/data-worker.js.map +1 -0
  7. package/dist/document-storage-CwwTQ49-.d.ts +91 -0
  8. package/dist/document-storage-CwwTQ49-.d.ts.map +1 -0
  9. package/dist/document-storage-LM925Stz.js +270 -0
  10. package/dist/document-storage-LM925Stz.js.map +1 -0
  11. package/dist/document-storage.d.ts +2 -0
  12. package/dist/document-storage.js +3 -1
  13. package/dist/image-fetch.d.ts +60 -0
  14. package/dist/image-fetch.d.ts.map +1 -0
  15. package/dist/image-fetch.js +108 -79
  16. package/dist/image-fetch.js.map +1 -0
  17. package/dist/image-worker.d.ts +40 -0
  18. package/dist/image-worker.d.ts.map +1 -0
  19. package/dist/image-worker.js +95 -95
  20. package/dist/image-worker.js.map +1 -0
  21. package/dist/pagination.d.ts +2 -0
  22. package/dist/pagination.js +3 -0
  23. package/dist/storage.d.ts +354 -0
  24. package/dist/storage.d.ts.map +1 -0
  25. package/dist/storage.js +914 -0
  26. package/dist/storage.js.map +1 -0
  27. package/dist/types-CRs-Qww6.d.ts +17 -0
  28. package/dist/types-CRs-Qww6.d.ts.map +1 -0
  29. package/dist/unpkg/dev/data-worker.js +231 -338
  30. package/dist/unpkg/dev/data-worker.js.map +1 -0
  31. package/dist/unpkg/dev/document-storage-DkiEg9rY.js +268 -0
  32. package/dist/unpkg/dev/document-storage-DkiEg9rY.js.map +1 -0
  33. package/dist/unpkg/dev/document-storage.js +2 -348
  34. package/dist/unpkg/dev/image-fetch.js +104 -71
  35. package/dist/unpkg/dev/image-fetch.js.map +1 -0
  36. package/dist/unpkg/dev/image-worker.js +93 -94
  37. package/dist/unpkg/dev/image-worker.js.map +1 -0
  38. package/dist/unpkg/dev/pagination.js +3 -0
  39. package/dist/unpkg/dev/storage.js +913 -0
  40. package/dist/unpkg/dev/storage.js.map +1 -0
  41. package/dist/unpkg/dev/worker-fetch.js +112 -145
  42. package/dist/unpkg/dev/worker-fetch.js.map +1 -0
  43. package/dist/unpkg/dev-deprecated/data-worker.js +231 -338
  44. package/dist/unpkg/dev-deprecated/data-worker.js.map +1 -0
  45. package/dist/unpkg/dev-deprecated/document-storage-DkiEg9rY.js +268 -0
  46. package/dist/unpkg/dev-deprecated/document-storage-DkiEg9rY.js.map +1 -0
  47. package/dist/unpkg/dev-deprecated/document-storage.js +2 -348
  48. package/dist/unpkg/dev-deprecated/image-fetch.js +104 -71
  49. package/dist/unpkg/dev-deprecated/image-fetch.js.map +1 -0
  50. package/dist/unpkg/dev-deprecated/image-worker.js +93 -94
  51. package/dist/unpkg/dev-deprecated/image-worker.js.map +1 -0
  52. package/dist/unpkg/dev-deprecated/pagination.js +3 -0
  53. package/dist/unpkg/dev-deprecated/storage.js +913 -0
  54. package/dist/unpkg/dev-deprecated/storage.js.map +1 -0
  55. package/dist/unpkg/dev-deprecated/worker-fetch.js +112 -145
  56. package/dist/unpkg/dev-deprecated/worker-fetch.js.map +1 -0
  57. package/dist/unpkg/prod/data-worker.js +224 -324
  58. package/dist/unpkg/prod/data-worker.js.map +1 -0
  59. package/dist/unpkg/prod/document-storage-D0k_6GM9.js +262 -0
  60. package/dist/unpkg/prod/document-storage-D0k_6GM9.js.map +1 -0
  61. package/dist/unpkg/prod/document-storage.js +2 -338
  62. package/dist/unpkg/prod/image-fetch.js +101 -71
  63. package/dist/unpkg/prod/image-fetch.js.map +1 -0
  64. package/dist/unpkg/prod/image-worker.js +93 -94
  65. package/dist/unpkg/prod/image-worker.js.map +1 -0
  66. package/dist/unpkg/prod/pagination.js +3 -0
  67. package/dist/unpkg/prod/storage.js +844 -0
  68. package/dist/unpkg/prod/storage.js.map +1 -0
  69. package/dist/unpkg/prod/worker-fetch.js +109 -145
  70. package/dist/unpkg/prod/worker-fetch.js.map +1 -0
  71. package/dist/unpkg/prod-deprecated/data-worker.js +224 -324
  72. package/dist/unpkg/prod-deprecated/data-worker.js.map +1 -0
  73. package/dist/unpkg/prod-deprecated/document-storage-D0k_6GM9.js +262 -0
  74. package/dist/unpkg/prod-deprecated/document-storage-D0k_6GM9.js.map +1 -0
  75. package/dist/unpkg/prod-deprecated/document-storage.js +2 -338
  76. package/dist/unpkg/prod-deprecated/image-fetch.js +101 -71
  77. package/dist/unpkg/prod-deprecated/image-fetch.js.map +1 -0
  78. package/dist/unpkg/prod-deprecated/image-worker.js +93 -94
  79. package/dist/unpkg/prod-deprecated/image-worker.js.map +1 -0
  80. package/dist/unpkg/prod-deprecated/pagination.js +3 -0
  81. package/dist/unpkg/prod-deprecated/storage.js +844 -0
  82. package/dist/unpkg/prod-deprecated/storage.js.map +1 -0
  83. package/dist/unpkg/prod-deprecated/worker-fetch.js +109 -145
  84. package/dist/unpkg/prod-deprecated/worker-fetch.js.map +1 -0
  85. package/dist/worker-fetch.d.ts +23 -0
  86. package/dist/worker-fetch.d.ts.map +1 -0
  87. package/dist/worker-fetch.js +116 -153
  88. package/dist/worker-fetch.js.map +1 -0
  89. package/package.json +27 -23
  90. package/declarations/data-worker/cache-handler.d.ts +0 -7
  91. package/declarations/data-worker/fetch.d.ts +0 -21
  92. package/declarations/data-worker/types.d.ts +0 -31
  93. package/declarations/data-worker/utils.d.ts +0 -12
  94. package/declarations/data-worker/worker.d.ts +0 -23
  95. package/declarations/data-worker.d.ts +0 -2
  96. package/declarations/document-storage/index.d.ts +0 -90
  97. package/declarations/document-storage.d.ts +0 -1
  98. package/declarations/image-fetch.d.ts +0 -1
  99. package/declarations/image-worker/fetch.d.ts +0 -18
  100. package/declarations/image-worker/types.d.ts +0 -21
  101. package/declarations/image-worker/worker.d.ts +0 -14
  102. package/declarations/image-worker.d.ts +0 -1
  103. package/declarations/worker-fetch.d.ts +0 -1
  104. package/dist/index-CGCX7hY2.js +0 -349
@@ -1,366 +1,266 @@
1
- import { DocumentStorage } from './document-storage.js';
2
- import { assertPrivateStore } from '@warp-drive-mirror/core/store/-private';
3
- import { SkipCache } from '@warp-drive-mirror/core/types/request';
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
- 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
- }
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
- 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;
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
- return error instanceof Error && error.name === 'AbortError';
103
+ return error instanceof Error && error.name === "AbortError";
136
104
  }
137
105
  function prepareResponse(result) {
138
- const newResponse = {
139
- response: softCloneResponse(result.response),
140
- content: result.content
141
- };
142
- return newResponse;
106
+ return {
107
+ response: softCloneResponse(result.response),
108
+ content: result.content
109
+ };
143
110
  }
144
111
  function prepareRequest(event) {
145
- if (event.data.headers) {
146
- event.data.headers = new Headers(event.data.headers);
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
- const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
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
- * 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
- */
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
- 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);
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
- return Boolean(request.op && MUTATION_OPS.has(request.op));
133
+ return Boolean(request.op && MUTATION_OPS.has(request.op));
166
134
  }
167
135
  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;
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
- return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
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
- 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;
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
- * 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
- };
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
- 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);
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
- 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
- }
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
- 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
- }
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
- 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);
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
- 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;
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
- 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
- }
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
- 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;
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
- 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));
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