react-relay 0.0.0-main-1e923be9 → 0.0.0-main-764af24d
Sign up to get free protection for your applications and to get access to all the features.
- package/ReactRelayContext.js +1 -1
- package/hooks.js +1 -1
- package/index.js +1 -1
- package/legacy.js +1 -1
- package/lib/relay-hooks/react-cache/RelayReactCache.js +37 -0
- package/lib/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js +197 -0
- package/lib/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js +400 -0
- package/lib/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js +45 -0
- package/package.json +2 -2
- package/react-relay-hooks.js +1 -1
- package/react-relay-hooks.min.js +1 -1
- package/react-relay-legacy.js +1 -1
- package/react-relay-legacy.min.js +1 -1
- package/react-relay.js +1 -1
- package/react-relay.min.js +1 -1
- package/relay-hooks/FragmentResource.js.flow +1 -1
- package/relay-hooks/react-cache/RelayReactCache.js.flow +42 -0
- package/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js.flow +243 -0
- package/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js.flow +418 -0
- package/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js.flow +66 -0
package/ReactRelayContext.js
CHANGED
package/hooks.js
CHANGED
package/index.js
CHANGED
package/legacy.js
CHANGED
@@ -0,0 +1,37 @@
|
|
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
|
+
* @emails oncall+relay
|
9
|
+
* @format
|
10
|
+
*/
|
11
|
+
// flowlint ambiguous-object-type:error
|
12
|
+
'use strict';
|
13
|
+
|
14
|
+
var invariant = require('invariant'); // $FlowFixMe[prop-missing] These exist in experimental builds but aren't in React's types yet.
|
15
|
+
|
16
|
+
|
17
|
+
var _require = require('react'),
|
18
|
+
unstable_getCacheForType = _require.unstable_getCacheForType,
|
19
|
+
unstable_getCacheSignal = _require.unstable_getCacheSignal;
|
20
|
+
|
21
|
+
var _require2 = require('relay-runtime'),
|
22
|
+
RelayFeatureFlags = _require2.RelayFeatureFlags;
|
23
|
+
|
24
|
+
function getCacheForType(factory) {
|
25
|
+
!(typeof unstable_getCacheForType === 'function' && RelayFeatureFlags.USE_REACT_CACHE) ? process.env.NODE_ENV !== "production" ? invariant(false, 'RelayReactCache.getCacheForType should only be called when the USE_REACT_CACHE feature flag is enabled and when on an experimental React build that supports it.') : invariant(false) : void 0;
|
26
|
+
return unstable_getCacheForType(factory);
|
27
|
+
}
|
28
|
+
|
29
|
+
function getCacheSignal() {
|
30
|
+
!(typeof unstable_getCacheSignal === 'function' && RelayFeatureFlags.USE_REACT_CACHE) ? process.env.NODE_ENV !== "production" ? invariant(false, 'RelayReactCache.getCacheSignal should only be called when the USE_REACT_CACHE feature flag is enabled and when on an experimental React build that supports it.') : invariant(false) : void 0;
|
31
|
+
return unstable_getCacheSignal();
|
32
|
+
}
|
33
|
+
|
34
|
+
module.exports = {
|
35
|
+
getCacheForType: getCacheForType,
|
36
|
+
getCacheSignal: getCacheSignal
|
37
|
+
};
|
@@ -0,0 +1,197 @@
|
|
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
|
+
* @emails oncall+relay
|
9
|
+
* @format
|
10
|
+
*/
|
11
|
+
// flowlint ambiguous-object-type:error
|
12
|
+
'use strict';
|
13
|
+
|
14
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
15
|
+
|
16
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
17
|
+
|
18
|
+
var _require = require('./RelayReactCache'),
|
19
|
+
getCacheForType = _require.getCacheForType,
|
20
|
+
getCacheSignal = _require.getCacheSignal;
|
21
|
+
|
22
|
+
var invariant = require('invariant');
|
23
|
+
|
24
|
+
var _require2 = require('relay-runtime'),
|
25
|
+
fetchQueryInternal = _require2.__internal.fetchQuery;
|
26
|
+
|
27
|
+
var warning = require("fbjs/lib/warning");
|
28
|
+
|
29
|
+
var DEFAULT_FETCH_POLICY = 'store-or-network';
|
30
|
+
|
31
|
+
function createQueryCache() {
|
32
|
+
return new Map();
|
33
|
+
}
|
34
|
+
|
35
|
+
function getQueryCacheKey(operation, fetchPolicy, renderPolicy) {
|
36
|
+
var cacheIdentifier = "".concat(fetchPolicy, "-").concat(renderPolicy, "-").concat(operation.request.identifier);
|
37
|
+
return cacheIdentifier;
|
38
|
+
}
|
39
|
+
|
40
|
+
function constructQueryResult(operation) {
|
41
|
+
var rootFragmentRef = {
|
42
|
+
__id: operation.fragment.dataID,
|
43
|
+
__fragments: (0, _defineProperty2["default"])({}, operation.fragment.node.name, operation.request.variables),
|
44
|
+
__fragmentOwner: operation.request
|
45
|
+
};
|
46
|
+
return {
|
47
|
+
fragmentNode: operation.request.node.fragment,
|
48
|
+
fragmentRef: rootFragmentRef
|
49
|
+
};
|
50
|
+
}
|
51
|
+
|
52
|
+
function getQueryResultOrFetchQuery_REACT_CACHE(environment, queryOperationDescriptor) {
|
53
|
+
var fetchPolicy = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_FETCH_POLICY;
|
54
|
+
var maybeRenderPolicy = arguments.length > 3 ? arguments[3] : undefined;
|
55
|
+
var renderPolicy = maybeRenderPolicy !== null && maybeRenderPolicy !== void 0 ? maybeRenderPolicy : environment.UNSTABLE_getDefaultRenderPolicy();
|
56
|
+
var cache = getCacheForType(createQueryCache);
|
57
|
+
var cacheKey = getQueryCacheKey(queryOperationDescriptor, fetchPolicy, renderPolicy);
|
58
|
+
var entry = cache.get(cacheKey);
|
59
|
+
|
60
|
+
if (entry === undefined) {
|
61
|
+
// Initiate a query to fetch the data if needed:
|
62
|
+
entry = onCacheMiss(environment, queryOperationDescriptor, fetchPolicy, renderPolicy, function (newCacheEntry) {
|
63
|
+
cache.set(cacheKey, newCacheEntry);
|
64
|
+
});
|
65
|
+
cache.set(cacheKey, entry); // Since this is the first time rendering, retain the query. React will
|
66
|
+
// trigger the abort signal when this cache entry is no longer needed.
|
67
|
+
|
68
|
+
var retention = environment.retain(queryOperationDescriptor);
|
69
|
+
var abortSignal = getCacheSignal();
|
70
|
+
abortSignal.addEventListener('abort', function () {
|
71
|
+
retention.dispose();
|
72
|
+
cache["delete"](cacheKey);
|
73
|
+
}, {
|
74
|
+
once: true
|
75
|
+
});
|
76
|
+
}
|
77
|
+
|
78
|
+
switch (entry.status) {
|
79
|
+
case 'pending':
|
80
|
+
throw entry.promise;
|
81
|
+
|
82
|
+
case 'rejected':
|
83
|
+
throw entry.error;
|
84
|
+
|
85
|
+
case 'resolved':
|
86
|
+
return entry.result;
|
87
|
+
}
|
88
|
+
|
89
|
+
!false ? process.env.NODE_ENV !== "production" ? invariant(false, 'switch statement should be exhaustive') : invariant(false) : void 0;
|
90
|
+
}
|
91
|
+
|
92
|
+
function onCacheMiss(environment, operation, fetchPolicy, renderPolicy, updateCache) {
|
93
|
+
// NB: Besides checking if the data is available, calling `check` will write missing
|
94
|
+
// data to the store using any missing data handlers specified in the environment.
|
95
|
+
var queryAvailability = environment.check(operation);
|
96
|
+
var queryStatus = queryAvailability.status;
|
97
|
+
var hasFullQuery = queryStatus === 'available';
|
98
|
+
var canPartialRender = hasFullQuery || renderPolicy === 'partial' && queryStatus !== 'stale';
|
99
|
+
var shouldFetch;
|
100
|
+
var shouldRenderNow;
|
101
|
+
|
102
|
+
switch (fetchPolicy) {
|
103
|
+
case 'store-only':
|
104
|
+
{
|
105
|
+
shouldFetch = false;
|
106
|
+
shouldRenderNow = true;
|
107
|
+
break;
|
108
|
+
}
|
109
|
+
|
110
|
+
case 'store-or-network':
|
111
|
+
{
|
112
|
+
shouldFetch = !hasFullQuery;
|
113
|
+
shouldRenderNow = canPartialRender;
|
114
|
+
break;
|
115
|
+
}
|
116
|
+
|
117
|
+
case 'store-and-network':
|
118
|
+
{
|
119
|
+
shouldFetch = true;
|
120
|
+
shouldRenderNow = canPartialRender;
|
121
|
+
break;
|
122
|
+
}
|
123
|
+
|
124
|
+
case 'network-only':
|
125
|
+
default:
|
126
|
+
{
|
127
|
+
shouldFetch = true;
|
128
|
+
shouldRenderNow = false;
|
129
|
+
break;
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
var promise = shouldFetch ? executeOperationAndKeepUpToDate(environment, operation, updateCache) : undefined;
|
134
|
+
|
135
|
+
if (shouldRenderNow) {
|
136
|
+
return {
|
137
|
+
status: 'resolved',
|
138
|
+
result: constructQueryResult(operation)
|
139
|
+
};
|
140
|
+
} else {
|
141
|
+
!promise ? process.env.NODE_ENV !== "production" ? invariant(false, 'Should either fetch or render (or both), otherwise we would suspend forever.') : invariant(false) : void 0;
|
142
|
+
return {
|
143
|
+
status: 'pending',
|
144
|
+
promise: promise
|
145
|
+
};
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
function executeOperationAndKeepUpToDate(environment, operation, updateCache) {
|
150
|
+
var resolvePromise;
|
151
|
+
var promise = new Promise(function (r) {
|
152
|
+
resolvePromise = r;
|
153
|
+
}); // $FlowExpectedError[prop-missing] Expando to annotate Promises.
|
154
|
+
|
155
|
+
promise.displayName = 'Relay(' + operation.request.node.operation.name + ')';
|
156
|
+
var isFirstPayload = true; // FIXME We may still need to cancel network requests for live queries.
|
157
|
+
|
158
|
+
var fetchObservable = fetchQueryInternal(environment, operation);
|
159
|
+
fetchObservable.subscribe({
|
160
|
+
start: function start(subscription) {},
|
161
|
+
error: function error(_error) {
|
162
|
+
if (isFirstPayload) {
|
163
|
+
updateCache({
|
164
|
+
status: 'rejected',
|
165
|
+
error: _error
|
166
|
+
});
|
167
|
+
} else {
|
168
|
+
// TODO:T92030819 Remove this warning and actually throw the network error
|
169
|
+
// To complete this task we need to have a way of precisely tracking suspendable points
|
170
|
+
process.env.NODE_ENV !== "production" ? warning(false, 'getQueryResultOrFetchQuery: An incremental payload for query `%` returned an error: `%`:`%`.', operation.request.node.operation.name, _error.message, _error.stack) : void 0;
|
171
|
+
}
|
172
|
+
|
173
|
+
resolvePromise();
|
174
|
+
isFirstPayload = false;
|
175
|
+
},
|
176
|
+
next: function next(response) {
|
177
|
+
// Stop suspending on the first payload because of streaming, defer, etc.
|
178
|
+
updateCache({
|
179
|
+
status: 'resolved',
|
180
|
+
result: constructQueryResult(operation)
|
181
|
+
});
|
182
|
+
resolvePromise();
|
183
|
+
isFirstPayload = false;
|
184
|
+
},
|
185
|
+
complete: function complete() {
|
186
|
+
updateCache({
|
187
|
+
status: 'resolved',
|
188
|
+
result: constructQueryResult(operation)
|
189
|
+
});
|
190
|
+
resolvePromise();
|
191
|
+
isFirstPayload = false;
|
192
|
+
}
|
193
|
+
});
|
194
|
+
return promise;
|
195
|
+
}
|
196
|
+
|
197
|
+
module.exports = getQueryResultOrFetchQuery_REACT_CACHE;
|
@@ -0,0 +1,400 @@
|
|
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
|
+
* @emails oncall+relay
|
9
|
+
* @format
|
10
|
+
*/
|
11
|
+
// flowlint ambiguous-object-type:error
|
12
|
+
'use strict';
|
13
|
+
|
14
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
15
|
+
|
16
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
17
|
+
|
18
|
+
var _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));
|
19
|
+
|
20
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
21
|
+
|
22
|
+
var useRelayEnvironment = require('../useRelayEnvironment');
|
23
|
+
|
24
|
+
var getQueryResultOrFetchQuery = require('./getQueryResultOrFetchQuery_REACT_CACHE');
|
25
|
+
|
26
|
+
var invariant = require('invariant');
|
27
|
+
|
28
|
+
var _require = require('react'),
|
29
|
+
useDebugValue = _require.useDebugValue,
|
30
|
+
useEffect = _require.useEffect,
|
31
|
+
useMemo = _require.useMemo,
|
32
|
+
useRef = _require.useRef,
|
33
|
+
useState = _require.useState;
|
34
|
+
|
35
|
+
var _require2 = require('relay-runtime'),
|
36
|
+
areEqualSelectors = _require2.areEqualSelectors,
|
37
|
+
createOperationDescriptor = _require2.createOperationDescriptor,
|
38
|
+
getPendingOperationsForFragment = _require2.getPendingOperationsForFragment,
|
39
|
+
getSelector = _require2.getSelector,
|
40
|
+
getVariablesFromFragment = _require2.getVariablesFromFragment,
|
41
|
+
recycleNodesInto = _require2.recycleNodesInto,
|
42
|
+
reportMissingRequiredFields = _require2.reportMissingRequiredFields;
|
43
|
+
|
44
|
+
function isMissingData(state) {
|
45
|
+
if (state.kind === 'bailout') {
|
46
|
+
return false;
|
47
|
+
} else if (state.kind === 'singular') {
|
48
|
+
return state.snapshot.isMissingData;
|
49
|
+
} else {
|
50
|
+
return state.snapshots.some(function (s) {
|
51
|
+
return s.isMissingData;
|
52
|
+
});
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
function getMissingClientEdges(state) {
|
57
|
+
if (state.kind === 'bailout') {
|
58
|
+
return null;
|
59
|
+
} else if (state.kind === 'singular') {
|
60
|
+
var _state$snapshot$missi;
|
61
|
+
|
62
|
+
return (_state$snapshot$missi = state.snapshot.missingClientEdges) !== null && _state$snapshot$missi !== void 0 ? _state$snapshot$missi : null;
|
63
|
+
} else {
|
64
|
+
var edges = null;
|
65
|
+
|
66
|
+
var _iterator = (0, _createForOfIteratorHelper2["default"])(state.snapshots),
|
67
|
+
_step;
|
68
|
+
|
69
|
+
try {
|
70
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
71
|
+
var snapshot = _step.value;
|
72
|
+
|
73
|
+
if (snapshot.missingClientEdges) {
|
74
|
+
var _edges;
|
75
|
+
|
76
|
+
edges = (_edges = edges) !== null && _edges !== void 0 ? _edges : [];
|
77
|
+
|
78
|
+
var _iterator2 = (0, _createForOfIteratorHelper2["default"])(snapshot.missingClientEdges),
|
79
|
+
_step2;
|
80
|
+
|
81
|
+
try {
|
82
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
83
|
+
var edge = _step2.value;
|
84
|
+
edges.push(edge);
|
85
|
+
}
|
86
|
+
} catch (err) {
|
87
|
+
_iterator2.e(err);
|
88
|
+
} finally {
|
89
|
+
_iterator2.f();
|
90
|
+
}
|
91
|
+
}
|
92
|
+
}
|
93
|
+
} catch (err) {
|
94
|
+
_iterator.e(err);
|
95
|
+
} finally {
|
96
|
+
_iterator.f();
|
97
|
+
}
|
98
|
+
|
99
|
+
return edges;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
function reportMissingRequiredFieldsForState(environment, state) {
|
104
|
+
if (state.kind === 'singular') {
|
105
|
+
if (state.snapshot.missingRequiredFields != null) {
|
106
|
+
reportMissingRequiredFields(environment, state.snapshot.missingRequiredFields);
|
107
|
+
}
|
108
|
+
} else if (state.kind === 'plural') {
|
109
|
+
var _iterator3 = (0, _createForOfIteratorHelper2["default"])(state.snapshots),
|
110
|
+
_step3;
|
111
|
+
|
112
|
+
try {
|
113
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
114
|
+
var snapshot = _step3.value;
|
115
|
+
|
116
|
+
if (snapshot.missingRequiredFields != null) {
|
117
|
+
reportMissingRequiredFields(environment, snapshot.missingRequiredFields);
|
118
|
+
}
|
119
|
+
}
|
120
|
+
} catch (err) {
|
121
|
+
_iterator3.e(err);
|
122
|
+
} finally {
|
123
|
+
_iterator3.f();
|
124
|
+
}
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
function handleMissedUpdates(environment, state, setState) {
|
129
|
+
if (state.kind === 'bailout') {
|
130
|
+
return;
|
131
|
+
}
|
132
|
+
|
133
|
+
var currentEpoch = environment.getStore().getEpoch();
|
134
|
+
|
135
|
+
if (currentEpoch === state.epoch) {
|
136
|
+
return;
|
137
|
+
} // The store has updated since we rendered (without us being subscribed yet),
|
138
|
+
// so check for any updates to the data we're rendering:
|
139
|
+
|
140
|
+
|
141
|
+
if (state.kind === 'singular') {
|
142
|
+
var currentSnapshot = environment.lookup(state.snapshot.selector);
|
143
|
+
var updatedData = recycleNodesInto(state.snapshot.data, currentSnapshot.data);
|
144
|
+
|
145
|
+
if (updatedData !== state.snapshot.data) {
|
146
|
+
setState({
|
147
|
+
kind: 'singular',
|
148
|
+
snapshot: currentSnapshot,
|
149
|
+
epoch: currentEpoch
|
150
|
+
});
|
151
|
+
}
|
152
|
+
} else {
|
153
|
+
var updates = null;
|
154
|
+
|
155
|
+
for (var index = 0; index < state.snapshots.length; index++) {
|
156
|
+
var snapshot = state.snapshots[index];
|
157
|
+
|
158
|
+
var _currentSnapshot = environment.lookup(snapshot.selector);
|
159
|
+
|
160
|
+
var _updatedData = recycleNodesInto(snapshot.data, _currentSnapshot.data);
|
161
|
+
|
162
|
+
if (_updatedData !== snapshot.data) {
|
163
|
+
updates = updates === null ? new Array(state.snapshots.length) : updates;
|
164
|
+
updates[index] = snapshot;
|
165
|
+
}
|
166
|
+
}
|
167
|
+
|
168
|
+
if (updates !== null) {
|
169
|
+
var theUpdates = updates; // preserve flow refinement.
|
170
|
+
|
171
|
+
setState(function (existing) {
|
172
|
+
!(existing.kind === 'plural') ? process.env.NODE_ENV !== "production" ? invariant(false, 'Cannot go from singular to plural or from bailout to plural.') : invariant(false) : void 0;
|
173
|
+
var updated = (0, _toConsumableArray2["default"])(existing.snapshots);
|
174
|
+
|
175
|
+
for (var _index = 0; _index < theUpdates.length; _index++) {
|
176
|
+
var updatedSnapshot = theUpdates[_index];
|
177
|
+
|
178
|
+
if (updatedSnapshot) {
|
179
|
+
updated[_index] = updatedSnapshot;
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
return {
|
184
|
+
kind: 'plural',
|
185
|
+
snapshots: updated,
|
186
|
+
epoch: currentEpoch
|
187
|
+
};
|
188
|
+
});
|
189
|
+
}
|
190
|
+
}
|
191
|
+
}
|
192
|
+
|
193
|
+
function handleMissingClientEdge(environment, parentFragmentNode, parentFragmentRef, missingClientEdgeRequestInfo, queryOptions) {
|
194
|
+
var originalVariables = getVariablesFromFragment(parentFragmentNode, parentFragmentRef);
|
195
|
+
var variables = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, originalVariables), {}, {
|
196
|
+
id: missingClientEdgeRequestInfo.clientEdgeDestinationID // TODO should be a reserved name
|
197
|
+
|
198
|
+
});
|
199
|
+
var queryOperationDescriptor = createOperationDescriptor(missingClientEdgeRequestInfo.request, variables, queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.networkCacheConfig); // This may suspend. We don't need to do anything with the results; all we're
|
200
|
+
// doing here is started the query if needed and retaining and releasing it
|
201
|
+
// according to the component mount/suspense cycle; getQueryResultOrFetchQuery
|
202
|
+
// already handles this by itself.
|
203
|
+
|
204
|
+
getQueryResultOrFetchQuery(environment, queryOperationDescriptor, queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.fetchPolicy);
|
205
|
+
}
|
206
|
+
|
207
|
+
function subscribeToSnapshot(environment, state, setState) {
|
208
|
+
if (state.kind === 'bailout') {
|
209
|
+
return function () {};
|
210
|
+
} else if (state.kind === 'singular') {
|
211
|
+
var disposable = environment.subscribe(state.snapshot, function (latestSnapshot) {
|
212
|
+
setState({
|
213
|
+
kind: 'singular',
|
214
|
+
snapshot: latestSnapshot,
|
215
|
+
epoch: environment.getStore().getEpoch()
|
216
|
+
});
|
217
|
+
});
|
218
|
+
return function () {
|
219
|
+
disposable.dispose();
|
220
|
+
};
|
221
|
+
} else {
|
222
|
+
var disposables = state.snapshots.map(function (snapshot, index) {
|
223
|
+
return environment.subscribe(snapshot, function (latestSnapshot) {
|
224
|
+
setState(function (existing) {
|
225
|
+
!(existing.kind === 'plural') ? process.env.NODE_ENV !== "production" ? invariant(false, 'Cannot go from singular to plural or from bailout to plural.') : invariant(false) : void 0;
|
226
|
+
var updated = (0, _toConsumableArray2["default"])(existing.snapshots);
|
227
|
+
updated[index] = latestSnapshot;
|
228
|
+
return {
|
229
|
+
kind: 'plural',
|
230
|
+
snapshots: updated,
|
231
|
+
epoch: environment.getStore().getEpoch()
|
232
|
+
};
|
233
|
+
});
|
234
|
+
});
|
235
|
+
});
|
236
|
+
return function () {
|
237
|
+
var _iterator4 = (0, _createForOfIteratorHelper2["default"])(disposables),
|
238
|
+
_step4;
|
239
|
+
|
240
|
+
try {
|
241
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
242
|
+
var d = _step4.value;
|
243
|
+
d.dispose();
|
244
|
+
}
|
245
|
+
} catch (err) {
|
246
|
+
_iterator4.e(err);
|
247
|
+
} finally {
|
248
|
+
_iterator4.f();
|
249
|
+
}
|
250
|
+
};
|
251
|
+
}
|
252
|
+
}
|
253
|
+
|
254
|
+
function getFragmentState(environment, fragmentSelector) {
|
255
|
+
if (fragmentSelector == null) {
|
256
|
+
return {
|
257
|
+
kind: 'bailout'
|
258
|
+
};
|
259
|
+
} else if (fragmentSelector.kind === 'PluralReaderSelector') {
|
260
|
+
return {
|
261
|
+
kind: 'plural',
|
262
|
+
snapshots: fragmentSelector.selectors.map(function (s) {
|
263
|
+
return environment.lookup(s);
|
264
|
+
}),
|
265
|
+
epoch: environment.getStore().getEpoch()
|
266
|
+
};
|
267
|
+
} else {
|
268
|
+
return {
|
269
|
+
kind: 'singular',
|
270
|
+
snapshot: environment.lookup(fragmentSelector),
|
271
|
+
epoch: environment.getStore().getEpoch()
|
272
|
+
};
|
273
|
+
}
|
274
|
+
} // fragmentNode cannot change during the lifetime of the component, though fragmentRef may change.
|
275
|
+
|
276
|
+
|
277
|
+
function useFragmentInternal_REACT_CACHE(fragmentNode, fragmentRef, hookDisplayName, queryOptions, fragmentKey) {
|
278
|
+
var _fragmentNode$metadat;
|
279
|
+
|
280
|
+
var fragmentSelector = getSelector(fragmentNode, fragmentRef);
|
281
|
+
|
282
|
+
if ((fragmentNode === null || fragmentNode === void 0 ? void 0 : (_fragmentNode$metadat = fragmentNode.metadata) === null || _fragmentNode$metadat === void 0 ? void 0 : _fragmentNode$metadat.plural) === true) {
|
283
|
+
!Array.isArray(fragmentRef) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: Expected fragment pointer%s for fragment `%s` to be ' + 'an array, instead got `%s`. Remove `@relay(plural: true)` ' + 'from fragment `%s` to allow the prop to be an object.', fragmentKey != null ? " for key `".concat(fragmentKey, "`") : '', fragmentNode.name, typeof fragmentRef, fragmentNode.name) : invariant(false) : void 0;
|
284
|
+
} else {
|
285
|
+
!!Array.isArray(fragmentRef) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: Expected fragment pointer%s for fragment `%s` not to be ' + 'an array, instead got `%s`. Add `@relay(plural: true)` ' + 'to fragment `%s` to allow the prop to be an array.', fragmentKey != null ? " for key `".concat(fragmentKey, "`") : '', fragmentNode.name, typeof fragmentRef, fragmentNode.name) : invariant(false) : void 0;
|
286
|
+
}
|
287
|
+
|
288
|
+
!(fragmentRef == null || fragmentSelector != null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: Expected to receive an object where `...%s` was spread, ' + 'but the fragment reference was not found`. This is most ' + 'likely the result of:\n' + "- Forgetting to spread `%s` in `%s`'s parent's fragment.\n" + '- Conditionally fetching `%s` but unconditionally passing %s prop ' + 'to `%s`. If the parent fragment only fetches the fragment conditionally ' + '- with e.g. `@include`, `@skip`, or inside a `... on SomeType { }` ' + 'spread - then the fragment reference will not exist. ' + 'In this case, pass `null` if the conditions for evaluating the ' + 'fragment are not met (e.g. if the `@include(if)` value is false.)', fragmentNode.name, fragmentNode.name, hookDisplayName, fragmentNode.name, fragmentKey == null ? 'a fragment reference' : "the `".concat(fragmentKey, "`"), hookDisplayName) : invariant(false) : void 0;
|
289
|
+
var environment = useRelayEnvironment();
|
290
|
+
|
291
|
+
var _useState = useState(function () {
|
292
|
+
return getFragmentState(environment, fragmentSelector);
|
293
|
+
}),
|
294
|
+
rawState = _useState[0],
|
295
|
+
setState = _useState[1];
|
296
|
+
|
297
|
+
var _useState2 = useState(fragmentSelector),
|
298
|
+
previousFragmentSelector = _useState2[0],
|
299
|
+
setPreviousFragmentSelector = _useState2[1];
|
300
|
+
|
301
|
+
if (!areEqualSelectors(fragmentSelector, previousFragmentSelector)) {
|
302
|
+
setPreviousFragmentSelector(fragmentSelector);
|
303
|
+
setState(getFragmentState(environment, fragmentSelector));
|
304
|
+
}
|
305
|
+
|
306
|
+
var state;
|
307
|
+
|
308
|
+
if (fragmentRef == null) {
|
309
|
+
state = {
|
310
|
+
kind: 'bailout'
|
311
|
+
};
|
312
|
+
} else if (rawState.kind === 'plural' && rawState.snapshots.length === 0) {
|
313
|
+
state = {
|
314
|
+
kind: 'bailout'
|
315
|
+
};
|
316
|
+
} else {
|
317
|
+
state = rawState;
|
318
|
+
} // Handle the queries for any missing client edges; this may suspend.
|
319
|
+
// FIXME handle client edges in parallel.
|
320
|
+
|
321
|
+
|
322
|
+
var missingClientEdges = getMissingClientEdges(state);
|
323
|
+
|
324
|
+
if (missingClientEdges === null || missingClientEdges === void 0 ? void 0 : missingClientEdges.length) {
|
325
|
+
var _iterator5 = (0, _createForOfIteratorHelper2["default"])(missingClientEdges),
|
326
|
+
_step5;
|
327
|
+
|
328
|
+
try {
|
329
|
+
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
330
|
+
var edge = _step5.value;
|
331
|
+
handleMissingClientEdge(environment, fragmentNode, fragmentRef, edge, queryOptions);
|
332
|
+
}
|
333
|
+
} catch (err) {
|
334
|
+
_iterator5.e(err);
|
335
|
+
} finally {
|
336
|
+
_iterator5.f();
|
337
|
+
}
|
338
|
+
}
|
339
|
+
|
340
|
+
if (isMissingData(state)) {
|
341
|
+
// Suspend if an active operation bears on this fragment, either the
|
342
|
+
// fragment's owner or some other mutation etc. that could affect it:
|
343
|
+
!(fragmentSelector != null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'refinement, see invariants above') : invariant(false) : void 0;
|
344
|
+
var fragmentOwner = fragmentSelector.kind === 'PluralReaderSelector' ? fragmentSelector.selectors[0].owner : fragmentSelector.owner;
|
345
|
+
var pendingOperationsResult = getPendingOperationsForFragment(environment, fragmentNode, fragmentOwner);
|
346
|
+
|
347
|
+
if (pendingOperationsResult) {
|
348
|
+
throw pendingOperationsResult.promise;
|
349
|
+
} // Report required fields only if we're not suspending, since that means
|
350
|
+
// they're missing even though we are out of options for possibly fetching them:
|
351
|
+
|
352
|
+
|
353
|
+
reportMissingRequiredFieldsForState(environment, state);
|
354
|
+
} // Subscriptions:
|
355
|
+
|
356
|
+
|
357
|
+
var isMountedRef = useRef(false);
|
358
|
+
var isListeningForUpdatesRef = useRef(true);
|
359
|
+
|
360
|
+
function enableStoreUpdates() {
|
361
|
+
isListeningForUpdatesRef.current = true;
|
362
|
+
handleMissedUpdates(environment, state, setState);
|
363
|
+
}
|
364
|
+
|
365
|
+
function disableStoreUpdates() {
|
366
|
+
isListeningForUpdatesRef.current = false;
|
367
|
+
}
|
368
|
+
|
369
|
+
useEffect(function () {
|
370
|
+
var wasAlreadySubscribed = isMountedRef.current;
|
371
|
+
isMountedRef.current = true;
|
372
|
+
|
373
|
+
if (!wasAlreadySubscribed) {
|
374
|
+
handleMissedUpdates(environment, state, setState);
|
375
|
+
}
|
376
|
+
|
377
|
+
return subscribeToSnapshot(environment, state, setState);
|
378
|
+
}, [environment, state]);
|
379
|
+
var data = useMemo(function () {
|
380
|
+
return state.kind === 'bailout' ? {} : state.kind === 'singular' ? state.snapshot.data : state.snapshots.map(function (s) {
|
381
|
+
return s.data;
|
382
|
+
});
|
383
|
+
}, [state]);
|
384
|
+
|
385
|
+
if (process.env.NODE_ENV !== "production") {
|
386
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
387
|
+
useDebugValue({
|
388
|
+
fragment: fragmentNode.name,
|
389
|
+
data: data
|
390
|
+
});
|
391
|
+
}
|
392
|
+
|
393
|
+
return {
|
394
|
+
data: data,
|
395
|
+
disableStoreUpdates: disableStoreUpdates,
|
396
|
+
enableStoreUpdates: enableStoreUpdates
|
397
|
+
};
|
398
|
+
}
|
399
|
+
|
400
|
+
module.exports = useFragmentInternal_REACT_CACHE;
|