Skip to content

Commit a7f9d34

Browse files
author
Billy Kwok
committed
rename variable from 'component'/'Component' to 'asyncModule'/'mod'
1 parent 58ca92b commit a7f9d34

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

src/flowTypes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export type Tools = {
9191
export type Ids = Array<string>
9292

9393
// RUC
94-
export type State = { error?: any, Component?: ?any }
94+
export type State = { error?: any, mod?: ?any }
9595

9696
type Info = { isMount: boolean, isSync: boolean, isServer: boolean }
9797
type OnBefore = Info => void

src/index.js

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const setHasBabelPlugin = () => {
2929
}
3030

3131
export default function universal<Props: Props>(
32-
component: Config | ConfigFunc,
32+
asyncModule: Config | ConfigFunc,
3333
opts: ComponentOptions = {}
3434
) {
3535
const {
@@ -52,7 +52,6 @@ export default function universal<Props: Props>(
5252
/* eslint-disable react/sort-comp */
5353
_mounted: boolean
5454
_asyncOnly: boolean
55-
_component: ?Object
5655

5756
state: State
5857
props: Props
@@ -61,43 +60,43 @@ export default function universal<Props: Props>(
6160

6261
static preload(props: Props, context: Object = {}) {
6362
props = props || {}
64-
const { requireAsync, requireSync } = req(component, options, props)
65-
let Component
63+
const { requireAsync, requireSync } = req(asyncModule, options, props)
64+
let mod
6665

6766
try {
68-
Component = requireSync(props, context)
67+
mod = requireSync(props, context)
6968
}
7069
catch (error) {
7170
return Promise.reject(error)
7271
}
7372

7473
return Promise.resolve()
7574
.then(() => {
76-
if (Component) return Component
75+
if (mod) return mod
7776
return requireAsync(props, context)
7877
})
79-
.then(Component => {
80-
hoist(UniversalComponent, Component, {
78+
.then(mod => {
79+
hoist(UniversalComponent, mod, {
8180
preload: true,
8281
preloadWeak: true
8382
})
84-
return Component
83+
return mod
8584
})
8685
}
8786

8887
static preloadWeak(props: Props, context: Object = {}) {
8988
props = props || {}
90-
const { requireSync } = req(component, options, props)
89+
const { requireSync } = req(asyncModule, options, props)
9190

92-
const Component = requireSync(props, context)
93-
if (Component) {
94-
hoist(UniversalComponent, Component, {
91+
const mod = requireSync(props, context)
92+
if (mod) {
93+
hoist(UniversalComponent, mod, {
9594
preload: true,
9695
preloadWeak: true
9796
})
9897
}
9998

100-
return Component
99+
return mod
101100
}
102101

103102
static contextTypes = {
@@ -114,15 +113,15 @@ export default function universal<Props: Props>(
114113
this._mounted = true
115114

116115
const { addModule, requireSync, requireAsync, asyncOnly } = req(
117-
component,
116+
asyncModule,
118117
options,
119118
this.props
120119
)
121120

122-
let Component
121+
let mod
123122

124123
try {
125-
Component = requireSync(this.props, this.context)
124+
mod = requireSync(this.props, this.context)
126125
}
127126
catch (error) {
128127
return this.update({ error })
@@ -135,9 +134,9 @@ export default function universal<Props: Props>(
135134
this.context.report(chunkName)
136135
}
137136

138-
if (Component || isServer) {
137+
if (mod || isServer) {
139138
this.handleBefore(true, true, isServer)
140-
this.update({ Component }, true, true, isServer)
139+
this.update({ mod }, true, true, isServer)
141140
return
142141
}
143142

@@ -152,56 +151,56 @@ export default function universal<Props: Props>(
152151
componentWillReceiveProps(nextProps: Props) {
153152
if (isDynamic || this._asyncOnly) {
154153
const { requireSync, requireAsync, shouldUpdate } = req(
155-
component,
154+
asyncModule,
156155
options,
157156
nextProps,
158157
this.props
159158
)
160159

161160
if (shouldUpdate(nextProps, this.props)) {
162-
let Component
161+
let mod
163162

164163
try {
165-
Component = requireSync(nextProps, this.context)
164+
mod = requireSync(nextProps, this.context)
166165
}
167166
catch (error) {
168167
return this.update({ error })
169168
}
170169

171-
this.handleBefore(false, !!Component)
170+
this.handleBefore(false, !!mod)
172171

173-
if (!Component) {
172+
if (!mod) {
174173
return this.requireAsync(requireAsync, nextProps)
175174
}
176175

177-
const state = { Component }
176+
const state = { mod }
178177

179178
if (alwaysDelay) {
180-
if (loadingTransition) this.update({ Component: null }) // display `loading` during componentWillReceiveProps
179+
if (loadingTransition) this.update({ mod: null }) // display `loading` during componentWillReceiveProps
181180
setTimeout(() => this.update(state, false, true), minDelay)
182181
return
183182
}
184183

185184
this.update(state, false, true)
186185
}
187186
else if (isHMR()) {
188-
const Component = requireSync(nextProps, this.context)
189-
this.setState({ Component: () => null }) // HMR /w Redux and HOCs can be finicky, so we
190-
setTimeout(() => this.setState({ Component })) // toggle components to insure updates occur
187+
const mod = requireSync(nextProps, this.context)
188+
this.setState({ mod: () => null }) // HMR /w Redux and HOCs can be finicky, so we
189+
setTimeout(() => this.setState({ mod })) // toggle components to insure updates occur
191190
}
192191
}
193192
}
194193

195194
requireAsync(requireAsync: RequireAsync, props: Props, isMount?: boolean) {
196-
if (this.state.Component && loadingTransition) {
197-
this.update({ Component: null }) // display `loading` during componentWillReceiveProps
195+
if (this.state.mod && loadingTransition) {
196+
this.update({ mod: null }) // display `loading` during componentWillReceiveProps
198197
}
199198

200199
const time = new Date()
201200

202201
requireAsync(props, this.context)
203-
.then((Component: ?any) => {
204-
const state = { Component }
202+
.then((mod: ?any) => {
203+
const state = { mod }
205204

206205
const timeLapsed = new Date() - time
207206
if (timeLapsed < minDelay) {
@@ -244,18 +243,18 @@ export default function universal<Props: Props>(
244243
isSync: boolean,
245244
isServer: boolean
246245
) {
247-
const { Component, error } = state
246+
const { mod, error } = state
248247

249-
if (Component && !error) {
250-
hoist(UniversalComponent, Component, {
248+
if (mod && !error) {
249+
hoist(UniversalComponent, mod, {
251250
preload: true,
252251
preloadWeak: true
253252
})
254253

255254
if (this.props.onAfter) {
256255
const { onAfter } = this.props
257256
const info = { isMount, isSync, isServer }
258-
onAfter(info, Component)
257+
onAfter(info, mod)
259258
}
260259
}
261260
else if (error && this.props.onError) {
@@ -266,7 +265,7 @@ export default function universal<Props: Props>(
266265
}
267266

268267
render() {
269-
const { error, Component } = this.state
268+
const { error, mod } = this.state
270269
const { isLoading, error: userError, ...props } = this.props
271270

272271
// user-provided props (e.g. for data-fetching loading):
@@ -279,9 +278,9 @@ export default function universal<Props: Props>(
279278
else if (error) {
280279
return createElement(Err, { ...props, error })
281280
}
282-
else if (Component) {
281+
else if (mod) {
283282
// primary usage (for async import loading + errors):
284-
return createElement(Component, props)
283+
return createElement(mod, props)
285284
}
286285

287286
return createElement(Loading, props)

0 commit comments

Comments
 (0)