@warp-drive-mirror/experiments 0.2.6-alpha.37 → 0.2.6-alpha.40
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/dist/unpkg/dev/data-worker.js +10 -8
- package/dist/unpkg/dev/document-storage.js +349 -1
- package/dist/unpkg/dev/image-fetch.js +2 -8
- package/dist/unpkg/dev/image-worker.js +2 -1
- package/dist/unpkg/dev/worker-fetch.js +2 -8
- package/dist/unpkg/dev-deprecated/data-worker.js +10 -8
- package/dist/unpkg/dev-deprecated/document-storage.js +349 -1
- package/dist/unpkg/dev-deprecated/image-fetch.js +2 -8
- package/dist/unpkg/dev-deprecated/image-worker.js +2 -1
- package/dist/unpkg/dev-deprecated/worker-fetch.js +2 -8
- package/dist/unpkg/prod/data-worker.js +5 -17
- package/dist/unpkg/prod/document-storage.js +339 -1
- package/dist/unpkg/prod/image-fetch.js +2 -8
- package/dist/unpkg/prod/image-worker.js +2 -1
- package/dist/unpkg/prod/worker-fetch.js +2 -8
- package/dist/unpkg/prod-deprecated/data-worker.js +5 -17
- package/dist/unpkg/prod-deprecated/document-storage.js +339 -1
- package/dist/unpkg/prod-deprecated/image-fetch.js +2 -8
- package/dist/unpkg/prod-deprecated/image-worker.js +2 -1
- package/dist/unpkg/prod-deprecated/worker-fetch.js +2 -8
- package/package.json +24 -44
- package/dist/unpkg/dev/declarations/data-worker/cache-handler.d.ts +0 -7
- package/dist/unpkg/dev/declarations/data-worker/fetch.d.ts +0 -21
- package/dist/unpkg/dev/declarations/data-worker/types.d.ts +0 -31
- package/dist/unpkg/dev/declarations/data-worker/utils.d.ts +0 -12
- package/dist/unpkg/dev/declarations/data-worker/worker.d.ts +0 -23
- package/dist/unpkg/dev/declarations/data-worker.d.ts +0 -2
- package/dist/unpkg/dev/declarations/document-storage/index.d.ts +0 -90
- package/dist/unpkg/dev/declarations/document-storage.d.ts +0 -1
- package/dist/unpkg/dev/declarations/image-fetch.d.ts +0 -1
- package/dist/unpkg/dev/declarations/image-worker/fetch.d.ts +0 -18
- package/dist/unpkg/dev/declarations/image-worker/types.d.ts +0 -21
- package/dist/unpkg/dev/declarations/image-worker/worker.d.ts +0 -14
- package/dist/unpkg/dev/declarations/image-worker.d.ts +0 -1
- package/dist/unpkg/dev/declarations/worker-fetch.d.ts +0 -1
- package/dist/unpkg/dev/index-CGCX7hY2.js +0 -349
- package/dist/unpkg/dev-deprecated/declarations/data-worker/cache-handler.d.ts +0 -7
- package/dist/unpkg/dev-deprecated/declarations/data-worker/fetch.d.ts +0 -21
- package/dist/unpkg/dev-deprecated/declarations/data-worker/types.d.ts +0 -31
- package/dist/unpkg/dev-deprecated/declarations/data-worker/utils.d.ts +0 -12
- package/dist/unpkg/dev-deprecated/declarations/data-worker/worker.d.ts +0 -23
- package/dist/unpkg/dev-deprecated/declarations/data-worker.d.ts +0 -2
- package/dist/unpkg/dev-deprecated/declarations/document-storage/index.d.ts +0 -90
- package/dist/unpkg/dev-deprecated/declarations/document-storage.d.ts +0 -1
- package/dist/unpkg/dev-deprecated/declarations/image-fetch.d.ts +0 -1
- package/dist/unpkg/dev-deprecated/declarations/image-worker/fetch.d.ts +0 -18
- package/dist/unpkg/dev-deprecated/declarations/image-worker/types.d.ts +0 -21
- package/dist/unpkg/dev-deprecated/declarations/image-worker/worker.d.ts +0 -14
- package/dist/unpkg/dev-deprecated/declarations/image-worker.d.ts +0 -1
- package/dist/unpkg/dev-deprecated/declarations/worker-fetch.d.ts +0 -1
- package/dist/unpkg/dev-deprecated/index-CGCX7hY2.js +0 -349
- package/dist/unpkg/prod/declarations/data-worker/cache-handler.d.ts +0 -7
- package/dist/unpkg/prod/declarations/data-worker/fetch.d.ts +0 -21
- package/dist/unpkg/prod/declarations/data-worker/types.d.ts +0 -31
- package/dist/unpkg/prod/declarations/data-worker/utils.d.ts +0 -12
- package/dist/unpkg/prod/declarations/data-worker/worker.d.ts +0 -23
- package/dist/unpkg/prod/declarations/data-worker.d.ts +0 -2
- package/dist/unpkg/prod/declarations/document-storage/index.d.ts +0 -90
- package/dist/unpkg/prod/declarations/document-storage.d.ts +0 -1
- package/dist/unpkg/prod/declarations/image-fetch.d.ts +0 -1
- package/dist/unpkg/prod/declarations/image-worker/fetch.d.ts +0 -18
- package/dist/unpkg/prod/declarations/image-worker/types.d.ts +0 -21
- package/dist/unpkg/prod/declarations/image-worker/worker.d.ts +0 -14
- package/dist/unpkg/prod/declarations/image-worker.d.ts +0 -1
- package/dist/unpkg/prod/declarations/worker-fetch.d.ts +0 -1
- package/dist/unpkg/prod/index-CGCX7hY2.js +0 -349
- package/dist/unpkg/prod-deprecated/declarations/data-worker/cache-handler.d.ts +0 -7
- package/dist/unpkg/prod-deprecated/declarations/data-worker/fetch.d.ts +0 -21
- package/dist/unpkg/prod-deprecated/declarations/data-worker/types.d.ts +0 -31
- package/dist/unpkg/prod-deprecated/declarations/data-worker/utils.d.ts +0 -12
- package/dist/unpkg/prod-deprecated/declarations/data-worker/worker.d.ts +0 -23
- package/dist/unpkg/prod-deprecated/declarations/data-worker.d.ts +0 -2
- package/dist/unpkg/prod-deprecated/declarations/document-storage/index.d.ts +0 -90
- package/dist/unpkg/prod-deprecated/declarations/document-storage.d.ts +0 -1
- package/dist/unpkg/prod-deprecated/declarations/image-fetch.d.ts +0 -1
- package/dist/unpkg/prod-deprecated/declarations/image-worker/fetch.d.ts +0 -18
- package/dist/unpkg/prod-deprecated/declarations/image-worker/types.d.ts +0 -21
- package/dist/unpkg/prod-deprecated/declarations/image-worker/worker.d.ts +0 -14
- package/dist/unpkg/prod-deprecated/declarations/image-worker.d.ts +0 -1
- package/dist/unpkg/prod-deprecated/declarations/worker-fetch.d.ts +0 -1
- package/dist/unpkg/prod-deprecated/index-CGCX7hY2.js +0 -349
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DocumentStorage } from './document-storage.js';
|
|
2
2
|
import { assertPrivateStore } from '@warp-drive-mirror/core/store/-private';
|
|
3
3
|
import { SkipCache } from '@warp-drive-mirror/core/types/request';
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
const WorkerScope = globalThis.SharedWorkerGlobalScope;
|
|
6
6
|
class DataWorker {
|
|
7
7
|
constructor(UserStore, options) {
|
|
@@ -147,6 +147,7 @@ function prepareRequest(event) {
|
|
|
147
147
|
}
|
|
148
148
|
return event;
|
|
149
149
|
}
|
|
150
|
+
|
|
150
151
|
const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
|
|
151
152
|
|
|
152
153
|
/**
|
|
@@ -217,7 +218,7 @@ const CacheHandler = {
|
|
|
217
218
|
}
|
|
218
219
|
return completeRequest(identifier, store, context, next);
|
|
219
220
|
}).catch(e => {
|
|
220
|
-
|
|
221
|
+
{
|
|
221
222
|
// eslint-disable-next-line no-console
|
|
222
223
|
console.log('Unable to retrieve document from persisted storage', e);
|
|
223
224
|
}
|
|
@@ -235,11 +236,11 @@ function completeRequest(identifier, store, context, next) {
|
|
|
235
236
|
if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
|
|
236
237
|
return fetchContentAndHydrate(next, context, identifier);
|
|
237
238
|
}
|
|
238
|
-
|
|
239
|
+
(test => {
|
|
239
240
|
if (!test) {
|
|
240
241
|
throw new Error(`Expected a peeked request to be present`);
|
|
241
242
|
}
|
|
242
|
-
})(peeked)
|
|
243
|
+
})(peeked);
|
|
243
244
|
context.setResponse(peeked.response);
|
|
244
245
|
if ('error' in peeked) {
|
|
245
246
|
throw peeked;
|
|
@@ -361,11 +362,11 @@ function fetchContentAndHydrate(next, context, identifier) {
|
|
|
361
362
|
if (isMutation(request)) {
|
|
362
363
|
// TODO should we handle multiple records in request.records by iteratively calling willCommit for each
|
|
363
364
|
const record = request.data?.record || request.records?.[0];
|
|
364
|
-
|
|
365
|
+
(test => {
|
|
365
366
|
if (!test) {
|
|
366
367
|
throw new Error(`Expected to receive a list of records included in the ${request.op} request`);
|
|
367
368
|
}
|
|
368
|
-
})(record)
|
|
369
|
+
})(record);
|
|
369
370
|
if (record) {
|
|
370
371
|
store.cache.willCommit(record, context);
|
|
371
372
|
}
|
|
@@ -375,4 +376,5 @@ function fetchContentAndHydrate(next, context, identifier) {
|
|
|
375
376
|
}
|
|
376
377
|
return next(request).then(document => handleFetchSuccess(store, request, identifier, document), error => handleFetchError(store, request, identifier, error));
|
|
377
378
|
}
|
|
378
|
-
|
|
379
|
+
|
|
380
|
+
export { CacheHandler, DataWorker };
|
|
@@ -1 +1,349 @@
|
|
|
1
|
-
|
|
1
|
+
const WARP_DRIVE_STORAGE_FILE_NAME = 'warp-drive_document-storage';
|
|
2
|
+
const WARP_DRIVE_STORAGE_VERSION = 1;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* DocumentStorage is specifically designed around WarpDrive Cache and Request concepts.
|
|
6
|
+
*
|
|
7
|
+
* CacheFileDocument is a StructuredDocument (request response) whose `content` is
|
|
8
|
+
* the ResourceDocument returned by inserting the request into a Store's Cache.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A CacheDocument is a reconstructed request response that rehydrates ResourceDocument
|
|
13
|
+
* with the associated resources based on their identifiers.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
class InternalDocumentStorage {
|
|
17
|
+
constructor(options) {
|
|
18
|
+
this.options = options;
|
|
19
|
+
this._lastModified = 0;
|
|
20
|
+
this._invalidated = true;
|
|
21
|
+
this._fileHandle = this._open(options.scope);
|
|
22
|
+
this._channel = Object.assign(new BroadcastChannel(options.scope), {
|
|
23
|
+
onmessage: this._onMessage.bind(this)
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
_onMessage(_event) {
|
|
27
|
+
this._invalidated = true;
|
|
28
|
+
}
|
|
29
|
+
async _open(scope) {
|
|
30
|
+
const directoryHandle = await navigator.storage.getDirectory();
|
|
31
|
+
const fileHandle = await directoryHandle.getFileHandle(scope, {
|
|
32
|
+
create: true
|
|
33
|
+
});
|
|
34
|
+
return fileHandle;
|
|
35
|
+
}
|
|
36
|
+
async _read() {
|
|
37
|
+
if (this._filePromise) {
|
|
38
|
+
return this._filePromise;
|
|
39
|
+
}
|
|
40
|
+
if (this._invalidated) {
|
|
41
|
+
const updateFile = async () => {
|
|
42
|
+
const fileHandle = await this._fileHandle;
|
|
43
|
+
const file = await fileHandle.getFile();
|
|
44
|
+
const lastModified = file.lastModified;
|
|
45
|
+
if (lastModified === this._lastModified && this._cache) {
|
|
46
|
+
return this._cache;
|
|
47
|
+
}
|
|
48
|
+
const contents = await file.text();
|
|
49
|
+
const cache = contents ? JSON.parse(contents) : {
|
|
50
|
+
documents: [],
|
|
51
|
+
resources: []
|
|
52
|
+
};
|
|
53
|
+
const documents = new Map(cache.documents);
|
|
54
|
+
const resources = new Map(cache.resources);
|
|
55
|
+
const cacheMap = {
|
|
56
|
+
documents,
|
|
57
|
+
resources
|
|
58
|
+
};
|
|
59
|
+
this._cache = cacheMap;
|
|
60
|
+
this._invalidated = false;
|
|
61
|
+
this._lastModified = lastModified;
|
|
62
|
+
return cacheMap;
|
|
63
|
+
};
|
|
64
|
+
this._filePromise = updateFile();
|
|
65
|
+
await this._filePromise;
|
|
66
|
+
this._filePromise = null;
|
|
67
|
+
}
|
|
68
|
+
return this._cache;
|
|
69
|
+
}
|
|
70
|
+
async _patch(documentKey, document, updatedResources) {
|
|
71
|
+
const fileHandle = await this._fileHandle;
|
|
72
|
+
// secure a lock before getting latest state
|
|
73
|
+
const writable = await fileHandle.createWritable();
|
|
74
|
+
const cache = await this._read();
|
|
75
|
+
cache.documents.set(documentKey, document);
|
|
76
|
+
updatedResources.forEach((resource, key) => {
|
|
77
|
+
cache.resources.set(key, resource);
|
|
78
|
+
});
|
|
79
|
+
const documents = [...cache.documents.entries()];
|
|
80
|
+
const resources = [...cache.resources.entries()];
|
|
81
|
+
const cacheFile = {
|
|
82
|
+
documents,
|
|
83
|
+
resources
|
|
84
|
+
};
|
|
85
|
+
await writable.write(JSON.stringify(cacheFile));
|
|
86
|
+
await writable.close();
|
|
87
|
+
this._channel.postMessage({
|
|
88
|
+
type: 'patch',
|
|
89
|
+
key: documentKey,
|
|
90
|
+
resources: [...updatedResources.keys()]
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
async getDocument(key) {
|
|
94
|
+
const cache = await this._read();
|
|
95
|
+
// clone the document to avoid leaking the internal cache
|
|
96
|
+
const document = safeDocumentHydrate(cache.documents.get(key.lid));
|
|
97
|
+
if (!document) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// expand the document with the resources
|
|
102
|
+
if (document.content) {
|
|
103
|
+
if (docHasData(document.content)) {
|
|
104
|
+
let data = null;
|
|
105
|
+
if (Array.isArray(document.content.data)) {
|
|
106
|
+
data = document.content.data.map(resourceIdentifier => {
|
|
107
|
+
const resource = cache.resources.get(resourceIdentifier.lid);
|
|
108
|
+
if (!resource) {
|
|
109
|
+
throw new Error(`Resource not found for ${resourceIdentifier.lid}`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// clone the resource to avoid leaking the internal cache
|
|
113
|
+
return structuredClone(resource);
|
|
114
|
+
});
|
|
115
|
+
} else if (document.content.data) {
|
|
116
|
+
const resource = cache.resources.get(document.content.data.lid);
|
|
117
|
+
if (!resource) {
|
|
118
|
+
throw new Error(`Resource not found for ${document.content.data.lid}`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// clone the resource to avoid leaking the internal cache
|
|
122
|
+
data = structuredClone(resource);
|
|
123
|
+
}
|
|
124
|
+
if (document.content.included) {
|
|
125
|
+
const included = document.content.included.map(resourceIdentifier => {
|
|
126
|
+
const resource = cache.resources.get(resourceIdentifier.lid);
|
|
127
|
+
if (!resource) {
|
|
128
|
+
throw new Error(`Resource not found for ${resourceIdentifier.lid}`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// clone the resource to avoid leaking the internal cache
|
|
132
|
+
return structuredClone(resource);
|
|
133
|
+
});
|
|
134
|
+
document.content.included = included;
|
|
135
|
+
}
|
|
136
|
+
document.content.data = data;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return document;
|
|
140
|
+
}
|
|
141
|
+
async putDocument(document, resourceCollector) {
|
|
142
|
+
const resources = new Map();
|
|
143
|
+
if (!document.content) {
|
|
144
|
+
throw new Error(`Document content is missing, only finalized documents can be stored`);
|
|
145
|
+
}
|
|
146
|
+
if (!document.content.lid) {
|
|
147
|
+
throw new Error(`Document content is missing a lid, only documents with a cache-key can be stored`);
|
|
148
|
+
}
|
|
149
|
+
if (docHasData(document.content)) {
|
|
150
|
+
this._getResources(document.content, resourceCollector, resources);
|
|
151
|
+
}
|
|
152
|
+
await this._patch(document.content.lid, safeDocumentSerialize(document), resources);
|
|
153
|
+
}
|
|
154
|
+
_getResources(document, resourceCollector, resources = new Map()) {
|
|
155
|
+
if (Array.isArray(document.data)) {
|
|
156
|
+
document.data.forEach(resourceIdentifier => {
|
|
157
|
+
const resource = resourceCollector(resourceIdentifier);
|
|
158
|
+
resources.set(resourceIdentifier.lid, structuredClone(resource));
|
|
159
|
+
});
|
|
160
|
+
} else if (document.data) {
|
|
161
|
+
const resource = resourceCollector(document.data);
|
|
162
|
+
resources.set(document.data.lid, structuredClone(resource));
|
|
163
|
+
}
|
|
164
|
+
if (document.included) {
|
|
165
|
+
document.included.forEach(resourceIdentifier => {
|
|
166
|
+
const resource = resourceCollector(resourceIdentifier);
|
|
167
|
+
resources.set(resourceIdentifier.lid, structuredClone(resource));
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return resources;
|
|
171
|
+
}
|
|
172
|
+
async putResources(document, resourceCollector) {
|
|
173
|
+
const fileHandle = await this._fileHandle;
|
|
174
|
+
// secure a lock before getting latest state
|
|
175
|
+
const writable = await fileHandle.createWritable();
|
|
176
|
+
const cache = await this._read();
|
|
177
|
+
const updatedResources = this._getResources(document, resourceCollector);
|
|
178
|
+
updatedResources.forEach((resource, key) => {
|
|
179
|
+
cache.resources.set(key, resource);
|
|
180
|
+
});
|
|
181
|
+
const documents = [...cache.documents.entries()];
|
|
182
|
+
const resources = [...cache.resources.entries()];
|
|
183
|
+
const cacheFile = {
|
|
184
|
+
documents,
|
|
185
|
+
resources
|
|
186
|
+
};
|
|
187
|
+
await writable.write(JSON.stringify(cacheFile));
|
|
188
|
+
await writable.close();
|
|
189
|
+
this._channel.postMessage({
|
|
190
|
+
type: 'patch',
|
|
191
|
+
key: null,
|
|
192
|
+
resources: [...updatedResources.keys()]
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
async clear(reset) {
|
|
196
|
+
const fileHandle = await this._fileHandle;
|
|
197
|
+
const writable = await fileHandle.createWritable();
|
|
198
|
+
await writable.write('');
|
|
199
|
+
await writable.close();
|
|
200
|
+
this._invalidated = true;
|
|
201
|
+
this._lastModified = 0;
|
|
202
|
+
this._cache = null;
|
|
203
|
+
this._filePromise = null;
|
|
204
|
+
this._channel.postMessage({
|
|
205
|
+
type: 'clear'
|
|
206
|
+
});
|
|
207
|
+
if (!reset) {
|
|
208
|
+
this._channel.close();
|
|
209
|
+
this._channel = null;
|
|
210
|
+
if (!this.options.isolated) {
|
|
211
|
+
Storages.delete(this.options.scope);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function safeDocumentSerialize(document) {
|
|
217
|
+
(test => {
|
|
218
|
+
if (!test) {
|
|
219
|
+
throw new Error(`Expected to receive a document`);
|
|
220
|
+
}
|
|
221
|
+
})(document && typeof document === 'object');
|
|
222
|
+
const doc = document;
|
|
223
|
+
const newDoc = {};
|
|
224
|
+
if ('request' in doc) {
|
|
225
|
+
newDoc.request = prepareRequest(doc.request);
|
|
226
|
+
}
|
|
227
|
+
if ('response' in doc) {
|
|
228
|
+
newDoc.response = prepareResponse(doc.response);
|
|
229
|
+
}
|
|
230
|
+
if ('content' in doc) {
|
|
231
|
+
newDoc.content = structuredClone(doc.content);
|
|
232
|
+
}
|
|
233
|
+
return newDoc;
|
|
234
|
+
}
|
|
235
|
+
function prepareRequest(request) {
|
|
236
|
+
const {
|
|
237
|
+
signal,
|
|
238
|
+
headers
|
|
239
|
+
} = request;
|
|
240
|
+
const requestCopy = Object.assign({}, request);
|
|
241
|
+
delete requestCopy.store;
|
|
242
|
+
if (signal instanceof AbortSignal) {
|
|
243
|
+
delete requestCopy.signal;
|
|
244
|
+
}
|
|
245
|
+
if (headers instanceof Headers) {
|
|
246
|
+
requestCopy.headers = Array.from(headers);
|
|
247
|
+
}
|
|
248
|
+
return requestCopy;
|
|
249
|
+
}
|
|
250
|
+
function prepareResponse(response) {
|
|
251
|
+
if (!response) return null;
|
|
252
|
+
const clone = {};
|
|
253
|
+
if (response.headers) {
|
|
254
|
+
clone.headers = Array.from(response.headers);
|
|
255
|
+
}
|
|
256
|
+
clone.ok = response.ok;
|
|
257
|
+
clone.redirected = response.redirected;
|
|
258
|
+
clone.status = response.status;
|
|
259
|
+
clone.statusText = response.statusText;
|
|
260
|
+
clone.type = response.type;
|
|
261
|
+
clone.url = response.url;
|
|
262
|
+
return clone;
|
|
263
|
+
}
|
|
264
|
+
function safeDocumentHydrate(document) {
|
|
265
|
+
(test => {
|
|
266
|
+
if (!test) {
|
|
267
|
+
throw new Error(`Expected to receive a document`);
|
|
268
|
+
}
|
|
269
|
+
})(document && typeof document === 'object');
|
|
270
|
+
const doc = document;
|
|
271
|
+
const newDoc = {};
|
|
272
|
+
if ('request' in doc) {
|
|
273
|
+
const headers = new Headers(doc.request.headers);
|
|
274
|
+
const req = Object.assign({}, doc.request, {
|
|
275
|
+
headers
|
|
276
|
+
});
|
|
277
|
+
newDoc.request = new Request(doc.request.url ?? '', req);
|
|
278
|
+
}
|
|
279
|
+
if ('response' in doc) {
|
|
280
|
+
const headers = new Headers(doc.response.headers);
|
|
281
|
+
const resp = Object.assign({}, doc.response, {
|
|
282
|
+
headers
|
|
283
|
+
});
|
|
284
|
+
newDoc.response = new Response(null, resp);
|
|
285
|
+
}
|
|
286
|
+
if ('content' in doc) {
|
|
287
|
+
newDoc.content = structuredClone(doc.content);
|
|
288
|
+
}
|
|
289
|
+
return newDoc;
|
|
290
|
+
}
|
|
291
|
+
function docHasData(doc) {
|
|
292
|
+
return 'data' in doc;
|
|
293
|
+
}
|
|
294
|
+
const Storages = new Map();
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* DocumentStorage is a wrapper around the StorageManager API that provides
|
|
298
|
+
* a simple interface for reading and updating documents and requests.
|
|
299
|
+
*
|
|
300
|
+
* Some goals for this experiment:
|
|
301
|
+
*
|
|
302
|
+
* - optimize for storing requests/documents
|
|
303
|
+
* - optimize for storing resources
|
|
304
|
+
* - optimize for looking up resources associated to a document
|
|
305
|
+
* - optimize for notifying cross-tab when data is updated
|
|
306
|
+
*
|
|
307
|
+
* optional features:
|
|
308
|
+
*
|
|
309
|
+
* - support for offline mode
|
|
310
|
+
* - ?? support for relationship based cache traversal
|
|
311
|
+
* - a way to index records by type + another field (e.g updatedAt/createAt/name)
|
|
312
|
+
* such that simple queries can be done without having to scan all records
|
|
313
|
+
*/
|
|
314
|
+
class DocumentStorage {
|
|
315
|
+
constructor(options = {}) {
|
|
316
|
+
options.isolated = options.isolated ?? false;
|
|
317
|
+
options.scope = options.scope ?? 'default';
|
|
318
|
+
const fileName = `${WARP_DRIVE_STORAGE_FILE_NAME}@version_${WARP_DRIVE_STORAGE_VERSION}:${options.scope}`;
|
|
319
|
+
if (!options.isolated && Storages.has(fileName)) {
|
|
320
|
+
const storage = Storages.get(fileName);
|
|
321
|
+
if (storage) {
|
|
322
|
+
this._storage = storage.deref();
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
const storage = new InternalDocumentStorage({
|
|
327
|
+
scope: fileName,
|
|
328
|
+
isolated: options.isolated
|
|
329
|
+
});
|
|
330
|
+
this._storage = storage;
|
|
331
|
+
if (!options.isolated) {
|
|
332
|
+
Storages.set(fileName, new WeakRef(storage));
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
getDocument(key) {
|
|
336
|
+
return this._storage.getDocument(key);
|
|
337
|
+
}
|
|
338
|
+
putDocument(document, resourceCollector) {
|
|
339
|
+
return this._storage.putDocument(document, resourceCollector);
|
|
340
|
+
}
|
|
341
|
+
putResources(document, resourceCollector) {
|
|
342
|
+
return this._storage.putResources(document, resourceCollector);
|
|
343
|
+
}
|
|
344
|
+
clear(reset) {
|
|
345
|
+
return this._storage.clear(reset);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export { DocumentStorage };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createDeferred } from '@warp-drive-mirror/core/request';
|
|
2
|
-
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
3
2
|
|
|
4
3
|
// @ts-expect-error untyped global
|
|
5
4
|
const isServerEnv = typeof FastBoot !== 'undefined';
|
|
@@ -8,12 +7,6 @@ class ImageFetch {
|
|
|
8
7
|
this.threadId = isServerEnv ? '' : crypto.randomUUID();
|
|
9
8
|
this.pending = new Map();
|
|
10
9
|
this.cache = new Map();
|
|
11
|
-
const isTesting = macroCondition(getGlobalConfig().WarpDriveMirror.env.TESTING) ? true : false;
|
|
12
|
-
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
13
|
-
if (!test) {
|
|
14
|
-
throw new Error(`Expected a SharedWorker instance`);
|
|
15
|
-
}
|
|
16
|
-
})(isTesting || isServerEnv || worker instanceof SharedWorker) : {};
|
|
17
10
|
this.worker = worker;
|
|
18
11
|
if (!isServerEnv) {
|
|
19
12
|
const fn = event => {
|
|
@@ -77,4 +70,5 @@ class ImageFetch {
|
|
|
77
70
|
return deferred.promise;
|
|
78
71
|
}
|
|
79
72
|
}
|
|
80
|
-
|
|
73
|
+
|
|
74
|
+
export { ImageFetch };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createDeferred } from '@warp-drive-mirror/core/request';
|
|
2
|
-
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
3
2
|
|
|
4
3
|
// @ts-expect-error untyped global
|
|
5
4
|
const isServerEnv = typeof FastBoot !== 'undefined';
|
|
@@ -26,12 +25,6 @@ class WorkerFetch {
|
|
|
26
25
|
constructor(worker) {
|
|
27
26
|
this.threadId = isServerEnv ? '' : crypto.randomUUID();
|
|
28
27
|
this.pending = new Map();
|
|
29
|
-
const isTesting = macroCondition(getGlobalConfig().WarpDriveMirror.env.TESTING) ? true : false;
|
|
30
|
-
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
31
|
-
if (!test) {
|
|
32
|
-
throw new Error(`Expected a SharedWorker instance`);
|
|
33
|
-
}
|
|
34
|
-
})(isTesting || isServerEnv || worker instanceof SharedWorker) : {};
|
|
35
28
|
this.worker = worker;
|
|
36
29
|
if (!isServerEnv) {
|
|
37
30
|
const fn = event => {
|
|
@@ -161,4 +154,5 @@ function prepareRequest(request) {
|
|
|
161
154
|
request: requestCopy
|
|
162
155
|
};
|
|
163
156
|
}
|
|
164
|
-
|
|
157
|
+
|
|
158
|
+
export { WorkerFetch };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DocumentStorage } from './document-storage.js';
|
|
2
2
|
import { assertPrivateStore } from '@warp-drive-mirror/core/store/-private';
|
|
3
3
|
import { SkipCache } from '@warp-drive-mirror/core/types/request';
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
const WorkerScope = globalThis.SharedWorkerGlobalScope;
|
|
6
6
|
class DataWorker {
|
|
7
7
|
constructor(UserStore, options) {
|
|
@@ -147,6 +147,7 @@ function prepareRequest(event) {
|
|
|
147
147
|
}
|
|
148
148
|
return event;
|
|
149
149
|
}
|
|
150
|
+
|
|
150
151
|
const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
|
|
151
152
|
|
|
152
153
|
/**
|
|
@@ -217,7 +218,7 @@ const CacheHandler = {
|
|
|
217
218
|
}
|
|
218
219
|
return completeRequest(identifier, store, context, next);
|
|
219
220
|
}).catch(e => {
|
|
220
|
-
|
|
221
|
+
{
|
|
221
222
|
// eslint-disable-next-line no-console
|
|
222
223
|
console.log('Unable to retrieve document from persisted storage', e);
|
|
223
224
|
}
|
|
@@ -235,11 +236,11 @@ function completeRequest(identifier, store, context, next) {
|
|
|
235
236
|
if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
|
|
236
237
|
return fetchContentAndHydrate(next, context, identifier);
|
|
237
238
|
}
|
|
238
|
-
|
|
239
|
+
(test => {
|
|
239
240
|
if (!test) {
|
|
240
241
|
throw new Error(`Expected a peeked request to be present`);
|
|
241
242
|
}
|
|
242
|
-
})(peeked)
|
|
243
|
+
})(peeked);
|
|
243
244
|
context.setResponse(peeked.response);
|
|
244
245
|
if ('error' in peeked) {
|
|
245
246
|
throw peeked;
|
|
@@ -361,11 +362,11 @@ function fetchContentAndHydrate(next, context, identifier) {
|
|
|
361
362
|
if (isMutation(request)) {
|
|
362
363
|
// TODO should we handle multiple records in request.records by iteratively calling willCommit for each
|
|
363
364
|
const record = request.data?.record || request.records?.[0];
|
|
364
|
-
|
|
365
|
+
(test => {
|
|
365
366
|
if (!test) {
|
|
366
367
|
throw new Error(`Expected to receive a list of records included in the ${request.op} request`);
|
|
367
368
|
}
|
|
368
|
-
})(record)
|
|
369
|
+
})(record);
|
|
369
370
|
if (record) {
|
|
370
371
|
store.cache.willCommit(record, context);
|
|
371
372
|
}
|
|
@@ -375,4 +376,5 @@ function fetchContentAndHydrate(next, context, identifier) {
|
|
|
375
376
|
}
|
|
376
377
|
return next(request).then(document => handleFetchSuccess(store, request, identifier, document), error => handleFetchError(store, request, identifier, error));
|
|
377
378
|
}
|
|
378
|
-
|
|
379
|
+
|
|
380
|
+
export { CacheHandler, DataWorker };
|