@warp-drive/experiments 0.2.6-alpha.3 → 0.2.6-alpha.32

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 (99) hide show
  1. package/README.md +14 -17
  2. package/dist/data-worker.js +1 -0
  3. package/dist/unpkg/dev/data-worker.js +378 -0
  4. package/dist/unpkg/dev/declarations/data-worker/cache-handler.d.ts +7 -0
  5. package/dist/unpkg/dev/declarations/data-worker/fetch.d.ts +21 -0
  6. package/dist/unpkg/dev/declarations/data-worker/types.d.ts +31 -0
  7. package/dist/unpkg/dev/declarations/data-worker/utils.d.ts +12 -0
  8. package/dist/unpkg/dev/declarations/data-worker/worker.d.ts +23 -0
  9. package/dist/unpkg/dev/declarations/data-worker.d.ts +2 -0
  10. package/dist/unpkg/dev/declarations/document-storage/index.d.ts +90 -0
  11. package/dist/unpkg/dev/declarations/document-storage.d.ts +1 -0
  12. package/dist/unpkg/dev/declarations/image-fetch.d.ts +1 -0
  13. package/dist/unpkg/dev/declarations/image-worker/fetch.d.ts +18 -0
  14. package/dist/unpkg/dev/declarations/image-worker/types.d.ts +21 -0
  15. package/dist/unpkg/dev/declarations/image-worker/worker.d.ts +14 -0
  16. package/dist/unpkg/dev/declarations/image-worker.d.ts +1 -0
  17. package/dist/unpkg/dev/declarations/worker-fetch.d.ts +1 -0
  18. package/dist/unpkg/dev/document-storage.js +1 -0
  19. package/dist/unpkg/dev/image-fetch.js +80 -0
  20. package/dist/unpkg/dev/image-worker.js +98 -0
  21. package/dist/unpkg/dev/index-CGCX7hY2.js +349 -0
  22. package/dist/unpkg/dev/worker-fetch.js +164 -0
  23. package/dist/unpkg/dev-deprecated/data-worker.js +378 -0
  24. package/dist/unpkg/dev-deprecated/declarations/data-worker/cache-handler.d.ts +7 -0
  25. package/dist/unpkg/dev-deprecated/declarations/data-worker/fetch.d.ts +21 -0
  26. package/dist/unpkg/dev-deprecated/declarations/data-worker/types.d.ts +31 -0
  27. package/dist/unpkg/dev-deprecated/declarations/data-worker/utils.d.ts +12 -0
  28. package/dist/unpkg/dev-deprecated/declarations/data-worker/worker.d.ts +23 -0
  29. package/dist/unpkg/dev-deprecated/declarations/data-worker.d.ts +2 -0
  30. package/dist/unpkg/dev-deprecated/declarations/document-storage/index.d.ts +90 -0
  31. package/dist/unpkg/dev-deprecated/declarations/document-storage.d.ts +1 -0
  32. package/dist/unpkg/dev-deprecated/declarations/image-fetch.d.ts +1 -0
  33. package/dist/unpkg/dev-deprecated/declarations/image-worker/fetch.d.ts +18 -0
  34. package/dist/unpkg/dev-deprecated/declarations/image-worker/types.d.ts +21 -0
  35. package/dist/unpkg/dev-deprecated/declarations/image-worker/worker.d.ts +14 -0
  36. package/dist/unpkg/dev-deprecated/declarations/image-worker.d.ts +1 -0
  37. package/dist/unpkg/dev-deprecated/declarations/worker-fetch.d.ts +1 -0
  38. package/dist/unpkg/dev-deprecated/document-storage.js +1 -0
  39. package/dist/unpkg/dev-deprecated/image-fetch.js +80 -0
  40. package/dist/unpkg/dev-deprecated/image-worker.js +98 -0
  41. package/dist/unpkg/dev-deprecated/index-CGCX7hY2.js +349 -0
  42. package/dist/unpkg/dev-deprecated/worker-fetch.js +164 -0
  43. package/dist/unpkg/prod/data-worker.js +378 -0
  44. package/dist/unpkg/prod/declarations/data-worker/cache-handler.d.ts +7 -0
  45. package/dist/unpkg/prod/declarations/data-worker/fetch.d.ts +21 -0
  46. package/dist/unpkg/prod/declarations/data-worker/types.d.ts +31 -0
  47. package/dist/unpkg/prod/declarations/data-worker/utils.d.ts +12 -0
  48. package/dist/unpkg/prod/declarations/data-worker/worker.d.ts +23 -0
  49. package/dist/unpkg/prod/declarations/data-worker.d.ts +2 -0
  50. package/dist/unpkg/prod/declarations/document-storage/index.d.ts +90 -0
  51. package/dist/unpkg/prod/declarations/document-storage.d.ts +1 -0
  52. package/dist/unpkg/prod/declarations/image-fetch.d.ts +1 -0
  53. package/dist/unpkg/prod/declarations/image-worker/fetch.d.ts +18 -0
  54. package/dist/unpkg/prod/declarations/image-worker/types.d.ts +21 -0
  55. package/dist/unpkg/prod/declarations/image-worker/worker.d.ts +14 -0
  56. package/dist/unpkg/prod/declarations/image-worker.d.ts +1 -0
  57. package/dist/unpkg/prod/declarations/worker-fetch.d.ts +1 -0
  58. package/dist/unpkg/prod/document-storage.js +1 -0
  59. package/dist/unpkg/prod/image-fetch.js +80 -0
  60. package/dist/unpkg/prod/image-worker.js +98 -0
  61. package/dist/unpkg/prod/index-CGCX7hY2.js +349 -0
  62. package/dist/unpkg/prod/worker-fetch.js +164 -0
  63. package/dist/unpkg/prod-deprecated/data-worker.js +378 -0
  64. package/dist/unpkg/prod-deprecated/declarations/data-worker/cache-handler.d.ts +7 -0
  65. package/dist/unpkg/prod-deprecated/declarations/data-worker/fetch.d.ts +21 -0
  66. package/dist/unpkg/prod-deprecated/declarations/data-worker/types.d.ts +31 -0
  67. package/dist/unpkg/prod-deprecated/declarations/data-worker/utils.d.ts +12 -0
  68. package/dist/unpkg/prod-deprecated/declarations/data-worker/worker.d.ts +23 -0
  69. package/dist/unpkg/prod-deprecated/declarations/data-worker.d.ts +2 -0
  70. package/dist/unpkg/prod-deprecated/declarations/document-storage/index.d.ts +90 -0
  71. package/dist/unpkg/prod-deprecated/declarations/document-storage.d.ts +1 -0
  72. package/dist/unpkg/prod-deprecated/declarations/image-fetch.d.ts +1 -0
  73. package/dist/unpkg/prod-deprecated/declarations/image-worker/fetch.d.ts +18 -0
  74. package/dist/unpkg/prod-deprecated/declarations/image-worker/types.d.ts +21 -0
  75. package/dist/unpkg/prod-deprecated/declarations/image-worker/worker.d.ts +14 -0
  76. package/dist/unpkg/prod-deprecated/declarations/image-worker.d.ts +1 -0
  77. package/dist/unpkg/prod-deprecated/declarations/worker-fetch.d.ts +1 -0
  78. package/dist/unpkg/prod-deprecated/document-storage.js +1 -0
  79. package/dist/unpkg/prod-deprecated/image-fetch.js +80 -0
  80. package/dist/unpkg/prod-deprecated/image-worker.js +98 -0
  81. package/dist/unpkg/prod-deprecated/index-CGCX7hY2.js +349 -0
  82. package/dist/unpkg/prod-deprecated/worker-fetch.js +164 -0
  83. package/logos/README.md +2 -2
  84. package/logos/logo-yellow-slab.svg +1 -0
  85. package/logos/word-mark-black.svg +1 -0
  86. package/logos/word-mark-white.svg +1 -0
  87. package/package.json +5 -5
  88. package/logos/NCC-1701-a-blue.svg +0 -4
  89. package/logos/NCC-1701-a-gold.svg +0 -4
  90. package/logos/NCC-1701-a-gold_100.svg +0 -1
  91. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  92. package/logos/NCC-1701-a.svg +0 -4
  93. package/logos/docs-badge.svg +0 -2
  94. package/logos/ember-data-logo-dark.svg +0 -12
  95. package/logos/ember-data-logo-light.svg +0 -12
  96. package/logos/social1.png +0 -0
  97. package/logos/social2.png +0 -0
  98. package/logos/warp-drive-logo-dark.svg +0 -4
  99. package/logos/warp-drive-logo-gold.svg +0 -4
