-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathRelayRootContainer.js
333 lines (310 loc) · 9.35 KB
/
RelayRootContainer.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule RelayRootContainer
* @typechecks
* @flow
*/
'use strict';
var GraphQLFragmentPointer = require('GraphQLFragmentPointer');
var React = require('React');
var RelayDeprecated = require('RelayDeprecated');
import type {RelayQueryConfigSpec} from 'RelayContainer';
var RelayStore = require('RelayStore');
var RelayStoreData = require('RelayStoreData');
var RelayPropTypes = require('RelayPropTypes');
import type {
Abortable,
ComponentFetchState,
ComponentReadyState,
ReadyState,
RelayContainer
} from 'RelayTypes';
var StaticContainer = require('StaticContainer.react');
var getRelayQueries = require('getRelayQueries');
var invariant = require('invariant');
var mapObject = require('mapObject');
type GraphQLFragmentPointers =
{[propName: string]: ?GraphQLFragmentPointer};
type RootContainerProps = {
Component: RelayContainer;
forceFetch?: ?boolean;
onReadyStateChange?: ?(readyState: ReadyState) => void;
refetchRoute?: ?boolean; // TODO: Deprecate, #6247867.
renderFailure?: ?(error: Error, retry: () => void) => ReactElement;
renderFetched?: ?(
data: GraphQLFragmentPointers,
fetchState: ComponentFetchState
) => ReactElement;
renderLoading?: ?() => ReactElement;
route: RelayQueryConfigSpec;
};
type RootContainerState = {
activeComponent: ?RelayContainer;
activeRoute: ?RelayQueryConfigSpec;
error: ?Error;
fragmentPointers: ?GraphQLFragmentPointers;
pendingRequest: ?Abortable;
readyState: ?ComponentReadyState;
fetchState: ComponentFetchState;
};
var {PropTypes} = React;
var storeData = RelayStoreData.getDefaultInstance();
/**
* @public
*
* RelayRootContainer sends requests for data required to render the supplied
* `Component` and `route`. The `Component` must be a container created using
* `Relay.createContainer`.
*
* See the `RelayStore` module for documentation on `onReadyStateChange`.
*
* === Render Callbacks ===
*
* Whenever the RelayRootContainer renders, one of three render callback props
* are invoked depending on whether data is being loaded, can be resolved, or if
* an error is incurred.
*
* ReactDOM.render(
* <RelayRootContainer
* Component={FooComponent}
* route={fooRoute}
* renderLoading={function() {
* return <View>Loading...</View>;
* }}
* renderFetched={function(data) {
* // Must spread `data` into <FooComponent>.
* return <FooComponent {...data} />;
* }}
* renderFailure={function(error) {
* return <View>Error: {error.message}</View>;
* }}
* />,
* ...
* );
*
* If a callback is not supplied, it has a default behavior:
*
* - Without `renderFetched`, `Component` will be rendered with fetched data.
* - Without `renderFailure`, an error will render to null.
* - Without `renderLoading`, the existing view will continue to render. If
* this is the initial mount (with no existing view), renders to null.
*
* In addition, supplying a `renderLoading` that returns undefined has the same
* effect as not supplying the callback. (Usually, an undefined return value is
* an error in React).
*
* === Refs ===
*
* References to elements rendered by any of these callbacks can be obtained by
* using the React `ref` prop. For example:
*
* <FooComponent {...data} ref={handleFooRef} />
*
* function handleFooRef(component) {
* // Invoked when `<FooComponent>` is mounted or unmounted. When mounted,
* // `component` will be the component. When unmounted, `component` will
* // be null.
* }
*
*/
class RelayRootContainer extends React.Component {
mounted: boolean;
props: RootContainerProps;
state: RootContainerState;
constructor(props: RootContainerProps, context: any) {
super(props, context);
this.mounted = true;
this.state = this._runQueries(this.props);
}
getChildContext(): Object {
return {route: this.props.route};
}
/**
* @private
*/
_runQueries(
{Component, forceFetch, refetchRoute, route}: RootContainerProps
): RootContainerState {
var querySet = getRelayQueries(Component, route);
var onReadyStateChange = readyState => {
if (!this.mounted) {
this._handleReadyStateChange({...readyState, mounted: false});
return;
}
var {fragmentPointers, pendingRequest} = this.state;
if (request !== pendingRequest) {
// Ignore (abort) ready state if we have a new pending request.
return;
}
if (readyState.aborted || readyState.done || readyState.error) {
pendingRequest = null;
}
if (readyState.ready && !fragmentPointers) {
fragmentPointers = mapObject(
querySet,
query => query ?
GraphQLFragmentPointer.createForRoot(
storeData.getQueuedStore(),
query
) :
null
);
}
this.setState({
activeComponent: Component,
activeRoute: route,
error: readyState.error,
fragmentPointers,
pendingRequest,
readyState: {...readyState, mounted: true},
fetchState: {
done: readyState.done,
stale: readyState.stale,
},
});
};
if (typeof refetchRoute !== 'undefined') {
RelayDeprecated.warn({
was: 'RelayRootContainer.refetchRoute',
now: 'RelayRootContainer.forceFetch',
});
forceFetch = refetchRoute;
}
var request = forceFetch ?
RelayStore.forceFetch(querySet, onReadyStateChange) :
RelayStore.primeCache(querySet, onReadyStateChange);
return {
activeComponent: null,
activeRoute: null,
error: null,
fragmentPointers: null,
pendingRequest: request,
readyState: null,
fetchState: {
done: false,
stale: false,
},
};
}
/**
* Returns whether or not the view should be updated during the current render
* pass. This is false between invoking `Relay.Store.{primeCache,forceFetch}`
* and the first invocation of the `onReadyStateChange` callback.
*
* @private
*/
_shouldUpdate(): boolean {
return (
this.props.Component === this.state.activeComponent &&
this.props.route === this.state.activeRoute
);
}
/**
* Exposed as the second argument to the `onFailure` prop.
*
* @private
*/
_retry(): void {
invariant(
this.state.error,
'RelayRootContainer: Can only invoke `retry` in a failure state.'
);
this.setState(this._runQueries(this.props));
}
componentWillReceiveProps(nextProps: RootContainerProps): void {
if (nextProps.Component !== this.props.Component ||
nextProps.route !== this.props.route) {
if (this.state.pendingRequest) {
this.state.pendingRequest.abort();
}
this.setState(this._runQueries(nextProps));
}
}
componentDidUpdate(
prevProps: RootContainerProps,
prevState?: RootContainerState
): void {
// `prevState` should exist; the truthy check is for Flow soundness.
var readyState = this.state.readyState;
if (readyState) {
if (!prevState || readyState !== prevState.readyState) {
this._handleReadyStateChange(readyState);
}
}
}
/**
* @private
*/
_handleReadyStateChange(readyState: ReadyState): void {
var onReadyStateChange = this.props.onReadyStateChange;
if (onReadyStateChange) {
onReadyStateChange(readyState);
}
}
componentWillUnmount(): void {
if (this.state.pendingRequest) {
this.state.pendingRequest.abort();
}
this.mounted = false;
}
render(): ?ReactElement {
var children = null;
var shouldUpdate = this._shouldUpdate();
if (shouldUpdate && this.state.error) {
var renderFailure = this.props.renderFailure;
if (renderFailure) {
children = renderFailure(this.state.error, this._retry.bind(this));
}
} else if (shouldUpdate && this.state.fragmentPointers) {
var renderFetched = this.props.renderFetched;
if (renderFetched) {
children = renderFetched({
...this.props.route.params,
...this.state.fragmentPointers,
}, this.state.fetchState);
} else {
var Component = this.props.Component;
children =
<Component
{...this.props.route.params}
{...this.state.fragmentPointers}
/>;
}
} else {
var renderLoading = this.props.renderLoading;
if (renderLoading) {
children = renderLoading();
} else {
children = undefined;
}
if (children === undefined) {
children = null;
shouldUpdate = false;
}
}
return (
<StaticContainer shouldUpdate={shouldUpdate}>
{children}
</StaticContainer>
);
}
}
RelayRootContainer.propTypes = {
Component: RelayPropTypes.Container,
forceFetch: PropTypes.bool,
onReadyStateChange: PropTypes.func,
renderFailure: PropTypes.func,
renderFetched: PropTypes.func,
renderLoading: PropTypes.func,
route: RelayPropTypes.QueryConfig.isRequired,
};
RelayRootContainer.childContextTypes = {
route: RelayPropTypes.QueryConfig.isRequired,
};
module.exports = RelayRootContainer;