-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathReactRelayQueryRenderer.js
308 lines (284 loc) · 8.19 KB
/
ReactRelayQueryRenderer.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
/**
* Copyright (c) 2013-present, 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 ReactRelayQueryRenderer
* @flow
* @format
*/
'use strict';
const React = require('React');
const RelayPropTypes = require('RelayPropTypes');
const areEqual = require('areEqual');
const deepFreeze = require('deepFreeze');
import type {CacheConfig, Disposable} from 'RelayCombinedEnvironmentTypes';
import type {
RelayEnvironmentInterface as ClassicEnvironment,
} from 'RelayEnvironment';
import type {GraphQLTaggedNode} from 'RelayModernGraphQLTag';
import type {
Environment,
OperationSelector,
RelayContext,
Snapshot,
} from 'RelayStoreTypes';
import type {Variables} from 'RelayTypes';
type Props = {
cacheConfig?: ?CacheConfig,
environment: Environment | ClassicEnvironment,
query: ?GraphQLTaggedNode,
render: (readyState: ReadyState) => ?React.Element<*>,
variables: Variables,
};
type ReadyState = {
error: ?Error,
props: ?Object,
retry: ?() => void,
};
type State = {
readyState: ReadyState,
};
/**
* @public
*
* Orchestrates fetching and rendering data for a single view or view hierarchy:
* - Fetches the query/variables using the given network implementation.
* - Normalizes the response(s) to that query, publishing them to the given
* store.
* - Renders the pending/fail/success states with the provided render function.
* - Subscribes for updates to the root data and re-renders with any changes.
*/
class ReactRelayQueryRenderer extends React.Component {
_pendingFetch: ?Disposable;
_relayContext: RelayContext;
_rootSubscription: ?Disposable;
_selectionReference: ?Disposable;
props: Props;
state: State;
constructor(props: Props, context: Object) {
super(props, context);
let {query, variables} = props;
// TODO (#16225453) QueryRenderer works with old and new environment, but
// the flow typing doesn't quite work abstracted.
// $FlowFixMe
const environment: Environment = props.environment;
let operation = null;
if (query) {
const {
createOperationSelector,
getOperation,
} = environment.unstable_internal;
query = getOperation(query);
operation = createOperationSelector(query, variables);
variables = operation.variables;
}
this._pendingFetch = null;
this._relayContext = {
environment,
variables,
};
this._rootSubscription = null;
this._selectionReference = null;
if (query) {
this.state = {
readyState: getDefaultState(),
};
} else {
this.state = {
readyState: {
error: null,
props: {},
retry: null,
},
};
}
if (operation) {
const readyState = this._fetch(operation, props.cacheConfig);
if (readyState) {
this.state = {readyState};
}
}
}
componentWillReceiveProps(nextProps: Props): void {
if (
nextProps.query !== this.props.query ||
nextProps.environment !== this.props.environment ||
!areEqual(nextProps.variables, this.props.variables)
) {
const {query, variables} = nextProps;
// TODO (#16225453) QueryRenderer works with old and new environment, but
// the flow typing doesn't quite work abstracted.
// $FlowFixMe
const environment: Environment = nextProps.environment;
if (query) {
const {
createOperationSelector,
getOperation,
} = environment.unstable_internal;
const operation = createOperationSelector(
getOperation(query),
variables,
);
this._relayContext = {
environment,
variables: operation.variables,
};
const readyState = this._fetch(operation, nextProps.cacheConfig);
this.setState({
readyState: readyState || getDefaultState(),
});
} else {
this._relayContext = {
environment,
variables,
};
this._release();
this.setState({
readyState: {
error: null,
props: {},
retry: null,
},
});
}
}
}
componentWillUnmount(): void {
this._release();
}
shouldComponentUpdate(nextProps: Props, nextState: State): boolean {
return (
nextProps.render !== this.props.render ||
nextState.readyState !== this.state.readyState
);
}
_release(): void {
if (this._pendingFetch) {
this._pendingFetch.dispose();
this._pendingFetch = null;
}
if (this._rootSubscription) {
this._rootSubscription.dispose();
this._rootSubscription = null;
}
if (this._selectionReference) {
this._selectionReference.dispose();
this._selectionReference = null;
}
}
_fetch(operation: OperationSelector, cacheConfig: ?CacheConfig): ?ReadyState {
const {environment} = this._relayContext;
// Immediately retain the results of the new query to prevent relevant data
// from being freed. This is not strictly required if all new data is
// fetched in a single step, but is necessary if the network could attempt
// to incrementally load data (ex: multiple query entries or incrementally
// loading records from disk cache).
const nextReference = environment.retain(operation.root);
let readyState = getDefaultState();
let snapshot: ?Snapshot; // results of the root fragment
let isOnNextCalled = false;
let isFunctionReturned = false;
const onCompleted = () => {
this._pendingFetch = null;
};
const onError = error => {
readyState = {
error,
props: null,
retry: () => {
this._fetch(operation, cacheConfig);
},
};
if (this._selectionReference) {
this._selectionReference.dispose();
}
this._pendingFetch = null;
this._selectionReference = nextReference;
this.setState({readyState});
};
const onNext = () => {
// `onNext` can be called multiple times by network layers that support
// data subscriptions. Wait until the first payload to render `props` and
// subscribe for data updates.
if (snapshot) {
return;
}
snapshot = environment.lookup(operation.fragment);
readyState = {
error: null,
props: snapshot.data,
retry: () => {
this._fetch(operation, cacheConfig);
},
};
if (this._selectionReference) {
this._selectionReference.dispose();
}
this._rootSubscription = environment.subscribe(snapshot, this._onChange);
this._selectionReference = nextReference;
// This line should be called only once.
isOnNextCalled = true;
if (isFunctionReturned) {
this.setState({readyState});
}
};
if (this._pendingFetch) {
this._pendingFetch.dispose();
}
if (this._rootSubscription) {
this._rootSubscription.dispose();
}
const request = environment.streamQuery({
cacheConfig,
onCompleted,
onError,
onNext,
operation,
});
this._pendingFetch = {
dispose() {
request.dispose();
nextReference.dispose();
},
};
isFunctionReturned = true;
return isOnNextCalled ? readyState : null;
}
_onChange = (snapshot: Snapshot): void => {
this.setState({
readyState: {
...this.state.readyState,
props: snapshot.data,
},
});
};
getChildContext(): Object {
return {
relay: this._relayContext,
};
}
render() {
// Note that the root fragment results in `readyState.props` is already
// frozen by the store; this call is to freeze the readyState object and
// error property if set.
if (__DEV__) {
deepFreeze(this.state.readyState);
}
return this.props.render(this.state.readyState);
}
}
ReactRelayQueryRenderer.childContextTypes = {
relay: RelayPropTypes.Relay,
};
function getDefaultState(): ReadyState {
return {
error: null,
props: null,
retry: null,
};
}
module.exports = ReactRelayQueryRenderer;