@@ -0,0 +1,164 @@
1
+ import { createDeferred } from '@warp-drive/core/request';
2
+ import { macroCondition, getGlobalConfig } from '@embroider/macros';
3
+
4
+ // @ts-expect-error untyped global
5
+ const isServerEnv = typeof FastBoot !== 'undefined';
6
+ function isAggregateError(error) {
7
+ return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
8
+ }
9
+ function stitchTrace(stack, origin) {
10
+ if (origin.startsWith('Error\n')) {
11
+ return origin.slice(6) + '\n' + stack;
12
+ }
13
+ return origin + '\n' + stack;
14
+ }
15
+ function cloneError(error, stack) {
16
+ const isAggregate = isAggregateError(error);
17
+ const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
18
+ cloned.stack = stitchTrace(error.stack || '', stack);
19
+ cloned.error = error.error;
20
+
21
+ // copy over enumerable properties
22
+ Object.assign(cloned, error);
23
+ return cloned;
24
+ }
25
+ class WorkerFetch {
26
+ constructor(worker) {
27
+ this.threadId = isServerEnv ? '' : crypto.randomUUID();
28
+ this.pending = new Map();
29
+ const isTesting = macroCondition(getGlobalConfig().WarpDrive.env.TESTING) ? true : false;
30
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
31
+ if (!test) {
32
+ throw new Error(`Expected a SharedWorker instance`);
33
+ }
34
+ })(isTesting || isServerEnv || worker instanceof SharedWorker) : {};
35
+ this.worker = worker;
36
+ if (!isServerEnv) {
37
+ const fn = event => {
38
+ const {
39
+ type,
40
+ id,
41
+ data
42
+ } = event.data;
43
+ const info = this.cleanupRequest(id);
44
+
45
+ // typically this means the request was aborted
46
+ if (!info) {
47
+ return;
48
+ }
49
+ if (type === 'success-response') {
50
+ const {
51
+ deferred
52
+ } = info;
53
+ const {
54
+ response,
55
+ content
56
+ } = data;
57
+ if (response) {
58
+ response.headers = new Headers(response.headers);
59
+ info.context.setResponse(new Response(null, response));
60
+ }
61
+ deferred.resolve(content);
62
+ return;
63
+ }
64
+ if (type === 'error-response') {
65
+ const {
66
+ deferred,
67
+ stack
68
+ } = info;
69
+ deferred.reject(cloneError(data, stack));
70
+ return;
71
+ }
72
+ };
73
+ if (worker instanceof SharedWorker) {
74
+ worker.port.postMessage({
75
+ type: 'connect',
76
+ thread: this.threadId
77
+ });
78
+ worker.port.onmessage = fn;
79
+ } else if (worker) {
80
+ this.channel = new MessageChannel();
81
+ worker.postMessage({
82
+ type: 'connect',
83
+ thread: this.threadId
84
+ }, [this.channel.port2]);
85
+ this.channel.port1.onmessage = fn;
86
+ }
87
+ }
88
+ }
89
+ cleanupRequest(id) {
90
+ const info = this.pending.get(id);
91
+ this.pending.delete(id);
92
+ if (info?.signal) {
93
+ info.signal.removeEventListener('abort', info.abortFn);
94
+ }
95
+ return info;
96
+ }
97
+ send(event) {
98
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
99
+ this.worker instanceof SharedWorker ? this.worker.port.postMessage(event) : this.channel.port1.postMessage(event);
100
+ }
101
+ request(context, next) {
102
+ if (isServerEnv) {
103
+ return next(context.request);
104
+ }
105
+ const deferred = createDeferred();
106
+ const {
107
+ signal,
108
+ request
109
+ } = prepareRequest(context.request);
110
+ const abortFn = signal ? () => {
111
+ deferred.reject(enhanceReason(signal.reason));
112
+ this.send({
113
+ type: 'abort',
114
+ thread: this.threadId,
115
+ id: context.id,
116
+ data: signal.reason
117
+ });
118
+ this.cleanupRequest(context.id);
119
+ } : () => {
120
+ return;
121
+ };
122
+ signal?.addEventListener('abort', abortFn);
123
+ try {
124
+ throw new Error();
125
+ } catch (e) {
126
+ this.pending.set(context.id, {
127
+ context,
128
+ deferred,
129
+ signal,
130
+ abortFn,
131
+ stack: e.stack
132
+ });
133
+ }
134
+ this.send({
135
+ type: 'request',
136
+ thread: this.threadId,
137
+ id: context.id,
138
+ data: request
139
+ });
140
+ return deferred.promise;
141
+ }
142
+ }
143
+ function enhanceReason(reason) {
144
+ return new DOMException(reason || 'The user aborted a request.', 'AbortError');
145
+ }
146
+ function prepareRequest(request) {
147
+ const {
148
+ signal,
149
+ headers
150
+ } = request;
151
+ const requestCopy = Object.assign({}, request);
152
+ delete requestCopy.store;
153
+ if (signal instanceof AbortSignal) {
154
+ delete requestCopy.signal;
155
+ }
156
+ if (headers instanceof Headers) {
157
+ requestCopy.headers = Array.from(headers);
158
+ }
159
+ return {
160
+ signal: signal || null,
161
+ request: requestCopy
162
+ };
163
+ }
164
+ export { WorkerFetch };
@@ -0,0 +1,378 @@
1
+ import { D as DocumentStorage } from "./index-CGCX7hY2.js";
2
+ import { assertPrivateStore } from '@warp-drive/core/store/-private';
3
+ import { SkipCache } from '@warp-drive/core/types/request';
4
+ import { macroCondition, getGlobalConfig } from '@embroider/macros';
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
+ const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
151
+
152
+ /**
153
+ * In a Worker, any time we are asked to make a request, data needs to be returned.
154
+ * background requests are ergo no different than foreground requests.
155
+ * @internal
156
+ */
157
+ function calcShouldFetch(store, request, hasCachedValue, identifier) {
158
+ const {
159
+ cacheOptions
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);
162
+ }
163
+ function isMutation(request) {
164
+ return Boolean(request.op && MUTATION_OPS.has(request.op));
165
+ }
166
+ function isCacheAffecting(document) {
167
+ if (!isMutation(document.request)) {
168
+ return true;
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;
178
+ }
179
+ function isAggregateError(error) {
180
+ return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
181
+ }
182
+ // TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
183
+ function cloneError(error) {
184
+ const isAggregate = isAggregateError(error);
185
+ const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
186
+ cloned.stack = error.stack;
187
+ cloned.error = error.error;
188
+
189
+ // copy over enumerable properties
190
+ Object.assign(cloned, error);
191
+ return cloned;
192
+ }
193
+
194
+ /**
195
+ * A simplified CacheHandler that hydrates ResourceDataDocuments from the cache
196
+ * with their referenced resources.
197
+ *
198
+ */
199
+ const CacheHandler = {
200
+ request(context, next) {
201
+ // if we have no cache or no cache-key skip cache handling
202
+ if (!context.request.store || context.request.cacheOptions?.[SkipCache]) {
203
+ return next(context.request);
204
+ }
205
+ const {
206
+ store
207
+ } = context.request;
208
+ const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(context.request);
209
+ const peeked = identifier ? store.cache.peekRequest(identifier) : null;
210
+ if (identifier && !peeked) {
211
+ // if we are using persisted cache, we should attempt to populate the in-memory cache now
212
+ const worker = store._worker;
213
+ if (worker?.storage) {
214
+ return worker.storage.getDocument(identifier).then(document => {
215
+ if (document) {
216
+ store.cache.put(document);
217
+ }
218
+ return completeRequest(identifier, store, context, next);
219
+ }).catch(e => {
220
+ if (macroCondition(getGlobalConfig().WarpDrive.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
+ };
231
+ function completeRequest(identifier, store, context, next) {
232
+ const peeked = identifier ? store.cache.peekRequest(identifier) : null;
233
+ // In a Worker, any time we are asked to make a request, data needs to be returned.
234
+ // background requests are ergo no different than foreground requests.
235
+ if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
236
+ return fetchContentAndHydrate(next, context, identifier);
237
+ }
238
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
239
+ if (!test) {
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);
248
+ }
249
+ function maybeUpdateObjects(store, document) {
250
+ if (!document) {
251
+ return document;
252
+ }
253
+ if (Array.isArray(document.data)) {
254
+ const data = document.data.map(identifier => {
255
+ return store.cache.peek(identifier);
256
+ });
257
+ return Object.assign({}, document, {
258
+ data
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
+ }
267
+ }
268
+ function maybeUpdatePersistedCache(store, document, resourceDocument) {
269
+ const worker = store._worker;
270
+ if (!worker?.storage) {
271
+ return;
272
+ }
273
+ if (!document && resourceDocument) {
274
+ // we have resources to update but not a full request to cache
275
+ void worker.storage.putResources(resourceDocument, resourceIdentifier => {
276
+ return store.cache.peek(resourceIdentifier);
277
+ });
278
+ } else if (document) {
279
+ void worker.storage.putDocument(document, resourceIdentifier => {
280
+ return store.cache.peek(resourceIdentifier);
281
+ });
282
+ }
283
+ }
284
+ function updateCacheForSuccess(store, request, document) {
285
+ let response = null;
286
+ if (isMutation(request)) {
287
+ const record = request.data?.record || request.records?.[0];
288
+ if (record) {
289
+ // @ts-expect-error while this is valid, we should update the CacheHandler for transactional saves
290
+ response = store.cache.didCommit(record, document);
291
+
292
+ // a mutation combined with a 204 has no cache impact when no known records were involved
293
+ // a createRecord with a 201 with an empty response and no known records should similarly
294
+ // have no cache impact
295
+ } else if (isCacheAffecting(document)) {
296
+ response = store.cache.put(document);
297
+ maybeUpdatePersistedCache(store, null, response);
298
+ }
299
+ } else {
300
+ response = store.cache.put(document);
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);
308
+ }
309
+ function handleFetchSuccess(store, request, identifier, document) {
310
+ let response;
311
+ assertPrivateStore(store);
312
+ store._join(() => {
313
+ response = updateCacheForSuccess(store, request, document);
314
+ });
315
+ if (store.lifetimes?.didRequest) {
316
+ store.lifetimes.didRequest(request, document.response, identifier, store);
317
+ }
318
+ return response;
319
+ }
320
+ function updateCacheForError(store, request, error) {
321
+ if (isMutation(request)) {
322
+ // TODO similar to didCommit we should spec this to be similar to cache.put for handling full response
323
+ // currently we let the response remain undefiend.
324
+ const errors = error && error.content && typeof error.content === 'object' && 'errors' in error.content && Array.isArray(error.content.errors) ? error.content.errors : undefined;
325
+ const record = request.data?.record || request.records?.[0];
326
+ store.cache.commitWasRejected(record, errors);
327
+ } else {
328
+ const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(request);
329
+ if (identifier) {
330
+ maybeUpdatePersistedCache(store, error);
331
+ }
332
+ return store.cache.put(error);
333
+ }
334
+ }
335
+ function handleFetchError(store, request, identifier, error) {
336
+ if (request.signal?.aborted) {
337
+ throw error;
338
+ }
339
+ assertPrivateStore(store);
340
+ let response;
341
+ store._join(() => {
342
+ response = updateCacheForError(store, request, error);
343
+ });
344
+ if (identifier && store.lifetimes?.didRequest) {
345
+ store.lifetimes.didRequest(request, error.response, identifier, store);
346
+ }
347
+ if (isMutation(request)) {
348
+ throw error;
349
+ }
350
+ const newError = cloneError(error);
351
+ newError.content = response;
352
+ throw newError;
353
+ }
354
+ function fetchContentAndHydrate(next, context, identifier) {
355
+ const {
356
+ request
357
+ } = context;
358
+ const {
359
+ store
360
+ } = context.request;
361
+ if (isMutation(request)) {
362
+ // TODO should we handle multiple records in request.records by iteratively calling willCommit for each
363
+ const record = request.data?.record || request.records?.[0];
364
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
365
+ if (!test) {
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));
377
+ }
378
+ export { CacheHandler, DataWorker };
@@ -0,0 +1,7 @@
1
+ import type { CacheHandler as CacheHandlerType } from "@warp-drive/core/request";
2
+ /**
3
+ * A simplified CacheHandler that hydrates ResourceDataDocuments from the cache
4
+ * with their referenced resources.
5
+ *
6
+ */
7
+ export declare const CacheHandler: CacheHandlerType;
@@ -0,0 +1,21 @@
1
+ import type { Context, Deferred, Future, NextFn } from "@warp-drive/core/request";
2
+ import type { AbortEventData, RequestEventData } from "./types.js";
3
+ interface PendingItem {
4
+ context: Context;
5
+ signal: AbortSignal | null;
6
+ abortFn: () => void;
7
+ deferred: Deferred<unknown>;
8
+ stack: string;
9
+ }
10
+ export declare class WorkerFetch {
11
+ worker: Worker | SharedWorker;
12
+ threadId: string;
13
+ pending: Map<number, PendingItem>;
14
+ channel: MessageChannel;
15
+ constructor(worker: Worker | SharedWorker | null);
16
+ cleanupRequest(id: number): PendingItem | undefined;
17
+ send(event: RequestEventData | AbortEventData): void;
18
+ request<T>(context: Context, next: NextFn<T>): Promise<T> | Future<T>;
19
+ }
20
+ export declare function enhanceReason(reason?: string): DOMException;
21
+ export {};
@@ -0,0 +1,31 @@
1
+ import type { RequestInfo, StructuredDataDocument, StructuredErrorDocument } from "@warp-drive/core/types/request";
2
+ export type SuccessResponseEventData<T> = {
3
+ type: "success-response";
4
+ thread: string;
5
+ id: number;
6
+ data: StructuredDataDocument<T>;
7
+ };
8
+ export type ErrorResponseEventData<T> = {
9
+ type: "error-response";
10
+ thread: string;
11
+ id: number;
12
+ data: StructuredErrorDocument<T>;
13
+ };
14
+ export type RequestEventData = {
15
+ type: "request";
16
+ thread: string;
17
+ id: number;
18
+ data: RequestInfo;
19
+ };
20
+ export type AbortEventData = {
21
+ type: "abort";
22
+ thread: string;
23
+ id: number;
24
+ data: string;
25
+ };
26
+ export type ThreadInitEventData = {
27
+ type: "connect";
28
+ thread: string;
29
+ };
30
+ export type MainThreadEvent<T> = MessageEvent<SuccessResponseEventData<T> | ErrorResponseEventData<T>>;
31
+ export type WorkerThreadEvent = MessageEvent<RequestEventData> | MessageEvent<ThreadInitEventData> | MessageEvent<AbortEventData>;
@@ -0,0 +1,12 @@
1
+ import type { ImmutableCreateRequestOptions, ImmutableDeleteRequestOptions, ImmutableRequestInfo, ImmutableUpdateRequestOptions, StructuredDataDocument } from "@warp-drive/core/types/request";
2
+ import type { ApiError } from "@warp-drive/core/types/spec/error";
3
+ export declare const MUTATION_OPS: Set<string>;
4
+ export declare function isMutation(request: Partial<ImmutableRequestInfo>): request is ImmutableUpdateRequestOptions | ImmutableCreateRequestOptions | ImmutableDeleteRequestOptions;
5
+ export declare function isCacheAffecting<T>(document: StructuredDataDocument<T>): boolean;
6
+ type RobustError = Error & {
7
+ error: string | object;
8
+ errors?: ApiError[];
9
+ content?: unknown;
10
+ };
11
+ export declare function cloneError(error: RobustError): RobustError;
12
+ export {};
@@ -0,0 +1,23 @@
1
+ import type { Store } from "@warp-drive/core";
2
+ import type { Future } from "@warp-drive/core/request";
3
+ import { DocumentStorage } from "../document-storage.js";
4
+ import type { AbortEventData, RequestEventData } from "./types.js";
5
+ export declare class DataWorker {
6
+ store: Store;
7
+ threads: Map<string, MessagePort>;
8
+ pending: Map<string, Map<number, Future<unknown>>>;
9
+ isSharedWorker: boolean;
10
+ options: {
11
+ persisted: boolean;
12
+ scope?: string;
13
+ };
14
+ storage: DocumentStorage;
15
+ constructor(UserStore: typeof Store, options?: {
16
+ persisted: boolean;
17
+ scope?: string;
18
+ });
19
+ initialize(): void;
20
+ setupThread(thread: string, port: MessagePort): void;
21
+ abortRequest(event: AbortEventData): void;
22
+ request(event: RequestEventData): Promise<void>;
23
+ }
@@ -0,0 +1,2 @@
1
+ export { DataWorker } from "./data-worker/worker.js";
2
+ export { CacheHandler } from "./data-worker/cache-handler.js";