-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathwithReactiveQuery.js
62 lines (53 loc) · 1.51 KB
/
withReactiveQuery.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
import {withTracker} from 'meteor/react-meteor-data';
import {ReactiveVar} from 'meteor/reactive-var';
/**
* Wraps the query and provides reactive data fetching utility
*
* @param handler
* @param config
* @param QueryComponent
*/
export default function withReactiveContainer(handler, config, QueryComponent) {
let subscriptionError = new ReactiveVar();
const { loadOnRefetch = true } = config;
let hasBeenReady = false;
return withTracker((props) => {
const query = handler(props);
const subscriptionHandle = query.subscribe({
onStop(err) {
if (err) {
subscriptionError.set(err);
}
},
onReady() {
subscriptionError.set(null);
}
});
const isReady = subscriptionHandle.ready();
if (!loadOnRefetch && !hasBeenReady && isReady) {
hasBeenReady = true;
}
const data = query.fetch();
const isLoading = loadOnRefetch ? !isReady : !hasBeenReady && !isReady;
return {
grapher: {
isLoading,
data,
error: subscriptionError,
},
query,
config,
props,
}
})(errorTracker(QueryComponent))
}
const errorTracker = withTracker((props) => {
const error = props.grapher.error.get();
return {
...props,
grapher: {
...props.grapher,
error,
}
};
});