-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: code splitting in ContentPreview (#737)
- Loading branch information
1 parent
25c3868
commit 02ef9f4
Showing
32 changed files
with
749 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// @flow | ||
import * as React from 'react'; | ||
import { retryNumOfTimes } from 'utils/function'; | ||
|
||
type Props = { | ||
loader: () => Promise<any>, | ||
fallback?: React.Element<any>, | ||
errorComponent?: React.ComponentType<any>, | ||
}; | ||
|
||
type State = { | ||
error: ?Error, | ||
}; | ||
|
||
const DEFAULT_NUM_TIMES = 3; | ||
const DEFAULT_INITIAL_DELAY = 500; | ||
const DEFAULT_BACKOFF_FACTOR = 2; | ||
|
||
const NullComponent = () => null; | ||
|
||
const AsyncLoad = (asyncProps: Props = {}) => { | ||
const asyncLoadWithRetry = () => | ||
retryNumOfTimes( | ||
(successCallback, errorCallback) => asyncProps.loader().then(successCallback, errorCallback), | ||
DEFAULT_NUM_TIMES, | ||
DEFAULT_INITIAL_DELAY, | ||
DEFAULT_BACKOFF_FACTOR, | ||
); | ||
const LazyComponent = React.lazy(asyncLoadWithRetry); | ||
|
||
return class extends React.Component<Object, State> { | ||
state = { | ||
error: null, | ||
}; | ||
|
||
static getDerivedStateFromError(error: Error) { | ||
return { | ||
error, | ||
}; | ||
} | ||
|
||
render() { | ||
const { fallback = null, errorComponent: ErrorComponent = NullComponent } = asyncProps; | ||
const { error } = this.state; | ||
if (error) { | ||
return <ErrorComponent error={error} />; | ||
} | ||
|
||
return ( | ||
<React.Suspense fallback={fallback}> | ||
<LazyComponent {...this.props} /> | ||
</React.Suspense> | ||
); | ||
} | ||
}; | ||
}; | ||
|
||
export default AsyncLoad; |
51 changes: 51 additions & 0 deletions
51
src/elements/common/async-load/__tests__/AsyncLoad-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { retryNumOfTimes } from 'utils/function'; | ||
import AsyncLoad from '../AsyncLoad'; | ||
|
||
jest.mock('utils/function', () => ({ | ||
retryNumOfTimes: jest.fn(), | ||
})); | ||
|
||
describe('elements/common/async-load/AsyncLoad', () => { | ||
let defaultProps; | ||
|
||
const getAsyncComponent = (props = defaultProps) => AsyncLoad(props); | ||
|
||
beforeEach(() => { | ||
defaultProps = { | ||
loader: jest.fn(), | ||
}; | ||
}); | ||
|
||
test('should return a react component', () => { | ||
const AsyncComponent = getAsyncComponent(); | ||
expect(AsyncComponent.prototype).toBeInstanceOf(React.Component); | ||
}); | ||
|
||
test('should load the lazy component', () => { | ||
const AsyncComponent = getAsyncComponent(); | ||
const wrapper = shallow(<AsyncComponent foo="bar" />); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
test('should render the error component if there is an error', () => { | ||
const errorComponent = () => <div>ERROR!!</div>; | ||
const AsyncComponent = getAsyncComponent({ | ||
...defaultProps, | ||
errorComponent, | ||
}); | ||
|
||
const wrapper = shallow(<AsyncComponent />); | ||
wrapper.setState({ | ||
error: new Error('foo'), | ||
}); | ||
|
||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
test('should not invoke the loader until component mounted', () => { | ||
const AsyncComponent = getAsyncComponent(); | ||
expect(retryNumOfTimes).not.toHaveBeenCalled(); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
src/elements/common/async-load/__tests__/__snapshots__/AsyncLoad-test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`elements/common/async-load/AsyncLoad should load the lazy component 1`] = ` | ||
<React.Suspense | ||
fallback={null} | ||
> | ||
<React.Lazy | ||
foo="bar" | ||
/> | ||
</React.Suspense> | ||
`; | ||
|
||
exports[`elements/common/async-load/AsyncLoad should render the error component if there is an error 1`] = ` | ||
<errorComponent | ||
error={[Error: foo]} | ||
/> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// @flow | ||
export { default } from './AsyncLoad'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.