relay-runtime 11.0.0-rc.0 → 12.0.0
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/handlers/connection/ConnectionHandler.js.flow +7 -0
- package/handlers/connection/MutationHandlers.js.flow +28 -0
- package/index.js +1 -1
- package/index.js.flow +20 -3
- package/lib/handlers/RelayDefaultHandlerProvider.js +1 -1
- package/lib/handlers/connection/ConnectionHandler.js +12 -6
- package/lib/handlers/connection/MutationHandlers.js +67 -8
- package/lib/index.js +15 -0
- package/lib/multi-actor-environment/ActorIdentifier.js +33 -0
- package/lib/multi-actor-environment/ActorSpecificEnvironment.js +148 -0
- package/lib/multi-actor-environment/ActorUtils.js +27 -0
- package/lib/multi-actor-environment/MultiActorEnvironment.js +406 -0
- package/lib/multi-actor-environment/MultiActorEnvironmentTypes.js +11 -0
- package/lib/multi-actor-environment/index.js +21 -0
- package/lib/mutations/RelayRecordProxy.js +1 -1
- package/lib/mutations/RelayRecordSourceMutator.js +1 -1
- package/lib/mutations/RelayRecordSourceProxy.js +1 -1
- package/lib/mutations/RelayRecordSourceSelectorProxy.js +7 -2
- package/lib/mutations/applyOptimisticMutation.js +1 -1
- package/lib/mutations/commitMutation.js +5 -2
- package/lib/mutations/validateMutation.js +39 -17
- package/lib/network/RelayNetwork.js +1 -1
- package/lib/network/RelayObservable.js +3 -1
- package/lib/network/RelayQueryResponseCache.js +20 -3
- package/lib/network/wrapNetworkWithLogObserver.js +78 -0
- package/lib/query/GraphQLTag.js +1 -1
- package/lib/query/fetchQuery.js +1 -1
- package/lib/query/fetchQueryInternal.js +1 -1
- package/lib/store/DataChecker.js +132 -50
- package/lib/store/{RelayModernQueryExecutor.js → OperationExecutor.js} +524 -187
- package/lib/store/RelayConcreteVariables.js +29 -4
- package/lib/store/RelayModernEnvironment.js +137 -220
- package/lib/store/RelayModernFragmentSpecResolver.js +49 -23
- package/lib/store/RelayModernRecord.js +36 -2
- package/lib/store/RelayModernSelector.js +1 -1
- package/lib/store/RelayModernStore.js +53 -22
- package/lib/store/RelayOperationTracker.js +34 -24
- package/lib/store/RelayPublishQueue.js +30 -8
- package/lib/store/RelayReader.js +177 -29
- package/lib/store/RelayRecordSource.js +87 -3
- package/lib/store/RelayReferenceMarker.js +53 -28
- package/lib/store/RelayResponseNormalizer.js +247 -108
- package/lib/store/RelayStoreReactFlightUtils.js +7 -11
- package/lib/store/RelayStoreSubscriptions.js +8 -5
- package/lib/store/RelayStoreUtils.js +10 -4
- package/lib/store/ResolverCache.js +213 -0
- package/lib/store/ResolverFragments.js +57 -0
- package/lib/store/cloneRelayHandleSourceField.js +1 -1
- package/lib/store/cloneRelayScalarHandleSourceField.js +1 -1
- package/lib/store/createRelayContext.js +2 -2
- package/lib/store/defaultGetDataID.js +3 -1
- package/lib/store/readInlineData.js +1 -1
- package/lib/subscription/requestSubscription.js +32 -6
- package/lib/util/RelayConcreteNode.js +3 -0
- package/lib/util/RelayFeatureFlags.js +5 -4
- package/lib/util/RelayProfiler.js +17 -187
- package/lib/util/RelayReplaySubject.js +22 -7
- package/lib/util/deepFreeze.js +1 -0
- package/lib/util/getPaginationMetadata.js +41 -0
- package/lib/util/getPaginationVariables.js +67 -0
- package/lib/util/getPendingOperationsForFragment.js +55 -0
- package/lib/util/getRefetchMetadata.js +36 -0
- package/lib/util/getRelayHandleKey.js +1 -1
- package/lib/util/getRequestIdentifier.js +1 -1
- package/lib/util/getValueAtPath.js +51 -0
- package/lib/util/isEmptyObject.js +1 -1
- package/lib/util/registerEnvironmentWithDevTools.js +26 -0
- package/lib/util/withDuration.js +31 -0
- package/multi-actor-environment/ActorIdentifier.js.flow +43 -0
- package/multi-actor-environment/ActorSpecificEnvironment.js.flow +217 -0
- package/multi-actor-environment/ActorUtils.js.flow +33 -0
- package/multi-actor-environment/MultiActorEnvironment.js.flow +485 -0
- package/multi-actor-environment/MultiActorEnvironmentTypes.js.flow +245 -0
- package/multi-actor-environment/index.js.flow +27 -0
- package/mutations/RelayRecordSourceSelectorProxy.js.flow +7 -2
- package/mutations/commitMutation.js.flow +3 -1
- package/mutations/validateMutation.js.flow +42 -16
- package/network/RelayNetworkTypes.js.flow +17 -8
- package/network/RelayObservable.js.flow +2 -0
- package/network/RelayQueryResponseCache.js.flow +31 -17
- package/network/wrapNetworkWithLogObserver.js.flow +99 -0
- package/package.json +3 -2
- package/relay-runtime.js +2 -2
- package/relay-runtime.min.js +2 -2
- package/store/ClientID.js.flow +5 -1
- package/store/DataChecker.js.flow +148 -44
- package/store/{RelayModernQueryExecutor.js.flow → OperationExecutor.js.flow} +578 -237
- package/store/RelayConcreteVariables.js.flow +31 -1
- package/store/RelayModernEnvironment.js.flow +132 -220
- package/store/RelayModernFragmentSpecResolver.js.flow +40 -14
- package/store/RelayModernOperationDescriptor.js.flow +9 -3
- package/store/RelayModernRecord.js.flow +49 -0
- package/store/RelayModernStore.js.flow +57 -17
- package/store/RelayOperationTracker.js.flow +56 -34
- package/store/RelayPublishQueue.js.flow +37 -11
- package/store/RelayReader.js.flow +186 -27
- package/store/RelayRecordSource.js.flow +72 -6
- package/store/RelayReferenceMarker.js.flow +51 -21
- package/store/RelayResponseNormalizer.js.flow +251 -67
- package/store/RelayStoreReactFlightUtils.js.flow +6 -9
- package/store/RelayStoreSubscriptions.js.flow +10 -3
- package/store/RelayStoreTypes.js.flow +144 -21
- package/store/RelayStoreUtils.js.flow +19 -4
- package/store/ResolverCache.js.flow +247 -0
- package/store/ResolverFragments.js.flow +128 -0
- package/store/createRelayContext.js.flow +1 -1
- package/store/defaultGetDataID.js.flow +3 -1
- package/subscription/requestSubscription.js.flow +43 -8
- package/util/NormalizationNode.js.flow +16 -3
- package/util/ReaderNode.js.flow +29 -2
- package/util/RelayConcreteNode.js.flow +3 -0
- package/util/RelayFeatureFlags.js.flow +10 -6
- package/util/RelayProfiler.js.flow +22 -194
- package/util/RelayReplaySubject.js.flow +7 -6
- package/util/RelayRuntimeTypes.js.flow +4 -2
- package/util/deepFreeze.js.flow +2 -1
- package/util/getPaginationMetadata.js.flow +74 -0
- package/util/getPaginationVariables.js.flow +112 -0
- package/util/getPendingOperationsForFragment.js.flow +62 -0
- package/util/getRefetchMetadata.js.flow +80 -0
- package/util/getValueAtPath.js.flow +46 -0
- package/util/isEmptyObject.js.flow +2 -1
- package/util/registerEnvironmentWithDevTools.js.flow +33 -0
- package/util/withDuration.js.flow +32 -0
- package/lib/store/RelayRecordSourceMapImpl.js +0 -107
- package/lib/store/RelayStoreSubscriptionsUsingMapByID.js +0 -318
- package/store/RelayRecordSourceMapImpl.js.flow +0 -91
- package/store/RelayStoreSubscriptionsUsingMapByID.js.flow +0 -283
|
@@ -1,318 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its 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 RelayFeatureFlags = require('../util/RelayFeatureFlags');
|
|
18
|
-
|
|
19
|
-
var RelayReader = require('./RelayReader');
|
|
20
|
-
|
|
21
|
-
var deepFreeze = require('../util/deepFreeze');
|
|
22
|
-
|
|
23
|
-
var recycleNodesInto = require('../util/recycleNodesInto');
|
|
24
|
-
|
|
25
|
-
var RelayStoreSubscriptionsUsingMapByID = /*#__PURE__*/function () {
|
|
26
|
-
function RelayStoreSubscriptionsUsingMapByID(log) {
|
|
27
|
-
this._notifiedRevision = 0;
|
|
28
|
-
this._snapshotRevision = 0;
|
|
29
|
-
this._subscriptionsByDataId = new Map();
|
|
30
|
-
this._staleSubscriptions = new Set();
|
|
31
|
-
this.__log = log;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
var _proto = RelayStoreSubscriptionsUsingMapByID.prototype;
|
|
35
|
-
|
|
36
|
-
_proto.subscribe = function subscribe(snapshot, callback) {
|
|
37
|
-
var _this = this;
|
|
38
|
-
|
|
39
|
-
var subscription = {
|
|
40
|
-
backup: null,
|
|
41
|
-
callback: callback,
|
|
42
|
-
notifiedRevision: this._notifiedRevision,
|
|
43
|
-
snapshotRevision: this._snapshotRevision,
|
|
44
|
-
snapshot: snapshot
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
var dispose = function dispose() {
|
|
48
|
-
var _iterator = (0, _createForOfIteratorHelper2["default"])(snapshot.seenRecords),
|
|
49
|
-
_step;
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
53
|
-
var dataId = _step.value;
|
|
54
|
-
|
|
55
|
-
var subscriptionsForDataId = _this._subscriptionsByDataId.get(dataId);
|
|
56
|
-
|
|
57
|
-
if (subscriptionsForDataId != null) {
|
|
58
|
-
subscriptionsForDataId["delete"](subscription);
|
|
59
|
-
|
|
60
|
-
if (subscriptionsForDataId.size === 0) {
|
|
61
|
-
_this._subscriptionsByDataId["delete"](dataId);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
} catch (err) {
|
|
66
|
-
_iterator.e(err);
|
|
67
|
-
} finally {
|
|
68
|
-
_iterator.f();
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
var _iterator2 = (0, _createForOfIteratorHelper2["default"])(snapshot.seenRecords),
|
|
73
|
-
_step2;
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
77
|
-
var dataId = _step2.value;
|
|
78
|
-
|
|
79
|
-
var subscriptionsForDataId = this._subscriptionsByDataId.get(dataId);
|
|
80
|
-
|
|
81
|
-
if (subscriptionsForDataId != null) {
|
|
82
|
-
subscriptionsForDataId.add(subscription);
|
|
83
|
-
} else {
|
|
84
|
-
this._subscriptionsByDataId.set(dataId, new Set([subscription]));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
} catch (err) {
|
|
88
|
-
_iterator2.e(err);
|
|
89
|
-
} finally {
|
|
90
|
-
_iterator2.f();
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return {
|
|
94
|
-
dispose: dispose
|
|
95
|
-
};
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
_proto.snapshotSubscriptions = function snapshotSubscriptions(source) {
|
|
99
|
-
var _this2 = this;
|
|
100
|
-
|
|
101
|
-
this._snapshotRevision++;
|
|
102
|
-
|
|
103
|
-
this._subscriptionsByDataId.forEach(function (subscriptions) {
|
|
104
|
-
subscriptions.forEach(function (subscription) {
|
|
105
|
-
if (subscription.snapshotRevision === _this2._snapshotRevision) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
subscription.snapshotRevision = _this2._snapshotRevision; // Backup occurs after writing a new "final" payload(s) and before (re)applying
|
|
110
|
-
// optimistic changes. Each subscription's `snapshot` represents what was *last
|
|
111
|
-
// published to the subscriber*, which notably may include previous optimistic
|
|
112
|
-
// updates. Therefore a subscription can be in any of the following states:
|
|
113
|
-
// - stale=true: This subscription was restored to a different value than
|
|
114
|
-
// `snapshot`. That means this subscription has changes relative to its base,
|
|
115
|
-
// but its base has changed (we just applied a final payload): recompute
|
|
116
|
-
// a backup so that we can later restore to the state the subscription
|
|
117
|
-
// should be in.
|
|
118
|
-
// - stale=false: This subscription was restored to the same value than
|
|
119
|
-
// `snapshot`. That means this subscription does *not* have changes relative
|
|
120
|
-
// to its base, so the current `snapshot` is valid to use as a backup.
|
|
121
|
-
|
|
122
|
-
if (!_this2._staleSubscriptions.has(subscription)) {
|
|
123
|
-
subscription.backup = subscription.snapshot;
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
var snapshot = subscription.snapshot;
|
|
128
|
-
var backup = RelayReader.read(source, snapshot.selector);
|
|
129
|
-
var nextData = recycleNodesInto(snapshot.data, backup.data);
|
|
130
|
-
backup.data = nextData; // backup owns the snapshot and can safely mutate
|
|
131
|
-
|
|
132
|
-
subscription.backup = backup;
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
_proto.restoreSubscriptions = function restoreSubscriptions() {
|
|
138
|
-
var _this3 = this;
|
|
139
|
-
|
|
140
|
-
this._snapshotRevision++;
|
|
141
|
-
|
|
142
|
-
this._subscriptionsByDataId.forEach(function (subscriptions) {
|
|
143
|
-
subscriptions.forEach(function (subscription) {
|
|
144
|
-
if (subscription.snapshotRevision === _this3._snapshotRevision) {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
subscription.snapshotRevision = _this3._snapshotRevision;
|
|
149
|
-
var backup = subscription.backup;
|
|
150
|
-
subscription.backup = null;
|
|
151
|
-
|
|
152
|
-
if (backup) {
|
|
153
|
-
if (backup.data !== subscription.snapshot.data) {
|
|
154
|
-
_this3._staleSubscriptions.add(subscription);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
var prevSeenRecords = subscription.snapshot.seenRecords;
|
|
158
|
-
subscription.snapshot = {
|
|
159
|
-
data: subscription.snapshot.data,
|
|
160
|
-
isMissingData: backup.isMissingData,
|
|
161
|
-
seenRecords: backup.seenRecords,
|
|
162
|
-
selector: backup.selector,
|
|
163
|
-
missingRequiredFields: backup.missingRequiredFields
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
_this3._updateSubscriptionsMap(subscription, prevSeenRecords);
|
|
167
|
-
} else {
|
|
168
|
-
_this3._staleSubscriptions.add(subscription);
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
_proto.updateSubscriptions = function updateSubscriptions(source, updatedRecordIDs, updatedOwners, sourceOperation) {
|
|
175
|
-
var _this4 = this;
|
|
176
|
-
|
|
177
|
-
this._notifiedRevision++;
|
|
178
|
-
updatedRecordIDs.forEach(function (updatedRecordId) {
|
|
179
|
-
var subcriptionsForDataId = _this4._subscriptionsByDataId.get(updatedRecordId);
|
|
180
|
-
|
|
181
|
-
if (subcriptionsForDataId == null) {
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
subcriptionsForDataId.forEach(function (subscription) {
|
|
186
|
-
if (subscription.notifiedRevision === _this4._notifiedRevision) {
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
var owner = _this4._updateSubscription(source, subscription, false, sourceOperation);
|
|
191
|
-
|
|
192
|
-
if (owner != null) {
|
|
193
|
-
updatedOwners.push(owner);
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
this._staleSubscriptions.forEach(function (subscription) {
|
|
199
|
-
if (subscription.notifiedRevision === _this4._notifiedRevision) {
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
var owner = _this4._updateSubscription(source, subscription, true, sourceOperation);
|
|
204
|
-
|
|
205
|
-
if (owner != null) {
|
|
206
|
-
updatedOwners.push(owner);
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
this._staleSubscriptions.clear();
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Notifies the callback for the subscription if the data for the associated
|
|
214
|
-
* snapshot has changed.
|
|
215
|
-
* Additionally, updates the subscription snapshot with the latest snapshot,
|
|
216
|
-
* amarks it as not stale, and updates the subscription tracking for any
|
|
217
|
-
* any new ids observed in the latest data snapshot.
|
|
218
|
-
* Returns the owner (RequestDescriptor) if the subscription was affected by the
|
|
219
|
-
* latest update, or null if it was not affected.
|
|
220
|
-
*/
|
|
221
|
-
;
|
|
222
|
-
|
|
223
|
-
_proto._updateSubscription = function _updateSubscription(source, subscription, stale, sourceOperation) {
|
|
224
|
-
var backup = subscription.backup,
|
|
225
|
-
callback = subscription.callback,
|
|
226
|
-
snapshot = subscription.snapshot;
|
|
227
|
-
var nextSnapshot = stale && backup != null ? backup : RelayReader.read(source, snapshot.selector);
|
|
228
|
-
var nextData = recycleNodesInto(snapshot.data, nextSnapshot.data);
|
|
229
|
-
nextSnapshot = {
|
|
230
|
-
data: nextData,
|
|
231
|
-
isMissingData: nextSnapshot.isMissingData,
|
|
232
|
-
seenRecords: nextSnapshot.seenRecords,
|
|
233
|
-
selector: nextSnapshot.selector,
|
|
234
|
-
missingRequiredFields: nextSnapshot.missingRequiredFields
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
if (process.env.NODE_ENV !== "production") {
|
|
238
|
-
deepFreeze(nextSnapshot);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
var prevSeenRecords = subscription.snapshot.seenRecords;
|
|
242
|
-
subscription.snapshot = nextSnapshot;
|
|
243
|
-
subscription.notifiedRevision = this._notifiedRevision;
|
|
244
|
-
|
|
245
|
-
this._updateSubscriptionsMap(subscription, prevSeenRecords);
|
|
246
|
-
|
|
247
|
-
if (nextSnapshot.data !== snapshot.data) {
|
|
248
|
-
if (this.__log && RelayFeatureFlags.ENABLE_NOTIFY_SUBSCRIPTION) {
|
|
249
|
-
this.__log({
|
|
250
|
-
name: 'store.notify.subscription',
|
|
251
|
-
sourceOperation: sourceOperation,
|
|
252
|
-
snapshot: snapshot,
|
|
253
|
-
nextSnapshot: nextSnapshot
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
callback(nextSnapshot);
|
|
258
|
-
return snapshot.selector.owner;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* Updates the Map that tracks subscriptions by id.
|
|
263
|
-
* Given an updated subscription and the records that where seen
|
|
264
|
-
* on the previous subscription snapshot, updates our tracking
|
|
265
|
-
* to track the subscription for the newly and no longer seen ids.
|
|
266
|
-
*/
|
|
267
|
-
;
|
|
268
|
-
|
|
269
|
-
_proto._updateSubscriptionsMap = function _updateSubscriptionsMap(subscription, prevSeenRecords) {
|
|
270
|
-
var _iterator3 = (0, _createForOfIteratorHelper2["default"])(prevSeenRecords),
|
|
271
|
-
_step3;
|
|
272
|
-
|
|
273
|
-
try {
|
|
274
|
-
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
275
|
-
var dataId = _step3.value;
|
|
276
|
-
|
|
277
|
-
var subscriptionsForDataId = this._subscriptionsByDataId.get(dataId);
|
|
278
|
-
|
|
279
|
-
if (subscriptionsForDataId != null) {
|
|
280
|
-
subscriptionsForDataId["delete"](subscription);
|
|
281
|
-
|
|
282
|
-
if (subscriptionsForDataId.size === 0) {
|
|
283
|
-
this._subscriptionsByDataId["delete"](dataId);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
} catch (err) {
|
|
288
|
-
_iterator3.e(err);
|
|
289
|
-
} finally {
|
|
290
|
-
_iterator3.f();
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
var _iterator4 = (0, _createForOfIteratorHelper2["default"])(subscription.snapshot.seenRecords),
|
|
294
|
-
_step4;
|
|
295
|
-
|
|
296
|
-
try {
|
|
297
|
-
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
298
|
-
var _dataId = _step4.value;
|
|
299
|
-
|
|
300
|
-
var _subscriptionsForDataId = this._subscriptionsByDataId.get(_dataId);
|
|
301
|
-
|
|
302
|
-
if (_subscriptionsForDataId != null) {
|
|
303
|
-
_subscriptionsForDataId.add(subscription);
|
|
304
|
-
} else {
|
|
305
|
-
this._subscriptionsByDataId.set(_dataId, new Set([subscription]));
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
} catch (err) {
|
|
309
|
-
_iterator4.e(err);
|
|
310
|
-
} finally {
|
|
311
|
-
_iterator4.f();
|
|
312
|
-
}
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
return RelayStoreSubscriptionsUsingMapByID;
|
|
316
|
-
}();
|
|
317
|
-
|
|
318
|
-
module.exports = RelayStoreSubscriptionsUsingMapByID;
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its 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
|
-
* @flow strict-local
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
// flowlint ambiguous-object-type:error
|
|
12
|
-
|
|
13
|
-
'use strict';
|
|
14
|
-
|
|
15
|
-
const RelayRecordState = require('./RelayRecordState');
|
|
16
|
-
|
|
17
|
-
import type {DataID} from '../util/RelayRuntimeTypes';
|
|
18
|
-
import type {RecordState} from './RelayRecordState';
|
|
19
|
-
import type {
|
|
20
|
-
MutableRecordSource,
|
|
21
|
-
Record,
|
|
22
|
-
RecordObjectMap,
|
|
23
|
-
} from './RelayStoreTypes';
|
|
24
|
-
|
|
25
|
-
const {EXISTENT, NONEXISTENT, UNKNOWN} = RelayRecordState;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* An implementation of the `MutableRecordSource` interface (defined in
|
|
29
|
-
* `RelayStoreTypes`) that holds all records in memory (JS Map).
|
|
30
|
-
*/
|
|
31
|
-
class RelayMapRecordSourceMapImpl implements MutableRecordSource {
|
|
32
|
-
_records: Map<DataID, ?Record>;
|
|
33
|
-
|
|
34
|
-
constructor(records?: RecordObjectMap) {
|
|
35
|
-
this._records = new Map();
|
|
36
|
-
if (records != null) {
|
|
37
|
-
Object.keys(records).forEach(key => {
|
|
38
|
-
this._records.set(key, records[key]);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
clear(): void {
|
|
44
|
-
this._records = new Map();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
delete(dataID: DataID): void {
|
|
48
|
-
this._records.set(dataID, null);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
get(dataID: DataID): ?Record {
|
|
52
|
-
return this._records.get(dataID);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
getRecordIDs(): Array<DataID> {
|
|
56
|
-
return Array.from(this._records.keys());
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
getStatus(dataID: DataID): RecordState {
|
|
60
|
-
if (!this._records.has(dataID)) {
|
|
61
|
-
return UNKNOWN;
|
|
62
|
-
}
|
|
63
|
-
return this._records.get(dataID) == null ? NONEXISTENT : EXISTENT;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
has(dataID: DataID): boolean {
|
|
67
|
-
return this._records.has(dataID);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
remove(dataID: DataID): void {
|
|
71
|
-
this._records.delete(dataID);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
set(dataID: DataID, record: Record): void {
|
|
75
|
-
this._records.set(dataID, record);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
size(): number {
|
|
79
|
-
return this._records.size;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
toJSON(): {[DataID]: ?Record, ...} {
|
|
83
|
-
const obj = {};
|
|
84
|
-
for (const [key, value] of this._records) {
|
|
85
|
-
obj[key] = value;
|
|
86
|
-
}
|
|
87
|
-
return obj;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
module.exports = RelayMapRecordSourceMapImpl;
|
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its 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
|
-
* @flow strict-local
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
// flowlint ambiguous-object-type:error
|
|
12
|
-
|
|
13
|
-
'use strict';
|
|
14
|
-
|
|
15
|
-
const RelayFeatureFlags = require('../util/RelayFeatureFlags');
|
|
16
|
-
const RelayReader = require('./RelayReader');
|
|
17
|
-
|
|
18
|
-
const deepFreeze = require('../util/deepFreeze');
|
|
19
|
-
const recycleNodesInto = require('../util/recycleNodesInto');
|
|
20
|
-
|
|
21
|
-
import type {DataID, Disposable} from '../util/RelayRuntimeTypes';
|
|
22
|
-
import type {
|
|
23
|
-
LogFunction,
|
|
24
|
-
OperationDescriptor,
|
|
25
|
-
RecordSource,
|
|
26
|
-
RequestDescriptor,
|
|
27
|
-
Snapshot,
|
|
28
|
-
StoreSubscriptions,
|
|
29
|
-
DataIDSet,
|
|
30
|
-
} from './RelayStoreTypes';
|
|
31
|
-
|
|
32
|
-
type Subscription = {|
|
|
33
|
-
backup: ?Snapshot,
|
|
34
|
-
callback: (snapshot: Snapshot) => void,
|
|
35
|
-
notifiedRevision: number,
|
|
36
|
-
snapshot: Snapshot,
|
|
37
|
-
snapshotRevision: number,
|
|
38
|
-
|};
|
|
39
|
-
|
|
40
|
-
class RelayStoreSubscriptionsUsingMapByID implements StoreSubscriptions {
|
|
41
|
-
_notifiedRevision: number;
|
|
42
|
-
_snapshotRevision: number;
|
|
43
|
-
_subscriptionsByDataId: Map<DataID, Set<Subscription>>;
|
|
44
|
-
_staleSubscriptions: Set<Subscription>;
|
|
45
|
-
__log: ?LogFunction;
|
|
46
|
-
|
|
47
|
-
constructor(log?: ?LogFunction) {
|
|
48
|
-
this._notifiedRevision = 0;
|
|
49
|
-
this._snapshotRevision = 0;
|
|
50
|
-
this._subscriptionsByDataId = new Map();
|
|
51
|
-
this._staleSubscriptions = new Set();
|
|
52
|
-
this.__log = log;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
subscribe(
|
|
56
|
-
snapshot: Snapshot,
|
|
57
|
-
callback: (snapshot: Snapshot) => void,
|
|
58
|
-
): Disposable {
|
|
59
|
-
const subscription = {
|
|
60
|
-
backup: null,
|
|
61
|
-
callback,
|
|
62
|
-
notifiedRevision: this._notifiedRevision,
|
|
63
|
-
snapshotRevision: this._snapshotRevision,
|
|
64
|
-
snapshot,
|
|
65
|
-
};
|
|
66
|
-
const dispose = () => {
|
|
67
|
-
for (const dataId of snapshot.seenRecords) {
|
|
68
|
-
const subscriptionsForDataId = this._subscriptionsByDataId.get(dataId);
|
|
69
|
-
if (subscriptionsForDataId != null) {
|
|
70
|
-
subscriptionsForDataId.delete(subscription);
|
|
71
|
-
if (subscriptionsForDataId.size === 0) {
|
|
72
|
-
this._subscriptionsByDataId.delete(dataId);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
for (const dataId of snapshot.seenRecords) {
|
|
79
|
-
const subscriptionsForDataId = this._subscriptionsByDataId.get(dataId);
|
|
80
|
-
if (subscriptionsForDataId != null) {
|
|
81
|
-
subscriptionsForDataId.add(subscription);
|
|
82
|
-
} else {
|
|
83
|
-
this._subscriptionsByDataId.set(dataId, new Set([subscription]));
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return {dispose};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
snapshotSubscriptions(source: RecordSource) {
|
|
91
|
-
this._snapshotRevision++;
|
|
92
|
-
this._subscriptionsByDataId.forEach(subscriptions => {
|
|
93
|
-
subscriptions.forEach(subscription => {
|
|
94
|
-
if (subscription.snapshotRevision === this._snapshotRevision) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
subscription.snapshotRevision = this._snapshotRevision;
|
|
98
|
-
|
|
99
|
-
// Backup occurs after writing a new "final" payload(s) and before (re)applying
|
|
100
|
-
// optimistic changes. Each subscription's `snapshot` represents what was *last
|
|
101
|
-
// published to the subscriber*, which notably may include previous optimistic
|
|
102
|
-
// updates. Therefore a subscription can be in any of the following states:
|
|
103
|
-
// - stale=true: This subscription was restored to a different value than
|
|
104
|
-
// `snapshot`. That means this subscription has changes relative to its base,
|
|
105
|
-
// but its base has changed (we just applied a final payload): recompute
|
|
106
|
-
// a backup so that we can later restore to the state the subscription
|
|
107
|
-
// should be in.
|
|
108
|
-
// - stale=false: This subscription was restored to the same value than
|
|
109
|
-
// `snapshot`. That means this subscription does *not* have changes relative
|
|
110
|
-
// to its base, so the current `snapshot` is valid to use as a backup.
|
|
111
|
-
if (!this._staleSubscriptions.has(subscription)) {
|
|
112
|
-
subscription.backup = subscription.snapshot;
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
const snapshot = subscription.snapshot;
|
|
116
|
-
const backup = RelayReader.read(source, snapshot.selector);
|
|
117
|
-
const nextData = recycleNodesInto(snapshot.data, backup.data);
|
|
118
|
-
(backup: $FlowFixMe).data = nextData; // backup owns the snapshot and can safely mutate
|
|
119
|
-
subscription.backup = backup;
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
restoreSubscriptions() {
|
|
125
|
-
this._snapshotRevision++;
|
|
126
|
-
this._subscriptionsByDataId.forEach(subscriptions => {
|
|
127
|
-
subscriptions.forEach(subscription => {
|
|
128
|
-
if (subscription.snapshotRevision === this._snapshotRevision) {
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
subscription.snapshotRevision = this._snapshotRevision;
|
|
132
|
-
|
|
133
|
-
const backup = subscription.backup;
|
|
134
|
-
subscription.backup = null;
|
|
135
|
-
if (backup) {
|
|
136
|
-
if (backup.data !== subscription.snapshot.data) {
|
|
137
|
-
this._staleSubscriptions.add(subscription);
|
|
138
|
-
}
|
|
139
|
-
const prevSeenRecords = subscription.snapshot.seenRecords;
|
|
140
|
-
subscription.snapshot = {
|
|
141
|
-
data: subscription.snapshot.data,
|
|
142
|
-
isMissingData: backup.isMissingData,
|
|
143
|
-
seenRecords: backup.seenRecords,
|
|
144
|
-
selector: backup.selector,
|
|
145
|
-
missingRequiredFields: backup.missingRequiredFields,
|
|
146
|
-
};
|
|
147
|
-
this._updateSubscriptionsMap(subscription, prevSeenRecords);
|
|
148
|
-
} else {
|
|
149
|
-
this._staleSubscriptions.add(subscription);
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
updateSubscriptions(
|
|
156
|
-
source: RecordSource,
|
|
157
|
-
updatedRecordIDs: DataIDSet,
|
|
158
|
-
updatedOwners: Array<RequestDescriptor>,
|
|
159
|
-
sourceOperation?: OperationDescriptor,
|
|
160
|
-
) {
|
|
161
|
-
this._notifiedRevision++;
|
|
162
|
-
updatedRecordIDs.forEach(updatedRecordId => {
|
|
163
|
-
const subcriptionsForDataId = this._subscriptionsByDataId.get(
|
|
164
|
-
updatedRecordId,
|
|
165
|
-
);
|
|
166
|
-
if (subcriptionsForDataId == null) {
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
subcriptionsForDataId.forEach(subscription => {
|
|
170
|
-
if (subscription.notifiedRevision === this._notifiedRevision) {
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
const owner = this._updateSubscription(
|
|
174
|
-
source,
|
|
175
|
-
subscription,
|
|
176
|
-
false,
|
|
177
|
-
sourceOperation,
|
|
178
|
-
);
|
|
179
|
-
if (owner != null) {
|
|
180
|
-
updatedOwners.push(owner);
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
this._staleSubscriptions.forEach(subscription => {
|
|
185
|
-
if (subscription.notifiedRevision === this._notifiedRevision) {
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
const owner = this._updateSubscription(
|
|
189
|
-
source,
|
|
190
|
-
subscription,
|
|
191
|
-
true,
|
|
192
|
-
sourceOperation,
|
|
193
|
-
);
|
|
194
|
-
if (owner != null) {
|
|
195
|
-
updatedOwners.push(owner);
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
this._staleSubscriptions.clear();
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Notifies the callback for the subscription if the data for the associated
|
|
203
|
-
* snapshot has changed.
|
|
204
|
-
* Additionally, updates the subscription snapshot with the latest snapshot,
|
|
205
|
-
* amarks it as not stale, and updates the subscription tracking for any
|
|
206
|
-
* any new ids observed in the latest data snapshot.
|
|
207
|
-
* Returns the owner (RequestDescriptor) if the subscription was affected by the
|
|
208
|
-
* latest update, or null if it was not affected.
|
|
209
|
-
*/
|
|
210
|
-
_updateSubscription(
|
|
211
|
-
source: RecordSource,
|
|
212
|
-
subscription: Subscription,
|
|
213
|
-
stale: boolean,
|
|
214
|
-
sourceOperation?: OperationDescriptor,
|
|
215
|
-
): ?RequestDescriptor {
|
|
216
|
-
const {backup, callback, snapshot} = subscription;
|
|
217
|
-
let nextSnapshot: Snapshot =
|
|
218
|
-
stale && backup != null
|
|
219
|
-
? backup
|
|
220
|
-
: RelayReader.read(source, snapshot.selector);
|
|
221
|
-
const nextData = recycleNodesInto(snapshot.data, nextSnapshot.data);
|
|
222
|
-
nextSnapshot = ({
|
|
223
|
-
data: nextData,
|
|
224
|
-
isMissingData: nextSnapshot.isMissingData,
|
|
225
|
-
seenRecords: nextSnapshot.seenRecords,
|
|
226
|
-
selector: nextSnapshot.selector,
|
|
227
|
-
missingRequiredFields: nextSnapshot.missingRequiredFields,
|
|
228
|
-
}: Snapshot);
|
|
229
|
-
if (__DEV__) {
|
|
230
|
-
deepFreeze(nextSnapshot);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const prevSeenRecords = subscription.snapshot.seenRecords;
|
|
234
|
-
subscription.snapshot = nextSnapshot;
|
|
235
|
-
subscription.notifiedRevision = this._notifiedRevision;
|
|
236
|
-
this._updateSubscriptionsMap(subscription, prevSeenRecords);
|
|
237
|
-
|
|
238
|
-
if (nextSnapshot.data !== snapshot.data) {
|
|
239
|
-
if (this.__log && RelayFeatureFlags.ENABLE_NOTIFY_SUBSCRIPTION) {
|
|
240
|
-
this.__log({
|
|
241
|
-
name: 'store.notify.subscription',
|
|
242
|
-
sourceOperation,
|
|
243
|
-
snapshot,
|
|
244
|
-
nextSnapshot,
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
callback(nextSnapshot);
|
|
248
|
-
return snapshot.selector.owner;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* Updates the Map that tracks subscriptions by id.
|
|
254
|
-
* Given an updated subscription and the records that where seen
|
|
255
|
-
* on the previous subscription snapshot, updates our tracking
|
|
256
|
-
* to track the subscription for the newly and no longer seen ids.
|
|
257
|
-
*/
|
|
258
|
-
_updateSubscriptionsMap(
|
|
259
|
-
subscription: Subscription,
|
|
260
|
-
prevSeenRecords: DataIDSet,
|
|
261
|
-
) {
|
|
262
|
-
for (const dataId of prevSeenRecords) {
|
|
263
|
-
const subscriptionsForDataId = this._subscriptionsByDataId.get(dataId);
|
|
264
|
-
if (subscriptionsForDataId != null) {
|
|
265
|
-
subscriptionsForDataId.delete(subscription);
|
|
266
|
-
if (subscriptionsForDataId.size === 0) {
|
|
267
|
-
this._subscriptionsByDataId.delete(dataId);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
for (const dataId of subscription.snapshot.seenRecords) {
|
|
273
|
-
const subscriptionsForDataId = this._subscriptionsByDataId.get(dataId);
|
|
274
|
-
if (subscriptionsForDataId != null) {
|
|
275
|
-
subscriptionsForDataId.add(subscription);
|
|
276
|
-
} else {
|
|
277
|
-
this._subscriptionsByDataId.set(dataId, new Set([subscription]));
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
module.exports = RelayStoreSubscriptionsUsingMapByID;
|