-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
createSuspenseFragmentContainer.js
285 lines (255 loc) · 8.94 KB
/
createSuspenseFragmentContainer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
'use strict';
const React = require('React');
const ReactRelayContext = require('react-relay/modern/ReactRelayContext');
const areEqual = require('areEqual');
const assertFragmentMap = require('react-relay/modern/assertFragmentMap');
const forEachObject = require('forEachObject');
const getRelayProp = require('../helpers/getRelayProp');
const invariant = require('invariant');
const mapObject = require('mapObject');
const readContext = require('react-relay/modern/readContext');
const {DataResourceContext} = require('./DataResource');
const {getFragment, getDataIDsFromObject} = require('relay-runtime');
import type {TDataResourceCache} from './DataResource';
import type {
GeneratedNodeMap,
RelayProp,
$RelayProps,
} from 'react-relay/modern/ReactRelayTypes';
import type {
Disposable,
GraphQLTaggedNode,
OperationSelector,
Snapshot,
RelayContext,
} from 'relay-runtime';
function createSuspenseFragmentContainer<
Props: {},
TComponent: React.ComponentType<Props>,
>(
Component: TComponent,
fragmentSpecInput: GraphQLTaggedNode | GeneratedNodeMap,
): React.ComponentType<
$RelayProps<React.ElementConfig<TComponent>, RelayProp>,
> {
type InternalProps = {|
DataResource: TDataResourceCache,
forwardedRef: React.Ref<TComponent>,
fragmentRefs: {[string]: mixed},
// $FlowFixMe - TODO T36619782
relayContext: RelayContext & {query?: OperationSelector},
|};
type State = {|
mirroredFragmentRefs: {[string]: mixed},
mirroredRelayContext: RelayContext & {query?: OperationSelector},
mustResubscribe: boolean,
|};
const componentName =
// $FlowExpectedError - Supress lint: we actually want to do sketchy null check here
Component.displayName || Component.displayName || 'Unknown';
const containerName = `RelaySuspenseFragmentContainer(${componentName})`;
assertFragmentMap(componentName, fragmentSpecInput);
// $FlowExpectedError - The compiler converts a GraphQLTaggedNode into a GeneratedNodeMap for us
const fragmentSpec: GeneratedNodeMap = (fragmentSpecInput: any);
const fragmentNodes = mapObject(fragmentSpec, getFragment);
class SuspenseFragmentRenderer extends React.Component<InternalProps, State> {
static displayName = containerName;
_dataSubscriptions: Array<Disposable> | null = null;
_renderedSnapshots: {[string]: Snapshot | $ReadOnlyArray<Snapshot>} = {};
static getDerivedStateFromProps(nextProps, prevState) {
return {
mirroredFragmentRefs: nextProps.fragmentRefs,
mirroredRelayContext: nextProps.relayContext,
mustResubscribe:
prevState.mirroredRelayContext !== nextProps.relayContext ||
!areEqual(
getDataIDsFromObject(fragmentNodes, prevState.mirroredFragmentRefs),
getDataIDsFromObject(fragmentNodes, nextProps.fragmentRefs),
),
};
}
constructor(props) {
super(props);
this.state = {
mirroredFragmentRefs: props.fragmentRefs,
mirroredRelayContext: props.relayContext,
mustResubscribe: false,
};
}
componentDidMount() {
// TODO Check if data has changed between render and mount. Schedule another
// update if so
this._unsubscribe();
this._subscribe();
}
componentDidUpdate(prevProps: InternalProps) {
// TODO Check if data has changed between render and update. Schedule another
// update if so
const {mustResubscribe} = this.state;
if (mustResubscribe) {
// eslint-disable-next-line lint/react-state-props-mutation
this.state.mustResubscribe = false;
this._unsubscribe();
this._subscribe();
}
}
componentWillUnmount() {
const {DataResource, fragmentRefs, relayContext} = this.props;
const {variables} = relayContext;
this._unsubscribe();
// We invalidate on unmount because we want to allow a component that is
// remounting in the future to read fresh data from the Relay store
// If we didn't, new mounts of the component would always find the data
// cached in DataResource and not read from the store
DataResource.invalidateFragmentSpec({
fragmentNodes,
fragmentRefs,
variables,
});
}
_handleDataUpdate(fragmentKey, latestSnapshot) {
const {mustResubscribe} = this.state;
if (mustResubscribe) {
// If we're currently reading new fragment data in this render phase and
// going to resubscribe to new data on the next commit phase, we should
// ignore updates from the store for old data.
return;
}
const {DataResource, fragmentRefs, relayContext} = this.props;
const {variables} = relayContext;
const fragmentNode = fragmentNodes[fragmentKey];
invariant(
fragmentNode != null,
'SuspenseFragmentContainer: Expected fragment to be available during update',
);
const fragmentRef = fragmentRefs[fragmentKey];
DataResource.setFragment({
fragmentNode,
fragmentRef,
variables,
snapshot: latestSnapshot,
});
this.forceUpdate();
}
_subscribe() {
const {relayContext} = this.props;
const {environment} = relayContext;
const dataSubscriptions = this._dataSubscriptions ?? [];
forEachObject(this._renderedSnapshots, (snapshot, key) => {
invariant(
snapshot !== null,
'SuspenseFragmentContainer: Expected to have rendered with a snapshot',
);
if (Array.isArray(snapshot)) {
snapshot.forEach(s => {
dataSubscriptions.push(
environment.subscribe(s, latestSnapshot =>
this._handleDataUpdate(key, latestSnapshot),
),
);
});
} else {
dataSubscriptions.push(
environment.subscribe(snapshot, latestSnapshot =>
this._handleDataUpdate(key, latestSnapshot),
),
);
}
});
this._dataSubscriptions = dataSubscriptions;
}
_unsubscribe() {
if (this._dataSubscriptions != null) {
this._dataSubscriptions.map(s => s.dispose());
this._dataSubscriptions = null;
}
}
render() {
const {
DataResource,
forwardedRef,
fragmentRefs,
relayContext,
} = this.props;
const {environment, query, variables} = relayContext;
const readResult = DataResource.readFragmentSpec({
environment,
variables,
fragmentNodes,
fragmentRefs,
parentQuery: query,
});
this._renderedSnapshots = {};
const data = {};
forEachObject(readResult, (result, key) => {
if (result == null) {
data[key] = null;
return;
}
data[key] = result.data;
// WARNING: Keeping instance variables in render can be unsafe; however,
// in this case it is safe because we're ensuring they are only used in the
// commit phase.
this._renderedSnapshots[key] = result.snapshot;
});
return (
<Component
{...fragmentRefs}
{...data}
ref={forwardedRef}
relay={getRelayProp(environment)}
/>
);
}
}
const SuspenseFragmentContainer = (props, ref) => {
const DataResource = readContext(DataResourceContext);
const relayContext = readContext(ReactRelayContext);
invariant(
relayContext != null,
`SuspenseFragmentContainer: ${containerName} tried to render with ` +
`missing context. This means that ${containerName} was not rendered ` +
'as a descendant of a QueryRenderer.',
);
if (__DEV__) {
const {isRelayModernEnvironment} = require('relay-runtime');
if (!isRelayModernEnvironment(relayContext.environment)) {
throw new Error(
'SuspenseFragmentContainer: Can only use SuspenseFragmentContainer ' +
`${containerName} in a Relay Modern environment!\n` +
'When using Relay Modern and Relay Classic in the same ' +
'application, ensure components use Relay Compat to work in ' +
'both environments.\n' +
'See: http://facebook.github.io/relay/docs/relay-compat.html',
);
}
}
return (
<SuspenseFragmentRenderer
relayContext={relayContext}
DataResource={DataResource}
fragmentRefs={props}
forwardedRef={ref}
/>
);
};
SuspenseFragmentContainer.displayName = containerName;
// $FlowFixMe - TODO T29156721 forwardRef isn't Flow typed yet
const ForwardRefFragmentContainer = React.forwardRef(
SuspenseFragmentContainer,
);
if (__DEV__) {
ForwardRefFragmentContainer.__ComponentClass = Component;
}
return ForwardRefFragmentContainer;
}
module.exports = createSuspenseFragmentContainer;