/
RouterHookContext.js
158 lines (144 loc) · 4.12 KB
/
RouterHookContext.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
import React from 'react';
import PropTypes from 'prop-types';
import EventEmitter from 'eventemitter3';
import { ComponentStatus, routerHookPropName } from './constants';
import getAllComponents from './getAllComponents';
import { componentsShape, locationShape, routerHookContextShape } from './PropTypes';
const CHANGE_LOADING_STATE = 'changeLoadingState';
const canUseDOM = !!(
typeof window !== 'undefined' &&
window.document &&
window.document.createElement
);
const noop = () => null;
export default class RouterHookContext extends React.Component {
static propTypes = {
children: PropTypes.node.isRequired,
components: PropTypes.arrayOf(componentsShape).isRequired,
location: locationShape.isRequired,
onAborted: PropTypes.func.isRequired,
onCompleted: PropTypes.func.isRequired,
onError: PropTypes.func.isRequired,
onStarted: PropTypes.func.isRequired,
};
static childContextTypes = {
routerHookContext: routerHookContextShape,
};
constructor(props) {
super(props);
this.componentStatuses = {};
this.setComponentStatus = this.setComponentStatus.bind(this);
this.getComponentStatus = this.getComponentStatus.bind(this);
this.addLoadingListener = this.addLoadingListener.bind(this);
this.updateRouterLoading = this.updateRouterLoading.bind(this);
this.loading = false;
}
getChildContext() {
return {
routerHookContext: {
getComponentStatus: this.getComponentStatus,
setComponentStatus: this.setComponentStatus,
addLoadingListener: this.addLoadingListener,
},
};
}
componentDidMount() {
this.props.onStarted();
}
componentWillReceiveProps(nextProps) {
if (nextProps.location === this.props.location) {
return;
}
this.componentStatuses = {};
if (this.loading) {
this.props.onAborted();
}
}
componentWillUnmount() {
if (this.routerEventEmitter) {
this.routerEventEmitter.removeAllListeners(CHANGE_LOADING_STATE);
this.routerEventEmitter = null;
}
}
setComponentStatus(Component, status, err) {
const routerHooks = Component[routerHookPropName];
if (!routerHooks) {
return;
}
this.componentStatuses[routerHooks.id] = status;
if (err) {
this.props.onError({ Component, error: err });
this.componentStatuses[routerHooks.id] = ComponentStatus.DONE;
}
this.updateRouterLoading();
}
getComponentStatus(Component) {
const routerHooks = Component[routerHookPropName];
if (!routerHooks) {
return null;
}
return this.componentStatuses[routerHooks.id];
}
addLoadingListener(listener) {
if (!canUseDOM) {
return noop;
}
if (!this.routerEventEmitter) {
this.routerEventEmitter = new EventEmitter();
}
this.routerEventEmitter.on(CHANGE_LOADING_STATE, listener);
return () => {
this.routerEventEmitter.removeListener(CHANGE_LOADING_STATE, listener);
};
}
updateRouterLoading() {
if (!canUseDOM) {
return;
}
const components = getAllComponents(this.props.components);
let total = 0;
let init = 0;
let defer = 0;
let done = 0;
for (let i = 0, len = components.length; i < len; i += 1) {
const hookId = components[i][routerHookPropName].id;
const status = this.componentStatuses[hookId];
if (status) {
total += 1;
switch (status) {
case ComponentStatus.INIT:
init += 1;
break;
case ComponentStatus.DEFER:
defer += 1;
break;
case ComponentStatus.DONE:
done += 1;
break;
default:
throw new Error(`Unknown status ${status}`);
}
}
}
const loading = done < total;
if (this.routerEventEmitter) {
this.routerEventEmitter.emit(CHANGE_LOADING_STATE, loading, {
total,
init,
defer,
done,
});
}
if (this.loading !== loading) {
this.loading = loading;
if (!loading) {
this.props.onCompleted();
} else {
this.props.onStarted();
}
}
}
render() {
return this.props.children;
}
}