-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
237 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Component } from './component'; | ||
import { createElement } from './create-element'; | ||
|
||
// TODO: react warns in dev mode about defaultProps and propTypes not being supported on lazy | ||
// loaded components | ||
|
||
export const sym = Symbol.for('Suspense'); | ||
|
||
export class Suspense extends Component { | ||
constructor(props) { | ||
// TODO: should we add propTypes in DEV mode? | ||
super(props); | ||
|
||
// mark this component as a handler of suspension (thrown Promises) | ||
this[sym] = sym; | ||
|
||
this.state = { | ||
l: false | ||
}; | ||
} | ||
|
||
componentDidCatch(e) { | ||
if (e && typeof e.then === 'function') { | ||
this.setState({ l: true }); | ||
e.then( | ||
() => { | ||
this.setState({ l: false }); | ||
}, | ||
// TODO: what to do in error case?! | ||
// we could store the error to the state and then throw it during render | ||
// should have a look what react does in these cases... | ||
() => { | ||
this.setState({ l: false }); | ||
} | ||
); | ||
} | ||
else { | ||
throw e; | ||
} | ||
} | ||
|
||
render() { | ||
return this.state.l ? this.props.fallback : this.props.children; | ||
} | ||
} | ||
|
||
export function lazy(loader) { | ||
let prom; | ||
let component; | ||
let error; | ||
return function Lazy(props) { | ||
if (!prom) { | ||
prom = loader(); | ||
prom.then( | ||
({ default: c }) => { component = c; }, | ||
e => error = e, | ||
); | ||
} | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!component) { | ||
throw prom; | ||
} | ||
|
||
return createElement(component, props); | ||
}; | ||
} |
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,133 @@ | ||
/*eslint-env browser, mocha */ | ||
import { setupRerender } from 'preact/test-utils'; | ||
import { expect } from 'chai'; | ||
import { createElement as h, render, Component, Suspense, lazy } from '../../src/index'; | ||
import { setupScratch, teardown } from '../_util/helpers'; | ||
|
||
class LazyComp extends Component { | ||
render() { | ||
return <div>Hello Lazy</div>; | ||
} | ||
} | ||
|
||
class CustomSuspense extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { done: false }; | ||
} | ||
render() { | ||
if (!this.state.done) { | ||
throw new Promise((res) => { | ||
setTimeout(() => { | ||
this.setState({ done: true }); | ||
res(); | ||
}, 0); | ||
}); | ||
} | ||
|
||
return ( | ||
<div> | ||
Hello CustomSuspense | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
class Catcher extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { error: null }; | ||
} | ||
|
||
componentDidCatch(e) { | ||
this.setState({ error: e }); | ||
} | ||
|
||
render() { | ||
return this.state.error ? ( | ||
<div> | ||
Catcher did catch: {this.state.error.message} | ||
</div> | ||
) : this.props.children; | ||
} | ||
} | ||
|
||
const Lazy = lazy(() => new Promise((res) => { | ||
setTimeout(() => { | ||
res({ default: LazyComp }); | ||
}, 0); | ||
})); | ||
|
||
/** @jsx h */ | ||
|
||
describe('suspense', () => { | ||
let scratch, rerender; | ||
|
||
beforeEach(() => { | ||
scratch = setupScratch(); | ||
rerender = setupRerender(); | ||
}); | ||
|
||
afterEach(() => { | ||
teardown(scratch); | ||
}); | ||
|
||
it('should suspend when using lazy', () => { | ||
render(<Suspense fallback={<div>Suspended...</div>}> | ||
<Lazy /> | ||
</Suspense>, scratch); | ||
rerender(); | ||
expect(scratch.innerHTML).to.eql( | ||
`<div>Suspended...</div>` | ||
); | ||
}); | ||
|
||
it('should suspend when a promise is throw', () => { | ||
render(<Suspense fallback={<div>Suspended...</div>}> | ||
<CustomSuspense /> | ||
</Suspense>, scratch); | ||
rerender(); | ||
expect(scratch.innerHTML).to.eql( | ||
`<div>Suspended...</div>` | ||
); | ||
}); | ||
|
||
it('should suspend with custom error boundary', () => { | ||
render(<Suspense fallback={<div>Suspended...</div>}> | ||
<Catcher> | ||
<CustomSuspense /> | ||
</Catcher> | ||
</Suspense>, scratch); | ||
rerender(); | ||
expect(scratch.innerHTML).to.eql( | ||
`<div>Suspended...</div>` | ||
); | ||
}); | ||
|
||
it('should only suspend the most inner Suspend', () => { | ||
render(<Suspense fallback={<div>Suspended... 1</div>}> | ||
Not suspended... | ||
<Suspense fallback={<div>Suspended... 2</div>}> | ||
<Catcher> | ||
<CustomSuspense /> | ||
</Catcher> | ||
</Suspense> | ||
</Suspense>, scratch); | ||
rerender(); | ||
expect(scratch.innerHTML).to.eql( | ||
`Not suspended...<div>Suspended... 2</div>` | ||
); | ||
}); | ||
|
||
it('should throw when missing Suspense', () => { | ||
render(<Catcher> | ||
<CustomSuspense /> | ||
</Catcher>, scratch); | ||
rerender(); | ||
expect(scratch.innerHTML).to.eql( | ||
`<div>Catcher did catch: CustomSuspense suspended while rendering, but no fallback UI was specified. | ||
Add a <Suspense fallback=...> component higher in the tree to provide a loading indicator or placeholder to display.</div>` | ||
); | ||
}); | ||
}); |