relay-runtime 0.0.0-main-605e3666 → 0.0.0-main-0f21ad98
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/index.js +1 -1
- package/lib/store/experimental-external-state-resolvers/ExternalStateResolverCache.js +320 -0
- package/lib/store/experimental-external-state-resolvers/ExternalStateResolverStore.js +787 -0
- package/package.json +1 -1
- package/relay-runtime.js +1 -1
- package/relay-runtime.min.js +1 -1
- package/store/experimental-external-state-resolvers/ExternalStateResolverCache.js.flow +409 -0
- package/store/experimental-external-state-resolvers/ExternalStateResolverStore.js.flow +822 -0
package/index.js
CHANGED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
// flowlint ambiguous-object-type:error
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
14
|
+
|
|
15
|
+
var _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));
|
|
16
|
+
|
|
17
|
+
var recycleNodesInto = require('../../util/recycleNodesInto');
|
|
18
|
+
|
|
19
|
+
var _require = require('../ClientID'),
|
|
20
|
+
generateClientID = _require.generateClientID;
|
|
21
|
+
|
|
22
|
+
var RelayModernRecord = require('../RelayModernRecord');
|
|
23
|
+
|
|
24
|
+
var RelayRecordSource = require('../RelayRecordSource');
|
|
25
|
+
|
|
26
|
+
var _require2 = require('../RelayStoreUtils'),
|
|
27
|
+
RELAY_RESOLVER_ERROR_KEY = _require2.RELAY_RESOLVER_ERROR_KEY,
|
|
28
|
+
RELAY_RESOLVER_INPUTS_KEY = _require2.RELAY_RESOLVER_INPUTS_KEY,
|
|
29
|
+
RELAY_RESOLVER_INVALIDATION_KEY = _require2.RELAY_RESOLVER_INVALIDATION_KEY,
|
|
30
|
+
RELAY_RESOLVER_MISSING_REQUIRED_FIELDS_KEY = _require2.RELAY_RESOLVER_MISSING_REQUIRED_FIELDS_KEY,
|
|
31
|
+
RELAY_RESOLVER_READER_SELECTOR_KEY = _require2.RELAY_RESOLVER_READER_SELECTOR_KEY,
|
|
32
|
+
RELAY_RESOLVER_VALUE_KEY = _require2.RELAY_RESOLVER_VALUE_KEY,
|
|
33
|
+
getStorageKey = _require2.getStorageKey;
|
|
34
|
+
|
|
35
|
+
var ExternalStateResolverStore = require('./ExternalStateResolverStore');
|
|
36
|
+
|
|
37
|
+
var warning = require("fbjs/lib/warning"); // When this experiment gets promoted to stable, these keys will move into
|
|
38
|
+
// `RelayStoreUtils`.
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
var RELAY_RESOLVER_EXTERNAL_STATE_SUBSCRIPTION_KEY = '__resolverExternalStateSubscription';
|
|
42
|
+
var RELAY_EXTERNAL_STATE_VALUE = '__resolverExternalStateValue';
|
|
43
|
+
var RELAY_EXTERNAL_STATE_DIRTY = '__resolverExternalStateDirty';
|
|
44
|
+
/**
|
|
45
|
+
* An experimental fork of store/ResolverCache.js intended to let us experiment
|
|
46
|
+
* with External State Resolvers.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
// $FlowFixMe[unclear-type] - will always be empty
|
|
50
|
+
var emptySet = new Set();
|
|
51
|
+
|
|
52
|
+
function addDependencyEdge(edges, from, to) {
|
|
53
|
+
var set = edges.get(from);
|
|
54
|
+
|
|
55
|
+
if (!set) {
|
|
56
|
+
set = new Set();
|
|
57
|
+
edges.set(from, set);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
set.add(to);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
var ExternalStateResolverCache = /*#__PURE__*/function () {
|
|
64
|
+
function ExternalStateResolverCache(getRecordSource, store) {
|
|
65
|
+
this._resolverIDToRecordIDs = new Map();
|
|
66
|
+
this._recordIDToResolverIDs = new Map();
|
|
67
|
+
this._getRecordSource = getRecordSource;
|
|
68
|
+
this._store = store;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
var _proto = ExternalStateResolverCache.prototype;
|
|
72
|
+
|
|
73
|
+
_proto.readFromCacheOrEvaluate = function readFromCacheOrEvaluate(record, field, variables, evaluate, getDataForResolverFragment) {
|
|
74
|
+
var recordSource = this._getRecordSource();
|
|
75
|
+
|
|
76
|
+
var recordID = RelayModernRecord.getDataID(record);
|
|
77
|
+
var storageKey = getStorageKey(field, variables);
|
|
78
|
+
var linkedID = RelayModernRecord.getLinkedRecordID(record, storageKey);
|
|
79
|
+
var linkedRecord = linkedID == null ? null : recordSource.get(linkedID);
|
|
80
|
+
|
|
81
|
+
if (linkedRecord == null || this._isInvalid(linkedRecord, getDataForResolverFragment)) {
|
|
82
|
+
var _linkedID;
|
|
83
|
+
|
|
84
|
+
// Cache miss; evaluate the selector and store the result in a new record:
|
|
85
|
+
linkedID = (_linkedID = linkedID) !== null && _linkedID !== void 0 ? _linkedID : generateClientID(recordID, storageKey);
|
|
86
|
+
linkedRecord = RelayModernRecord.create(linkedID, '__RELAY_RESOLVER__');
|
|
87
|
+
var evaluationResult = evaluate(); // In the future we we know from the Reader AST node if we are trying to
|
|
88
|
+
// read a Relay Resolver field or not. For the purpose of this hack, we
|
|
89
|
+
// will just check if it quacks like a duck.
|
|
90
|
+
|
|
91
|
+
var externalState = isExternalStateValue(evaluationResult.resolverResult);
|
|
92
|
+
|
|
93
|
+
if (externalState != null) {
|
|
94
|
+
this._setExternalStateValue(linkedRecord, linkedID, externalState);
|
|
95
|
+
} else {
|
|
96
|
+
RelayModernRecord.setValue(linkedRecord, RELAY_RESOLVER_VALUE_KEY, evaluationResult.resolverResult);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
RelayModernRecord.setValue(linkedRecord, RELAY_RESOLVER_INPUTS_KEY, evaluationResult.fragmentValue);
|
|
100
|
+
RelayModernRecord.setValue(linkedRecord, RELAY_RESOLVER_READER_SELECTOR_KEY, evaluationResult.readerSelector);
|
|
101
|
+
RelayModernRecord.setValue(linkedRecord, RELAY_RESOLVER_MISSING_REQUIRED_FIELDS_KEY, evaluationResult.missingRequiredFields);
|
|
102
|
+
RelayModernRecord.setValue(linkedRecord, RELAY_RESOLVER_ERROR_KEY, evaluationResult.errors);
|
|
103
|
+
recordSource.set(linkedID, linkedRecord); // Link the resolver value record to the resolver field of the record being read:
|
|
104
|
+
|
|
105
|
+
var nextRecord = RelayModernRecord.clone(record);
|
|
106
|
+
RelayModernRecord.setLinkedRecordID(nextRecord, storageKey, linkedID);
|
|
107
|
+
recordSource.set(RelayModernRecord.getDataID(nextRecord), nextRecord); // Put records observed by the resolver into the dependency graph:
|
|
108
|
+
|
|
109
|
+
var resolverID = evaluationResult.resolverID;
|
|
110
|
+
addDependencyEdge(this._resolverIDToRecordIDs, resolverID, linkedID);
|
|
111
|
+
addDependencyEdge(this._recordIDToResolverIDs, recordID, resolverID);
|
|
112
|
+
|
|
113
|
+
var _iterator = (0, _createForOfIteratorHelper2["default"])(evaluationResult.seenRecordIDs),
|
|
114
|
+
_step;
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
118
|
+
var seenRecordID = _step.value;
|
|
119
|
+
addDependencyEdge(this._recordIDToResolverIDs, seenRecordID, resolverID);
|
|
120
|
+
}
|
|
121
|
+
} catch (err) {
|
|
122
|
+
_iterator.e(err);
|
|
123
|
+
} finally {
|
|
124
|
+
_iterator.f();
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
// If this is an External State Resolver, we might have a cache hit (the
|
|
128
|
+
// fragment data hasn't changed since we last evaluated the resolver),
|
|
129
|
+
// but it might still be "dirty" (the external state changed and we need
|
|
130
|
+
// to call `.read()` again).
|
|
131
|
+
//
|
|
132
|
+
// This is currently a bit implicit for now since we rely on the fact that
|
|
133
|
+
// only External State Resolvers can have `RELAY_EXTERNAL_STATE_DIRTY`
|
|
134
|
+
// set. However, in the future, we will have a distinct Reader AST node
|
|
135
|
+
// for External State Resolvers, so we won't have to be so implicit.
|
|
136
|
+
if (RelayModernRecord.getValue(linkedRecord, RELAY_EXTERNAL_STATE_DIRTY)) {
|
|
137
|
+
var _linkedID2;
|
|
138
|
+
|
|
139
|
+
linkedID = (_linkedID2 = linkedID) !== null && _linkedID2 !== void 0 ? _linkedID2 : generateClientID(recordID, storageKey);
|
|
140
|
+
linkedRecord = RelayModernRecord.clone(linkedRecord); // $FlowFixMe[incompatible-type] - casting mixed
|
|
141
|
+
|
|
142
|
+
var _externalState = RelayModernRecord.getValue(linkedRecord, RELAY_EXTERNAL_STATE_VALUE); // Set the new value for this and future reads.
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
RelayModernRecord.setValue(linkedRecord, RELAY_RESOLVER_VALUE_KEY, _externalState.read()); // Mark the resolver as clean again.
|
|
146
|
+
|
|
147
|
+
RelayModernRecord.setValue(linkedRecord, RELAY_EXTERNAL_STATE_DIRTY, false);
|
|
148
|
+
recordSource.set(linkedID, linkedRecord);
|
|
149
|
+
}
|
|
150
|
+
} // $FlowFixMe[incompatible-type] - will always be empty
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
var answer = linkedRecord[RELAY_RESOLVER_VALUE_KEY];
|
|
154
|
+
var missingRequiredFields = // $FlowFixMe[incompatible-type] - casting mixed
|
|
155
|
+
linkedRecord[RELAY_RESOLVER_MISSING_REQUIRED_FIELDS_KEY]; // $FlowFixMe[incompatible-type] - casting mixed
|
|
156
|
+
|
|
157
|
+
var errors = linkedRecord[RELAY_RESOLVER_ERROR_KEY];
|
|
158
|
+
return [answer, linkedID, errors, missingRequiredFields];
|
|
159
|
+
} // Register a new External State object in the store, subscribing to future
|
|
160
|
+
// updates.
|
|
161
|
+
;
|
|
162
|
+
|
|
163
|
+
_proto._setExternalStateValue = function _setExternalStateValue(linkedRecord, linkedID, externalState) {
|
|
164
|
+
// If there's an existing subscription, unsubscribe.
|
|
165
|
+
// $FlowFixMe[incompatible-type] - casting mixed
|
|
166
|
+
var previousUnsubscribe = RelayModernRecord.getValue(linkedRecord, RELAY_RESOLVER_EXTERNAL_STATE_SUBSCRIPTION_KEY);
|
|
167
|
+
|
|
168
|
+
if (previousUnsubscribe != null) {
|
|
169
|
+
previousUnsubscribe();
|
|
170
|
+
} // Subscribe to future values
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
var handler = this._makeExternalStateHandler(linkedID, externalState);
|
|
174
|
+
|
|
175
|
+
var unsubscribe = externalState.subscribe(handler); // Store the external state value for future re-reads.
|
|
176
|
+
|
|
177
|
+
RelayModernRecord.setValue(linkedRecord, RELAY_EXTERNAL_STATE_VALUE, externalState); // Store the current value, for this read, and future cached reads.
|
|
178
|
+
|
|
179
|
+
RelayModernRecord.setValue(linkedRecord, RELAY_RESOLVER_VALUE_KEY, externalState.read()); // Mark the field as clean.
|
|
180
|
+
|
|
181
|
+
RelayModernRecord.setValue(linkedRecord, RELAY_EXTERNAL_STATE_DIRTY, false); // Store our our unsubscribe function for future cleanup.
|
|
182
|
+
|
|
183
|
+
RelayModernRecord.setValue(linkedRecord, RELAY_RESOLVER_EXTERNAL_STATE_SUBSCRIPTION_KEY, unsubscribe);
|
|
184
|
+
} // Create a callback to handle notifications from the external source that the
|
|
185
|
+
// value may have changed.
|
|
186
|
+
;
|
|
187
|
+
|
|
188
|
+
_proto._makeExternalStateHandler = function _makeExternalStateHandler(linkedID, externalState) {
|
|
189
|
+
var _this = this;
|
|
190
|
+
|
|
191
|
+
return function () {
|
|
192
|
+
var currentSource = _this._getRecordSource();
|
|
193
|
+
|
|
194
|
+
var currentRecord = currentSource.get(linkedID);
|
|
195
|
+
|
|
196
|
+
if (!currentRecord) {
|
|
197
|
+
process.env.NODE_ENV !== "production" ? warning(false, 'Expected a resolver record with ID %s, but it was missing.', linkedID) : void 0;
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
var nextSource = RelayRecordSource.create();
|
|
202
|
+
var nextRecord = RelayModernRecord.clone(currentRecord); // Mark the field as dirty. The next time it's read, we will call
|
|
203
|
+
// `ExternalState.read()`.
|
|
204
|
+
|
|
205
|
+
RelayModernRecord.setValue(nextRecord, RELAY_EXTERNAL_STATE_DIRTY, true);
|
|
206
|
+
nextSource.set(linkedID, nextRecord);
|
|
207
|
+
|
|
208
|
+
_this._store.publish(nextSource); // In the future, this notify might be defferred if we are within a
|
|
209
|
+
// transaction.
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
_this._store.notify();
|
|
213
|
+
};
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
_proto.invalidateDataIDs = function invalidateDataIDs(updatedDataIDs) {
|
|
217
|
+
var recordSource = this._getRecordSource();
|
|
218
|
+
|
|
219
|
+
var visited = new Set();
|
|
220
|
+
var recordsToVisit = Array.from(updatedDataIDs);
|
|
221
|
+
|
|
222
|
+
while (recordsToVisit.length) {
|
|
223
|
+
var recordID = recordsToVisit.pop();
|
|
224
|
+
updatedDataIDs.add(recordID);
|
|
225
|
+
|
|
226
|
+
var _iterator2 = (0, _createForOfIteratorHelper2["default"])((_this$_recordIDToReso = this._recordIDToResolverIDs.get(recordID)) !== null && _this$_recordIDToReso !== void 0 ? _this$_recordIDToReso : emptySet),
|
|
227
|
+
_step2;
|
|
228
|
+
|
|
229
|
+
try {
|
|
230
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
231
|
+
var _this$_recordIDToReso;
|
|
232
|
+
|
|
233
|
+
var fragment = _step2.value;
|
|
234
|
+
|
|
235
|
+
if (!visited.has(fragment)) {
|
|
236
|
+
var _iterator3 = (0, _createForOfIteratorHelper2["default"])((_this$_resolverIDToRe = this._resolverIDToRecordIDs.get(fragment)) !== null && _this$_resolverIDToRe !== void 0 ? _this$_resolverIDToRe : emptySet),
|
|
237
|
+
_step3;
|
|
238
|
+
|
|
239
|
+
try {
|
|
240
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
241
|
+
var _this$_resolverIDToRe;
|
|
242
|
+
|
|
243
|
+
var anotherRecordID = _step3.value;
|
|
244
|
+
|
|
245
|
+
this._markInvalidatedResolverRecord(anotherRecordID, recordSource);
|
|
246
|
+
|
|
247
|
+
if (!visited.has(anotherRecordID)) {
|
|
248
|
+
recordsToVisit.push(anotherRecordID);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
} catch (err) {
|
|
252
|
+
_iterator3.e(err);
|
|
253
|
+
} finally {
|
|
254
|
+
_iterator3.f();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
} catch (err) {
|
|
259
|
+
_iterator2.e(err);
|
|
260
|
+
} finally {
|
|
261
|
+
_iterator2.f();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
_proto._markInvalidatedResolverRecord = function _markInvalidatedResolverRecord(dataID, recordSource) // Written to
|
|
267
|
+
{
|
|
268
|
+
var record = recordSource.get(dataID);
|
|
269
|
+
|
|
270
|
+
if (!record) {
|
|
271
|
+
process.env.NODE_ENV !== "production" ? warning(false, 'Expected a resolver record with ID %s, but it was missing.', dataID) : void 0;
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
var nextRecord = RelayModernRecord.clone(record);
|
|
276
|
+
RelayModernRecord.setValue(nextRecord, RELAY_RESOLVER_INVALIDATION_KEY, true);
|
|
277
|
+
recordSource.set(dataID, nextRecord);
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
_proto._isInvalid = function _isInvalid(record, getDataForResolverFragment) {
|
|
281
|
+
if (!RelayModernRecord.getValue(record, RELAY_RESOLVER_INVALIDATION_KEY)) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
var originalInputs = RelayModernRecord.getValue(record, RELAY_RESOLVER_INPUTS_KEY); // $FlowFixMe[incompatible-type] - storing values in records is not typed
|
|
286
|
+
|
|
287
|
+
var readerSelector = RelayModernRecord.getValue(record, RELAY_RESOLVER_READER_SELECTOR_KEY);
|
|
288
|
+
|
|
289
|
+
if (originalInputs == null || readerSelector == null) {
|
|
290
|
+
process.env.NODE_ENV !== "production" ? warning(false, 'Expected previous inputs and reader selector on resolver record with ID %s, but they were missing.', RelayModernRecord.getDataID(record)) : void 0;
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
var latestValues = getDataForResolverFragment(readerSelector);
|
|
295
|
+
var recycled = recycleNodesInto(originalInputs, latestValues);
|
|
296
|
+
|
|
297
|
+
if (recycled !== originalInputs) {
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return false;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
return ExternalStateResolverCache;
|
|
305
|
+
}(); // In the real implementaiton, we will probably have a special Reader AST node to tell us when
|
|
306
|
+
// a value is external state
|
|
307
|
+
// $FlowFixMe
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
function isExternalStateValue(v) {
|
|
311
|
+
if (v != null && typeof v.read === 'function' && typeof v.subscribe === 'function') {
|
|
312
|
+
return v;
|
|
313
|
+
} else {
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
module.exports = {
|
|
319
|
+
ExternalStateResolverCache: ExternalStateResolverCache
|
|
320
|
+
};
|