@isograph/react 0.0.0-main-3f26c9c8 → 0.0.0-main-84b67b62
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/.turbo/turbo-compile-libs.log +1 -1
- package/dist/core/IsographEnvironment.d.ts +9 -6
- package/dist/core/IsographEnvironment.d.ts.map +1 -1
- package/dist/core/IsographEnvironment.js +7 -1
- package/dist/core/cache.d.ts +2 -1
- package/dist/core/cache.d.ts.map +1 -1
- package/dist/core/cache.js +17 -27
- package/dist/core/check.d.ts.map +1 -1
- package/dist/core/check.js +10 -7
- package/dist/core/garbageCollection.d.ts +2 -1
- package/dist/core/garbageCollection.d.ts.map +1 -1
- package/dist/core/garbageCollection.js +21 -13
- package/dist/core/logging.d.ts +3 -2
- package/dist/core/logging.d.ts.map +1 -1
- package/dist/core/makeNetworkRequest.d.ts.map +1 -1
- package/dist/core/makeNetworkRequest.js +10 -1
- package/dist/core/optimisticProxy.d.ts +51 -0
- package/dist/core/optimisticProxy.d.ts.map +1 -0
- package/dist/core/optimisticProxy.js +372 -0
- package/dist/core/read.d.ts.map +1 -1
- package/dist/core/read.js +5 -4
- package/dist/core/startUpdate.d.ts +2 -1
- package/dist/core/startUpdate.d.ts.map +1 -1
- package/dist/core/startUpdate.js +31 -32
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/core/IsographEnvironment.ts +16 -6
- package/src/core/cache.ts +21 -14
- package/src/core/check.ts +11 -6
- package/src/core/garbageCollection.ts +27 -15
- package/src/core/logging.ts +2 -2
- package/src/core/makeNetworkRequest.ts +14 -1
- package/src/core/optimisticProxy.ts +510 -0
- package/src/core/read.ts +2 -1
- package/src/core/startUpdate.ts +44 -28
- package/src/index.ts +1 -1
- package/src/tests/garbageCollection.test.ts +2 -2
- package/src/tests/normalizeData.test.ts +5 -3
- package/src/tests/optimisticProxy.test.ts +860 -0
- package/src/tests/startUpdate.test.ts +7 -5
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
import {
|
|
2
|
+
callSubscriptions,
|
|
3
|
+
insertEmptySetIfMissing,
|
|
4
|
+
type EncounteredIds,
|
|
5
|
+
} from './cache';
|
|
6
|
+
import type {
|
|
7
|
+
BaseStoreLayerData,
|
|
8
|
+
IsographEnvironment,
|
|
9
|
+
StoreLayerData,
|
|
10
|
+
StoreLink,
|
|
11
|
+
StoreRecord,
|
|
12
|
+
} from './IsographEnvironment';
|
|
13
|
+
|
|
14
|
+
export function getOrInsertRecord(
|
|
15
|
+
dataLayer: StoreLayerData,
|
|
16
|
+
link: StoreLink,
|
|
17
|
+
): StoreRecord {
|
|
18
|
+
const recordsById = (dataLayer[link.__typename] ??= {});
|
|
19
|
+
return (recordsById[link.__link] ??= {});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Given the child-most store layer (i.e. environment.store) and a link (identifying a
|
|
24
|
+
* store record), create a proxy object that attempts to read through each successive
|
|
25
|
+
* store layer until a value (i.e. field name) is found. If found, return that value.
|
|
26
|
+
*/
|
|
27
|
+
export function getStoreRecordProxy(
|
|
28
|
+
storeLayer: StoreLayer,
|
|
29
|
+
link: StoreLink,
|
|
30
|
+
): Readonly<StoreRecord> | null | undefined {
|
|
31
|
+
let startNode: StoreLayer | null = storeLayer;
|
|
32
|
+
while (startNode !== null) {
|
|
33
|
+
const storeRecord = startNode.data[link.__typename]?.[link.__link];
|
|
34
|
+
if (storeRecord === null) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
if (storeRecord != null) {
|
|
38
|
+
return getMutableStoreRecordProxy(startNode, link);
|
|
39
|
+
}
|
|
40
|
+
startNode = startNode.parentStoreLayer;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function getMutableStoreRecordProxy(
|
|
47
|
+
childMostStoreLayer: StoreLayer,
|
|
48
|
+
link: StoreLink,
|
|
49
|
+
): StoreRecord {
|
|
50
|
+
return new Proxy<StoreRecord>(
|
|
51
|
+
{},
|
|
52
|
+
{
|
|
53
|
+
get(_, propertyName) {
|
|
54
|
+
let currentStoreLayer: StoreLayer | null = childMostStoreLayer;
|
|
55
|
+
while (currentStoreLayer !== null) {
|
|
56
|
+
const storeRecord =
|
|
57
|
+
currentStoreLayer.data[link.__typename]?.[link.__link];
|
|
58
|
+
if (storeRecord === null) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
if (storeRecord != null) {
|
|
62
|
+
const value = Reflect.get(storeRecord, propertyName);
|
|
63
|
+
if (value !== undefined) {
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
currentStoreLayer = currentStoreLayer.parentStoreLayer;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
has(_, propertyName) {
|
|
71
|
+
let currentStoreLayer: StoreLayer | null = childMostStoreLayer;
|
|
72
|
+
while (currentStoreLayer !== null) {
|
|
73
|
+
const storeRecord =
|
|
74
|
+
currentStoreLayer.data[link.__typename]?.[link.__link];
|
|
75
|
+
if (storeRecord === null) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
if (storeRecord != null) {
|
|
79
|
+
const value = Reflect.has(storeRecord, propertyName);
|
|
80
|
+
if (value !== undefined) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
currentStoreLayer = currentStoreLayer.parentStoreLayer;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
},
|
|
88
|
+
set(_, p, newValue) {
|
|
89
|
+
return Reflect.set(
|
|
90
|
+
getOrInsertRecord(childMostStoreLayer.data, link),
|
|
91
|
+
p,
|
|
92
|
+
newValue,
|
|
93
|
+
);
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export type BaseStoreLayer = {
|
|
100
|
+
readonly kind: 'BaseStoreLayer';
|
|
101
|
+
childStoreLayer: OptimisticStoreLayer | null;
|
|
102
|
+
readonly parentStoreLayer: null;
|
|
103
|
+
readonly data: BaseStoreLayerData;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export type NetworkResponseStoreLayer = {
|
|
107
|
+
readonly kind: 'NetworkResponseStoreLayer';
|
|
108
|
+
childStoreLayer: OptimisticStoreLayer | StartUpdateStoreLayer | null;
|
|
109
|
+
parentStoreLayer: OptimisticStoreLayer | StartUpdateStoreLayer;
|
|
110
|
+
readonly data: StoreLayerData;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export type DataUpdate<TStoreLayer extends StoreLayer> = (
|
|
114
|
+
storeLayer: TStoreLayer,
|
|
115
|
+
) => void;
|
|
116
|
+
|
|
117
|
+
export type StartUpdateStoreLayer = {
|
|
118
|
+
readonly kind: 'StartUpdateStoreLayer';
|
|
119
|
+
childStoreLayer: OptimisticStoreLayer | NetworkResponseStoreLayer | null;
|
|
120
|
+
parentStoreLayer: OptimisticStoreLayer | NetworkResponseStoreLayer;
|
|
121
|
+
data: StoreLayerData;
|
|
122
|
+
startUpdate: DataUpdate<StartUpdateStoreLayer | BaseStoreLayer>;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export type OptimisticStoreLayer = {
|
|
126
|
+
readonly kind: 'OptimisticStoreLayer';
|
|
127
|
+
childStoreLayer:
|
|
128
|
+
| OptimisticStoreLayer
|
|
129
|
+
| StartUpdateStoreLayer
|
|
130
|
+
| NetworkResponseStoreLayer
|
|
131
|
+
| null;
|
|
132
|
+
parentStoreLayer:
|
|
133
|
+
| OptimisticStoreLayer
|
|
134
|
+
| StartUpdateStoreLayer
|
|
135
|
+
| NetworkResponseStoreLayer
|
|
136
|
+
| BaseStoreLayer;
|
|
137
|
+
data: StoreLayerData;
|
|
138
|
+
readonly startUpdate: DataUpdate<OptimisticStoreLayer>;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export function addNetworkResponseStoreLayer(
|
|
142
|
+
parent: StoreLayer,
|
|
143
|
+
): StoreLayerWithData {
|
|
144
|
+
switch (parent.kind) {
|
|
145
|
+
case 'NetworkResponseStoreLayer':
|
|
146
|
+
case 'BaseStoreLayer': {
|
|
147
|
+
return parent;
|
|
148
|
+
}
|
|
149
|
+
case 'StartUpdateStoreLayer':
|
|
150
|
+
case 'OptimisticStoreLayer': {
|
|
151
|
+
const node: NetworkResponseStoreLayer = {
|
|
152
|
+
kind: 'NetworkResponseStoreLayer',
|
|
153
|
+
parentStoreLayer: parent,
|
|
154
|
+
childStoreLayer: null,
|
|
155
|
+
data: {},
|
|
156
|
+
};
|
|
157
|
+
parent.childStoreLayer = node;
|
|
158
|
+
|
|
159
|
+
return node;
|
|
160
|
+
}
|
|
161
|
+
default: {
|
|
162
|
+
parent satisfies never;
|
|
163
|
+
throw new Error('Unreachable. This is a bug in Isograph.');
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function mergeDataLayer(target: StoreLayerData, source: StoreLayerData): void {
|
|
169
|
+
for (const [typeName, sourceById] of Object.entries(source)) {
|
|
170
|
+
if (sourceById == null) {
|
|
171
|
+
target[typeName] = sourceById;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const targetRecordById = (target[typeName] ??= {});
|
|
175
|
+
for (const [id, sourceRecord] of Object.entries(sourceById)) {
|
|
176
|
+
if (sourceRecord === null) {
|
|
177
|
+
targetRecordById[id] = null;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const targetRecord = (targetRecordById[id] ??= {});
|
|
181
|
+
Object.assign(targetRecord, sourceRecord);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function addStartUpdateStoreLayer(
|
|
187
|
+
parent: StoreLayer,
|
|
188
|
+
startUpdate: StartUpdateStoreLayer['startUpdate'],
|
|
189
|
+
): StoreLayer {
|
|
190
|
+
switch (parent.kind) {
|
|
191
|
+
case 'BaseStoreLayer': {
|
|
192
|
+
startUpdate(parent);
|
|
193
|
+
return parent;
|
|
194
|
+
}
|
|
195
|
+
case 'StartUpdateStoreLayer': {
|
|
196
|
+
const node = parent;
|
|
197
|
+
|
|
198
|
+
const prevStartUpdate = node.startUpdate;
|
|
199
|
+
node.startUpdate = () => {
|
|
200
|
+
prevStartUpdate(node);
|
|
201
|
+
startUpdate(node);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
startUpdate(node);
|
|
205
|
+
return node;
|
|
206
|
+
}
|
|
207
|
+
case 'NetworkResponseStoreLayer':
|
|
208
|
+
case 'OptimisticStoreLayer': {
|
|
209
|
+
const node: StartUpdateStoreLayer = {
|
|
210
|
+
kind: 'StartUpdateStoreLayer',
|
|
211
|
+
parentStoreLayer: parent,
|
|
212
|
+
childStoreLayer: null,
|
|
213
|
+
data: {},
|
|
214
|
+
startUpdate: startUpdate,
|
|
215
|
+
};
|
|
216
|
+
parent.childStoreLayer = node;
|
|
217
|
+
|
|
218
|
+
startUpdate(node);
|
|
219
|
+
return node;
|
|
220
|
+
}
|
|
221
|
+
default: {
|
|
222
|
+
parent satisfies never;
|
|
223
|
+
throw new Error('Unreachable. This is a bug in Isograph.');
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function addOptimisticStoreLayer(
|
|
229
|
+
parent: StoreLayer,
|
|
230
|
+
startUpdate: OptimisticStoreLayer['startUpdate'],
|
|
231
|
+
): OptimisticStoreLayer {
|
|
232
|
+
switch (parent.kind) {
|
|
233
|
+
case 'BaseStoreLayer':
|
|
234
|
+
case 'StartUpdateStoreLayer':
|
|
235
|
+
case 'NetworkResponseStoreLayer':
|
|
236
|
+
case 'OptimisticStoreLayer': {
|
|
237
|
+
const node: OptimisticStoreLayer = {
|
|
238
|
+
kind: 'OptimisticStoreLayer',
|
|
239
|
+
parentStoreLayer: parent,
|
|
240
|
+
childStoreLayer: null,
|
|
241
|
+
data: {},
|
|
242
|
+
startUpdate: startUpdate,
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
startUpdate(node);
|
|
246
|
+
parent.childStoreLayer = node;
|
|
247
|
+
|
|
248
|
+
return node;
|
|
249
|
+
}
|
|
250
|
+
default: {
|
|
251
|
+
parent satisfies never;
|
|
252
|
+
throw new Error('Unreachable. This is a bug in Isograph.');
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Merge storeLayerToMerge, and its children, into baseStoreLayer.
|
|
259
|
+
* We can merge until we reach a revertible layer (i.e. an optimistic layer).
|
|
260
|
+
* All other layers cannot be reverted, so for housekeeping + perf, we merge
|
|
261
|
+
* them into a single layer.
|
|
262
|
+
*
|
|
263
|
+
* Note that BaseStoreLayer.childStoreLayer has type OptimisticStoreLayer | null.
|
|
264
|
+
* So, the state of the stack is never e.g. base <- network response. Instead,
|
|
265
|
+
* we have a base + a child that we would like to attach to the base. So, we merge
|
|
266
|
+
* (flatten) until we reach an optimistic layer or null, at which point, we can
|
|
267
|
+
* set baseStoreLayer.childStoreLayer = storeLayerToMerge (via setChildOfNode).
|
|
268
|
+
*/
|
|
269
|
+
function mergeLayersWithDataIntoBaseLayer(
|
|
270
|
+
environment: IsographEnvironment,
|
|
271
|
+
storeLayerToMerge: StoreLayer | null,
|
|
272
|
+
baseStoreLayer: BaseStoreLayer,
|
|
273
|
+
) {
|
|
274
|
+
while (
|
|
275
|
+
storeLayerToMerge &&
|
|
276
|
+
storeLayerToMerge.kind !== 'OptimisticStoreLayer'
|
|
277
|
+
) {
|
|
278
|
+
mergeDataLayer(baseStoreLayer.data, storeLayerToMerge.data);
|
|
279
|
+
storeLayerToMerge = storeLayerToMerge.childStoreLayer;
|
|
280
|
+
}
|
|
281
|
+
setChildOfNode(environment, baseStoreLayer, storeLayerToMerge);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Now that we have replaced the optimistic layer with a network response layer, we need
|
|
286
|
+
* to
|
|
287
|
+
* - re-execute startUpdate and optimistic nodes, in light of the replaced data, and
|
|
288
|
+
* - create two objects containing the old merged data (from the optimistic update layer
|
|
289
|
+
* onward) and the new merged data (from the network response layer onward).
|
|
290
|
+
* - we will compare the new and old merged data in order to determine the changed records
|
|
291
|
+
* and trigger subscriptions.
|
|
292
|
+
*
|
|
293
|
+
* Here, "merged data" means all of the records + fields that were modified, starting at
|
|
294
|
+
* storeLayer, e.g. in BaseLayer <- OptimisticLayer <- StartUpdateLayer, if we
|
|
295
|
+
* are replacing Optimistic, then oldData will contain the records + fields modified by
|
|
296
|
+
* OptimisticLayer + StartUpdateLayer.
|
|
297
|
+
*/
|
|
298
|
+
function reexecuteUpdatesAndMergeData(
|
|
299
|
+
storeLayer:
|
|
300
|
+
| OptimisticStoreLayer
|
|
301
|
+
| NetworkResponseStoreLayer
|
|
302
|
+
| StartUpdateStoreLayer
|
|
303
|
+
| null,
|
|
304
|
+
// reflects the (now reverted) optimistic layer
|
|
305
|
+
oldMergedData: StoreLayerData,
|
|
306
|
+
// reflects whatever replaced the optimistic layer
|
|
307
|
+
newMergedData: StoreLayerData,
|
|
308
|
+
): void {
|
|
309
|
+
while (storeLayer !== null) {
|
|
310
|
+
mergeDataLayer(oldMergedData, storeLayer.data);
|
|
311
|
+
switch (storeLayer.kind) {
|
|
312
|
+
case 'NetworkResponseStoreLayer':
|
|
313
|
+
break;
|
|
314
|
+
case 'StartUpdateStoreLayer': {
|
|
315
|
+
storeLayer.data = {};
|
|
316
|
+
storeLayer.startUpdate(storeLayer);
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
case 'OptimisticStoreLayer': {
|
|
320
|
+
storeLayer.data = {};
|
|
321
|
+
storeLayer.startUpdate(storeLayer);
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
default: {
|
|
325
|
+
storeLayer satisfies never;
|
|
326
|
+
throw new Error('Unreachable. This is a bug in Isograph.');
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
mergeDataLayer(newMergedData, storeLayer.data);
|
|
330
|
+
|
|
331
|
+
storeLayer = storeLayer.childStoreLayer;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Set storeLayerToModify's child to a given layer. This may be null!
|
|
337
|
+
* If it is null, set the environment.store to storeLayerToModify.
|
|
338
|
+
* If it is not null, then the existing environment.store value remains
|
|
339
|
+
* valid.
|
|
340
|
+
*/
|
|
341
|
+
function setChildOfNode<TStoreLayer extends StoreLayer>(
|
|
342
|
+
environment: IsographEnvironment,
|
|
343
|
+
storeLayerToModify: TStoreLayer,
|
|
344
|
+
newChildStoreLayer: TStoreLayer['childStoreLayer'],
|
|
345
|
+
) {
|
|
346
|
+
storeLayerToModify.childStoreLayer = newChildStoreLayer;
|
|
347
|
+
if (newChildStoreLayer !== null) {
|
|
348
|
+
newChildStoreLayer.parentStoreLayer = storeLayerToModify;
|
|
349
|
+
} else {
|
|
350
|
+
environment.store = storeLayerToModify;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Remove an optimistic store layer from the stack, potentially replacing it
|
|
356
|
+
* with a network response.
|
|
357
|
+
*
|
|
358
|
+
* After we do this, we must re-execute all child startUpdate and optimistic
|
|
359
|
+
* layers (since their data may have changed.) We also keep track of changed
|
|
360
|
+
* records, in order to call affected subscriptions.
|
|
361
|
+
*/
|
|
362
|
+
export function revertOptimisticStoreLayerAndMaybeReplace(
|
|
363
|
+
environment: IsographEnvironment,
|
|
364
|
+
optimisticNode: OptimisticStoreLayer,
|
|
365
|
+
normalizeData: null | ((storeLayer: StoreLayerWithData) => void),
|
|
366
|
+
): void {
|
|
367
|
+
// We cannot just replace the optimistic node with the network response node,
|
|
368
|
+
// because (e.g.) the types allow Base <- Opt, but not Base <- NetworkResponse.
|
|
369
|
+
// We also may be removing the optimistic layer without replacing it with
|
|
370
|
+
// anything, which would also be disallowed if the original stack was
|
|
371
|
+
// Base <- Opt <- NetworkResponse.
|
|
372
|
+
//
|
|
373
|
+
// Thus, instead, we will (1) replace the optimistic node's data with an empty object
|
|
374
|
+
// and attach the network response as a child.
|
|
375
|
+
const oldMergedData = optimisticNode.data;
|
|
376
|
+
optimisticNode.data = {};
|
|
377
|
+
|
|
378
|
+
let newMergedData = {};
|
|
379
|
+
let childNode = optimisticNode.childStoreLayer;
|
|
380
|
+
if (normalizeData !== null) {
|
|
381
|
+
const networkResponseStoreLayer: NetworkResponseStoreLayer = {
|
|
382
|
+
kind: 'NetworkResponseStoreLayer',
|
|
383
|
+
data: {},
|
|
384
|
+
parentStoreLayer: optimisticNode,
|
|
385
|
+
childStoreLayer: null,
|
|
386
|
+
};
|
|
387
|
+
normalizeData(networkResponseStoreLayer);
|
|
388
|
+
|
|
389
|
+
if (childNode?.kind === 'NetworkResponseStoreLayer') {
|
|
390
|
+
// (2) if the optimistic layer's child was a network response, and we are
|
|
391
|
+
// replacing it with a network response, we must merge the replacement
|
|
392
|
+
// and the child.
|
|
393
|
+
mergeDataLayer(networkResponseStoreLayer.data, childNode.data);
|
|
394
|
+
mergeDataLayer(oldMergedData, childNode.data);
|
|
395
|
+
childNode = childNode.childStoreLayer;
|
|
396
|
+
}
|
|
397
|
+
newMergedData = structuredClone(networkResponseStoreLayer.data);
|
|
398
|
+
setChildOfNode(environment, networkResponseStoreLayer, childNode);
|
|
399
|
+
optimisticNode.childStoreLayer = networkResponseStoreLayer;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// (3) Re-execute all updates, accumulating all changed values into newMergedData.
|
|
403
|
+
// Since we have already written the network response into newMergedData, we
|
|
404
|
+
// can proceed from the child of the (potentially merged) network response layer.
|
|
405
|
+
//
|
|
406
|
+
// Note that it is important that reexecuteUpdatesAndMergeData is called here!
|
|
407
|
+
// That is because we created newMergedData from the network response layer's data,
|
|
408
|
+
// and later, we may merge that network response into the parent layer (if it is
|
|
409
|
+
// a base layer). That merged layer will contain many extraneous records (unless the
|
|
410
|
+
// base layer is empty).
|
|
411
|
+
//
|
|
412
|
+
// This would cause us to re-execute subscriptions unnecessarily, as these records
|
|
413
|
+
// do not represent changes between the optimistic and network response layers.
|
|
414
|
+
reexecuteUpdatesAndMergeData(childNode, oldMergedData, newMergedData);
|
|
415
|
+
|
|
416
|
+
// (4) Now, we can finally remove the optimistic layer, i.e. do
|
|
417
|
+
// optimistic.parent.child = optimistic.child.
|
|
418
|
+
// But the types don't line up, so we handle the cases differently, based on the
|
|
419
|
+
// parent layer type.
|
|
420
|
+
if (optimisticNode.parentStoreLayer.kind === 'BaseStoreLayer') {
|
|
421
|
+
// (4a) If the optimistic parent is the base layer, then we have a problem: base.child
|
|
422
|
+
// must be an optimistic layer or null. So, we merge the optimistic children into the
|
|
423
|
+
// base layer until we reach an optimistic layer.
|
|
424
|
+
mergeLayersWithDataIntoBaseLayer(
|
|
425
|
+
environment,
|
|
426
|
+
optimisticNode.childStoreLayer,
|
|
427
|
+
optimisticNode.parentStoreLayer,
|
|
428
|
+
);
|
|
429
|
+
} else if (
|
|
430
|
+
optimisticNode.parentStoreLayer.kind === 'NetworkResponseStoreLayer' &&
|
|
431
|
+
optimisticNode.childStoreLayer?.kind === 'NetworkResponseStoreLayer'
|
|
432
|
+
) {
|
|
433
|
+
// (4b) if the parent is a network response layer, simply merge those. (We do not
|
|
434
|
+
// attempt to merge other layers, e.g. startUpdate layers, because there is some
|
|
435
|
+
// optimistic layer between this layer and the base, and the startUpdate will need
|
|
436
|
+
// to be recalculated if the optimistic layer is reverted.)
|
|
437
|
+
mergeDataLayer(
|
|
438
|
+
optimisticNode.parentStoreLayer.data,
|
|
439
|
+
optimisticNode.childStoreLayer.data,
|
|
440
|
+
);
|
|
441
|
+
|
|
442
|
+
setChildOfNode(
|
|
443
|
+
environment,
|
|
444
|
+
optimisticNode.parentStoreLayer,
|
|
445
|
+
optimisticNode.childStoreLayer.childStoreLayer,
|
|
446
|
+
);
|
|
447
|
+
} else {
|
|
448
|
+
// (4c) Otherwise, the parent is an optimistic or start update layer, and we can
|
|
449
|
+
// set optimistic.parent.child = optimistic.child.
|
|
450
|
+
setChildOfNode(
|
|
451
|
+
environment,
|
|
452
|
+
optimisticNode.parentStoreLayer,
|
|
453
|
+
optimisticNode.childStoreLayer,
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// (5) finally, compare the oldMergedData and newMergedData objects, in order to extract
|
|
458
|
+
// the modified IDs, and re-execute subscriptions.
|
|
459
|
+
let encounteredIds: EncounteredIds = new Map();
|
|
460
|
+
compareData(oldMergedData, newMergedData, encounteredIds);
|
|
461
|
+
callSubscriptions(environment, encounteredIds);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export type StoreLayer =
|
|
465
|
+
| OptimisticStoreLayer
|
|
466
|
+
| NetworkResponseStoreLayer
|
|
467
|
+
| StartUpdateStoreLayer
|
|
468
|
+
| BaseStoreLayer;
|
|
469
|
+
|
|
470
|
+
export type StoreLayerWithData = BaseStoreLayer | NetworkResponseStoreLayer;
|
|
471
|
+
|
|
472
|
+
function compareData(
|
|
473
|
+
oldData: StoreLayerData,
|
|
474
|
+
newData: StoreLayerData,
|
|
475
|
+
encounteredIds: EncounteredIds,
|
|
476
|
+
): void {
|
|
477
|
+
const oldDataTypeNames = new Set(Object.keys(oldData));
|
|
478
|
+
const newDataTypeNames = new Set(Object.keys(newData));
|
|
479
|
+
|
|
480
|
+
for (const oldTypeName of oldDataTypeNames.difference(newDataTypeNames)) {
|
|
481
|
+
const set = insertEmptySetIfMissing(encounteredIds, oldTypeName);
|
|
482
|
+
for (const id in oldData[oldTypeName]) {
|
|
483
|
+
set.add(id);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
for (const [typeName, newRecords] of Object.entries(newData)) {
|
|
488
|
+
if (newRecords == null) {
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
const oldRecords = oldData[typeName];
|
|
492
|
+
outer: for (const [id, newRecord] of Object.entries(newRecords)) {
|
|
493
|
+
if (newRecord == null) {
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
const oldRecord = oldRecords?.[id];
|
|
497
|
+
|
|
498
|
+
for (const [recordKey, newRecordValue] of Object.entries(newRecord)) {
|
|
499
|
+
// TODO: compare links, compare arrays
|
|
500
|
+
if (newRecordValue !== oldRecord?.[recordKey]) {
|
|
501
|
+
const set = insertEmptySetIfMissing(encounteredIds, typeName);
|
|
502
|
+
set.add(id);
|
|
503
|
+
continue outer;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
encounteredIds.get(typeName)?.delete(id);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
package/src/core/read.ts
CHANGED
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
} from './IsographEnvironment';
|
|
29
29
|
import { logMessage } from './logging';
|
|
30
30
|
import { maybeMakeNetworkRequest } from './makeNetworkRequest';
|
|
31
|
+
import { getStoreRecordProxy } from './optimisticProxy';
|
|
31
32
|
import {
|
|
32
33
|
getPromiseState,
|
|
33
34
|
NOT_SET,
|
|
@@ -153,7 +154,7 @@ function readData<TReadFromStore>(
|
|
|
153
154
|
root.__typename,
|
|
154
155
|
);
|
|
155
156
|
encounteredIds.add(root.__link);
|
|
156
|
-
let storeRecord = environment.store
|
|
157
|
+
let storeRecord = getStoreRecordProxy(environment.store, root);
|
|
157
158
|
if (storeRecord === undefined) {
|
|
158
159
|
return {
|
|
159
160
|
kind: 'MissingData',
|
package/src/core/startUpdate.ts
CHANGED
|
@@ -19,6 +19,13 @@ import {
|
|
|
19
19
|
type StoreLink,
|
|
20
20
|
} from './IsographEnvironment';
|
|
21
21
|
import { logMessage } from './logging';
|
|
22
|
+
import {
|
|
23
|
+
addStartUpdateStoreLayer,
|
|
24
|
+
getMutableStoreRecordProxy,
|
|
25
|
+
getOrInsertRecord,
|
|
26
|
+
type StartUpdateStoreLayer,
|
|
27
|
+
type StoreLayer,
|
|
28
|
+
} from './optimisticProxy';
|
|
22
29
|
import { readPromise, type PromiseWrapper } from './PromiseWrapper';
|
|
23
30
|
import {
|
|
24
31
|
readImperativelyLoadedField,
|
|
@@ -56,28 +63,38 @@ export function createStartUpdate<TReadFromStore extends UnknownTReadFromStore>(
|
|
|
56
63
|
return (updater) => {
|
|
57
64
|
let mutableUpdatedIds: EncounteredIds = new Map();
|
|
58
65
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
66
|
+
const startUpdate: StartUpdateStoreLayer['startUpdate'] = (storeLayer) => {
|
|
67
|
+
mutableUpdatedIds.clear();
|
|
68
|
+
let updatableData = createUpdatableProxy(
|
|
69
|
+
environment,
|
|
70
|
+
storeLayer,
|
|
71
|
+
fragmentReference,
|
|
72
|
+
networkRequestOptions,
|
|
73
|
+
mutableUpdatedIds,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
updater({ updatableData });
|
|
78
|
+
} catch (e) {
|
|
79
|
+
logMessage(environment, () => ({
|
|
80
|
+
kind: 'StartUpdateError',
|
|
81
|
+
error: e,
|
|
82
|
+
}));
|
|
83
|
+
throw e;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
environment.store = addStartUpdateStoreLayer(
|
|
88
|
+
environment.store,
|
|
89
|
+
startUpdate,
|
|
64
90
|
);
|
|
65
91
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}));
|
|
73
|
-
throw e;
|
|
74
|
-
} finally {
|
|
75
|
-
logMessage(environment, () => ({
|
|
76
|
-
kind: 'StartUpdateComplete',
|
|
77
|
-
updatedIds: mutableUpdatedIds,
|
|
78
|
-
}));
|
|
79
|
-
callSubscriptions(environment, mutableUpdatedIds);
|
|
80
|
-
}
|
|
92
|
+
logMessage(environment, () => ({
|
|
93
|
+
kind: 'StartUpdateComplete',
|
|
94
|
+
updatedIds: mutableUpdatedIds,
|
|
95
|
+
}));
|
|
96
|
+
|
|
97
|
+
callSubscriptions(environment, mutableUpdatedIds);
|
|
81
98
|
};
|
|
82
99
|
}
|
|
83
100
|
|
|
@@ -85,6 +102,7 @@ export function createUpdatableProxy<
|
|
|
85
102
|
TReadFromStore extends UnknownTReadFromStore,
|
|
86
103
|
>(
|
|
87
104
|
environment: IsographEnvironment,
|
|
105
|
+
storeLayer: StoreLayer,
|
|
88
106
|
fragmentReference: FragmentReference<TReadFromStore, unknown>,
|
|
89
107
|
networkRequestOptions: NetworkRequestReaderOptions,
|
|
90
108
|
mutableUpdatedIds: EncounteredIds,
|
|
@@ -95,6 +113,7 @@ export function createUpdatableProxy<
|
|
|
95
113
|
|
|
96
114
|
return readUpdatableData(
|
|
97
115
|
environment,
|
|
116
|
+
storeLayer,
|
|
98
117
|
readerWithRefetchQueries.readerArtifact.readerAst,
|
|
99
118
|
fragmentReference.root,
|
|
100
119
|
fragmentReference.variables ?? {},
|
|
@@ -154,6 +173,7 @@ function defineCachedProperty<T>(
|
|
|
154
173
|
|
|
155
174
|
function readUpdatableData<TReadFromStore extends UnknownTReadFromStore>(
|
|
156
175
|
environment: IsographEnvironment,
|
|
176
|
+
storeLayer: StoreLayer,
|
|
157
177
|
ast: ReaderAst<TReadFromStore>,
|
|
158
178
|
root: StoreLink,
|
|
159
179
|
variables: ExtractParameters<TReadFromStore>,
|
|
@@ -163,14 +183,7 @@ function readUpdatableData<TReadFromStore extends UnknownTReadFromStore>(
|
|
|
163
183
|
mutableState: MutableInvalidationState,
|
|
164
184
|
mutableUpdatedIds: EncounteredIds,
|
|
165
185
|
): ReadDataResultSuccess<ExtractUpdatableData<TReadFromStore>> {
|
|
166
|
-
|
|
167
|
-
if (storeRecord == null) {
|
|
168
|
-
return {
|
|
169
|
-
kind: 'Success',
|
|
170
|
-
data: null as any,
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
186
|
+
const storeRecord = getMutableStoreRecordProxy(storeLayer, root);
|
|
174
187
|
let target: { [index: string]: any } = {};
|
|
175
188
|
|
|
176
189
|
for (const field of ast) {
|
|
@@ -196,6 +209,7 @@ function readUpdatableData<TReadFromStore extends UnknownTReadFromStore>(
|
|
|
196
209
|
},
|
|
197
210
|
field.isUpdatable
|
|
198
211
|
? (newValue) => {
|
|
212
|
+
const storeRecord = getOrInsertRecord(storeLayer.data, root);
|
|
199
213
|
storeRecord[storeRecordName] = newValue;
|
|
200
214
|
const updatedIds = insertEmptySetIfMissing(
|
|
201
215
|
mutableUpdatedIds,
|
|
@@ -226,6 +240,7 @@ function readUpdatableData<TReadFromStore extends UnknownTReadFromStore>(
|
|
|
226
240
|
(ast, root) =>
|
|
227
241
|
readUpdatableData(
|
|
228
242
|
environment,
|
|
243
|
+
storeLayer,
|
|
229
244
|
ast,
|
|
230
245
|
root,
|
|
231
246
|
variables,
|
|
@@ -243,6 +258,7 @@ function readUpdatableData<TReadFromStore extends UnknownTReadFromStore>(
|
|
|
243
258
|
},
|
|
244
259
|
'isUpdatable' in field && field.isUpdatable
|
|
245
260
|
? (newValue) => {
|
|
261
|
+
const storeRecord = getOrInsertRecord(storeLayer.data, root);
|
|
246
262
|
if (Array.isArray(newValue)) {
|
|
247
263
|
storeRecord[storeRecordName] = newValue.map((node) =>
|
|
248
264
|
assertLink(node?.__link),
|
package/src/index.ts
CHANGED
|
@@ -7,14 +7,14 @@ import {
|
|
|
7
7
|
import {
|
|
8
8
|
createIsographEnvironment,
|
|
9
9
|
ROOT_ID,
|
|
10
|
-
type
|
|
10
|
+
type BaseStoreLayerData,
|
|
11
11
|
} from '../core/IsographEnvironment';
|
|
12
12
|
import { wrapResolvedValue } from '../core/PromiseWrapper';
|
|
13
13
|
import { iso } from './__isograph/iso';
|
|
14
14
|
import { meNameSuccessorRetainedQuery } from './meNameSuccessor';
|
|
15
15
|
import { nodeFieldRetainedQuery } from './nodeQuery';
|
|
16
16
|
|
|
17
|
-
const getDefaultStore = ():
|
|
17
|
+
const getDefaultStore = (): BaseStoreLayerData => ({
|
|
18
18
|
Query: {
|
|
19
19
|
[ROOT_ID]: {
|
|
20
20
|
me: { __link: '0', __typename: 'Economist' },
|