-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
RelayFragmentSpecResolver.js
331 lines (299 loc) · 8.45 KB
/
RelayFragmentSpecResolver.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
/**
* 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
* @format
*/
'use strict';
const forEachObject = require('forEachObject');
const invariant = require('invariant');
const {areEqualSelectors, getSelectorsFromObject} = require('./RelaySelector');
const {isScalarAndEqual} = require('relay-runtime');
import type {
Environment,
FragmentMap,
RelayContext,
ReaderSelector,
Snapshot,
} from './RelayEnvironmentTypes';
import type {Disposable, Variables} from 'relay-runtime';
import type {
FragmentSpecResolver,
FragmentSpecResults,
Props,
SelectorData,
} from 'relay-runtime';
type Resolvers = {[key: string]: ?(SelectorListResolver | SelectorResolver)};
/**
* An implementation of the `FragmentSpecResolver` interface defined in
* `RelayEnvironmentTypes`.
*
* This utility is implemented as an imperative, stateful API for performance
* reasons: reusing previous resolvers, callback functions, and subscriptions
* all helps to reduce object allocation and thereby decrease GC time.
*
* The `resolve()` function is also lazy and memoized: changes in the store mark
* the resolver as stale and notify the caller, and the actual results are
* recomputed the first time `resolve()` is called.
*/
class RelayFragmentSpecResolver implements FragmentSpecResolver {
_callback: ?() => void;
_context: RelayContext;
_data: Object;
_fragments: FragmentMap;
_props: Props;
_resolvers: Resolvers;
_stale: boolean;
constructor(
context: RelayContext,
fragments: FragmentMap,
props: Props,
callback?: () => void,
) {
this._callback = callback;
this._context = context;
this._data = {};
this._fragments = fragments;
this._props = props;
this._resolvers = {};
this._stale = false;
this.setProps(props);
}
dispose(): void {
forEachObject(this._resolvers, disposeCallback);
}
resolve(): FragmentSpecResults {
if (this._stale) {
// Avoid mapping the object multiple times, which could occur if data for
// multiple keys changes in the same event loop.
const prevData = this._data;
let nextData;
forEachObject(this._resolvers, (resolver, key) => {
const prevItem = prevData[key];
if (resolver) {
const nextItem = resolver.resolve();
if (nextData || nextItem !== prevItem) {
nextData = nextData || {...prevData};
nextData[key] = nextItem;
}
} else {
const prop = this._props[key];
const nextItem = prop !== undefined ? prop : null;
if (nextData || !isScalarAndEqual(nextItem, prevItem)) {
nextData = nextData || {...prevData};
nextData[key] = nextItem;
}
}
});
this._data = nextData || prevData;
this._stale = false;
}
return this._data;
}
setCallback(callback: () => void): void {
this._callback = callback;
}
setProps(props: Props): void {
const selectors = getSelectorsFromObject(
this._context.variables,
this._fragments,
props,
);
forEachObject(selectors, (selector, key) => {
let resolver = this._resolvers[key];
if (selector == null) {
if (resolver != null) {
resolver.dispose();
}
resolver = null;
} else if (Array.isArray(selector)) {
if (resolver == null) {
resolver = new SelectorListResolver(
this._context.environment,
selector,
this._onChange,
);
} else {
invariant(
resolver instanceof SelectorListResolver,
'RelayFragmentSpecResolver: Expected prop `%s` to always be an array.',
key,
);
resolver.setSelectors(selector);
}
} else {
if (resolver == null) {
resolver = new SelectorResolver(
this._context.environment,
selector,
this._onChange,
);
} else {
invariant(
resolver instanceof SelectorResolver,
'RelayFragmentSpecResolver: Expected prop `%s` to always be an object.',
key,
);
resolver.setSelector(selector);
}
}
this._resolvers[key] = resolver;
});
this._props = props;
this._stale = true;
}
setVariables(variables: Variables): void {
forEachObject(this._resolvers, resolver => {
if (resolver) {
resolver.setVariables(variables);
}
});
this._stale = true;
}
_onChange = (): void => {
this._stale = true;
if (typeof this._callback === 'function') {
this._callback();
}
};
isLoading(): boolean {
return false;
}
}
/**
* A resolver for a single Selector.
*/
class SelectorResolver {
_callback: () => void;
_data: ?SelectorData;
_environment: Environment;
_selector: ReaderSelector;
_subscription: ?Disposable;
constructor(
environment: Environment,
selector: ReaderSelector,
callback: () => void,
) {
const snapshot = environment.lookup(selector);
this._callback = callback;
this._data = snapshot.data;
this._environment = environment;
this._selector = selector;
this._subscription = environment.subscribe(snapshot, this._onChange);
}
dispose(): void {
if (this._subscription) {
this._subscription.dispose();
this._subscription = null;
}
}
resolve(): ?Object {
return this._data;
}
setSelector(selector: ReaderSelector): void {
if (
this._subscription != null &&
areEqualSelectors(selector, this._selector)
) {
return;
}
this.dispose();
const snapshot = this._environment.lookup(selector);
this._data = snapshot.data;
this._selector = selector;
this._subscription = this._environment.subscribe(snapshot, this._onChange);
}
setVariables(variables: Variables): void {
// Note: in the classic implementation variables have to be merged because
// they also contain root variables.
const selector = {
...this._selector,
variables: {
...this._selector.variables,
...variables,
},
};
this.setSelector(selector);
}
_onChange = (snapshot: Snapshot): void => {
this._data = snapshot.data;
this._callback();
};
}
/**
* A resolver for an array of Selectors.
*/
class SelectorListResolver {
_callback: () => void;
_data: Array<?SelectorData>;
_environment: Environment;
_resolvers: Array<SelectorResolver>;
_stale: boolean;
constructor(
environment: Environment,
selectors: Array<ReaderSelector>,
callback: () => void,
) {
this._callback = callback;
this._data = [];
this._environment = environment;
this._resolvers = [];
this._stale = true;
this.setSelectors(selectors);
}
dispose(): void {
this._resolvers.forEach(disposeCallback);
}
resolve(): Array<?Object> {
if (this._stale) {
// Avoid mapping the array multiple times, which could occur if data for
// multiple indices changes in the same event loop.
const prevData = this._data;
let nextData;
for (let ii = 0; ii < this._resolvers.length; ii++) {
const prevItem = prevData[ii];
const nextItem = this._resolvers[ii].resolve();
if (nextData || nextItem !== prevItem) {
nextData = nextData || prevData.slice(0, ii);
nextData.push(nextItem);
}
}
this._data = nextData || prevData;
this._stale = false;
}
return this._data;
}
setSelectors(selectors: Array<ReaderSelector>): void {
while (this._resolvers.length > selectors.length) {
const resolver = this._resolvers.pop();
resolver.dispose();
}
for (let ii = 0; ii < selectors.length; ii++) {
if (ii < this._resolvers.length) {
this._resolvers[ii].setSelector(selectors[ii]);
} else {
this._resolvers[ii] = new SelectorResolver(
this._environment,
selectors[ii],
this._onChange,
);
}
}
this._stale = true;
}
setVariables(variables: Variables): void {
this._resolvers.forEach(resolver => resolver.setVariables(variables));
this._stale = true;
}
_onChange = (data: ?Object): void => {
this._stale = true;
this._callback();
};
}
function disposeCallback(disposable: ?Disposable): void {
disposable && disposable.dispose();
}
module.exports = RelayFragmentSpecResolver;