-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathindex.js
314 lines (266 loc) · 7.42 KB
/
index.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
// @flow
import React from 'react'
import PropTypes from 'prop-types'
import hoist from 'hoist-non-react-statics'
import { Context } from 'vm'
import req from './requireUniversalModule'
import type {
Config,
ConfigFunc,
ComponentOptions,
RequireAsync,
State,
Props
} from './flowTypes'
import {
DefaultLoading,
DefaultError,
createDefaultRender,
isServer
} from './utils'
import { __update } from './helpers'
export { CHUNK_NAMES, MODULE_IDS } from './requireUniversalModule'
export { default as ReportChunks } from './report-chunks'
let hasBabelPlugin = false
const isHMR = () =>
// $FlowIgnore
module.hot && (module.hot.data || module.hot.status() === 'apply')
export const setHasBabelPlugin = () => {
hasBabelPlugin = true
}
export default function universal<Props: Props>(
asyncModule: Config | ConfigFunc,
opts: ComponentOptions = {}
) {
const {
render: userRender,
loading: Loading = DefaultLoading,
error: Err = DefaultError,
minDelay = 0,
alwaysDelay = false,
testBabelPlugin = false,
loadingTransition = true,
...options
} = opts
const renderFunc = userRender || createDefaultRender(Loading, Err)
const isDynamic = hasBabelPlugin || testBabelPlugin
options.isDynamic = isDynamic
options.usesBabelPlugin = hasBabelPlugin
options.modCache = {}
options.promCache = {}
return class UniversalComponent extends React.Component<void, Props, *> {
/* eslint-disable react/sort-comp */
_initialized: boolean
_asyncOnly: boolean
state: State
props: Props
context: Object
/* eslint-enable react/sort-comp */
static preload(props: Props, context: Object = {}) {
props = props || {}
const { requireAsync, requireSync } = req(asyncModule, options, props)
let mod
try {
mod = requireSync(props, context)
}
catch (error) {
return Promise.reject(error)
}
return Promise.resolve()
.then(() => {
if (mod) return mod
return requireAsync(props, context)
})
.then(mod => {
hoist(UniversalComponent, mod, {
preload: true,
preloadWeak: true
})
return mod
})
}
static preloadWeak(props: Props, context: Object = {}) {
props = props || {}
const { requireSync } = req(asyncModule, options, props)
const mod = requireSync(props, context)
if (mod) {
hoist(UniversalComponent, mod, {
preload: true,
preloadWeak: true
})
}
return mod
}
static contextTypes = {
store: PropTypes.object,
report: PropTypes.func
}
requireAsyncInner(
requireAsync: RequireAsync,
props: Props,
state: State,
context: Context,
isMount?: boolean
) {
if (!state.mod && loadingTransition) {
this.update({ mod: null, props }) // display `loading` during componentWillReceiveProps
}
const time = new Date()
requireAsync(props, context)
.then((mod: ?any) => {
const state = { mod, props, context }
const timeLapsed = new Date() - time
if (timeLapsed < minDelay) {
const extraDelay = minDelay - timeLapsed
return setTimeout(() => this.update(state, isMount), extraDelay)
}
this.update(state, isMount)
})
.catch(error => this.update({ error, props, context }))
}
update = (
state: State,
isMount?: boolean = false,
isSync?: boolean = false,
isServer?: boolean = false
) => {
if (!this._initialized) return
if (!state.error) state.error = null
this.handleAfter(state, isMount, isSync, isServer)
}
handleBefore(
isMount: boolean,
isSync: boolean,
isServer?: boolean = false
) {
if (this.props.onBefore) {
const { onBefore } = this.props
const info = { isMount, isSync, isServer }
onBefore(info)
}
}
handleAfter(
state: State,
isMount: boolean,
isSync: boolean,
isServer: boolean
) {
const { mod, error } = state
if (mod && !error) {
hoist(UniversalComponent, mod, {
preload: true,
preloadWeak: true
})
if (this.props.onAfter) {
const { onAfter } = this.props
const info = { isMount, isSync, isServer }
onAfter(info, mod)
}
}
else if (error && this.props.onError) {
this.props.onError(error)
}
this.setState(state)
}
// $FlowFixMe
init(props, context) {
const { addModule, requireSync, requireAsync, asyncOnly } = req(
asyncModule,
options,
props
)
let mod
try {
mod = requireSync(props, context)
}
catch (error) {
return __update(props, { error, props, context }, this._initialized)
}
this._asyncOnly = asyncOnly
const chunkName = addModule(props) // record the module for SSR flushing :)
if (context.report) {
context.report(chunkName)
}
if (mod || isServer) {
this.handleBefore(true, true, isServer)
return __update(
props,
{ asyncOnly, props, mod, context },
this._initialized,
true,
true,
isServer
)
}
this.handleBefore(true, false)
this.requireAsyncInner(
requireAsync,
props,
{ props, asyncOnly, mod, context },
context,
true
)
return { mod, asyncOnly, context, props }
}
constructor(props: Props, context: {}) {
super(props, context)
this.state = this.init(this.props, this.context)
// $FlowFixMe
this.state.error = null
}
static getDerivedStateFromProps(nextProps, currentState) {
const { requireSync, shouldUpdate } = req(
asyncModule,
options,
nextProps,
currentState.props
)
if (isHMR() && shouldUpdate(currentState.props, nextProps)) {
const mod = requireSync(nextProps, currentState.context)
return { ...currentState, mod }
}
return null
}
componentDidMount() {
this._initialized = true
}
componentDidUpdate(prevProps: Props) {
if (isDynamic || this._asyncOnly) {
const { requireSync, requireAsync, shouldUpdate } = req(
asyncModule,
options,
this.props,
prevProps
)
if (shouldUpdate(this.props, prevProps)) {
let mod
try {
mod = requireSync(this.props, this.context)
}
catch (error) {
return this.update({ error })
}
this.handleBefore(false, !!mod)
if (!mod) {
return this.requireAsyncInner(requireAsync, this.props, { mod })
}
const state = { mod }
if (alwaysDelay) {
if (loadingTransition) this.update({ mod: null }) // display `loading` during componentWillReceiveProps
setTimeout(() => this.update(state, false, true), minDelay)
return
}
this.update(state, false, true)
}
}
}
componentWillUnmount() {
this._initialized = false
}
render() {
const { isLoading, error: userError, ...props } = this.props
const { mod, error } = this.state
return renderFunc(props, mod, isLoading, userError || error)
}
}
}