-
-
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
11 changed files
with
700 additions
and
9 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
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,91 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
import { createElement, Component, memo, Fragment, Suspense, lazy } from "react"; | ||
|
||
function LazyComp() { | ||
return <div>I'm (fake) lazy loaded</div>; | ||
} | ||
|
||
const Lazy = lazy(() => Promise.resolve({ default: LazyComp })); | ||
|
||
function createSuspension(name, timeout, error) { | ||
let done = false; | ||
let prom; | ||
|
||
return { | ||
name, | ||
timeout, | ||
start: () => { | ||
if (!prom) { | ||
prom = new Promise((res, rej) => { | ||
setTimeout(() => { | ||
done = true; | ||
if (error) { | ||
rej(error); | ||
} | ||
else { | ||
res(); | ||
} | ||
}, timeout); | ||
}); | ||
} | ||
|
||
return prom; | ||
}, | ||
getPromise: () => prom, | ||
isDone: () => done | ||
}; | ||
} | ||
|
||
function CustomSuspense({ isDone, start, timeout, name }) { | ||
if (!isDone()) { | ||
throw start(); | ||
} | ||
|
||
return ( | ||
<div> | ||
Hello from CustomSuspense {name}, loaded after {timeout / 1000}s | ||
</div> | ||
); | ||
} | ||
|
||
function init() { | ||
return { | ||
s1: createSuspension('1', 1000, null), | ||
s2: createSuspension('2', 2000, null), | ||
s3: createSuspension('3', 3000, null) | ||
}; | ||
} | ||
|
||
export default class DevtoolsDemo extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = init(); | ||
this.onRerun = this.onRerun.bind(this); | ||
} | ||
|
||
onRerun() { | ||
this.setState(init()); | ||
} | ||
|
||
render(props, state) { | ||
return ( | ||
<div> | ||
<h1>lazy()</h1> | ||
<Suspense fallback={<div>Loading (fake) lazy loaded component...</div>}> | ||
<Lazy /> | ||
</Suspense> | ||
<h1>Suspense</h1> | ||
<div> | ||
<button onClick={this.onRerun} >Rerun</button> | ||
</div> | ||
<Suspense fallback={<div>Fallback 1</div>}> | ||
<CustomSuspense {...state.s1} /> | ||
<Suspense fallback={<div>Fallback 2</div>}> | ||
<CustomSuspense {...state.s2} /> | ||
<CustomSuspense {...state.s3} /> | ||
</Suspense> | ||
</Suspense> | ||
</div> | ||
); | ||
} | ||
} |
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,51 @@ | ||
import { Component } from './component'; | ||
import { createElement } from './create-element'; | ||
|
||
// having a "custom class" here saves 50bytes gzipped | ||
export function Suspense(props) {} | ||
Suspense.prototype = new Component(); | ||
|
||
/** | ||
* @param {Promise} promise The thrown promise | ||
*/ | ||
Suspense.prototype._childDidSuspend = function(promise) { | ||
this.setState({ _loading: true }); | ||
const cb = () => { this.setState({ _loading: false }); }; | ||
|
||
// Suspense ignores errors thrown in Promises as this should be handled by user land code | ||
promise.then(cb, cb); | ||
}; | ||
|
||
Suspense.prototype.render = function(props, state) { | ||
return state._loading ? props.fallback : props.children; | ||
}; | ||
|
||
export function lazy(loader) { | ||
let prom; | ||
let component; | ||
let error; | ||
|
||
function Lazy(props) { | ||
if (!prom) { | ||
prom = loader(); | ||
prom.then( | ||
(exports) => { component = exports.default; }, | ||
(e) => { error = e; }, | ||
); | ||
} | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (!component) { | ||
throw prom; | ||
} | ||
|
||
return createElement(component, props); | ||
} | ||
|
||
Lazy.displayName = 'Lazy'; | ||
|
||
return Lazy; | ||
} |
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.