Commit d28e46ac authored by Gerald Monaco's avatar Gerald Monaco Committed by Tim Neutkens

Support Concurrent Mode in Loadable (#9026)

parent b7efb3ff
...@@ -220,7 +220,7 @@ export default async function getBaseWebpackConfig( ...@@ -220,7 +220,7 @@ export default async function getBaseWebpackConfig(
react: { react: {
name: 'commons', name: 'commons',
chunks: 'all', chunks: 'all',
test: /[\\/]node_modules[\\/](react|react-dom|scheduler)[\\/]/, test: /[\\/]node_modules[\\/](react|react-dom|scheduler|use-subscription)[\\/]/,
}, },
}, },
}, },
...@@ -237,7 +237,7 @@ export default async function getBaseWebpackConfig( ...@@ -237,7 +237,7 @@ export default async function getBaseWebpackConfig(
// This regex ignores nested copies of framework libraries so they're // This regex ignores nested copies of framework libraries so they're
// bundled with their issuer. // bundled with their issuer.
// https://github.com/zeit/next.js/pull/9012 // https://github.com/zeit/next.js/pull/9012
test: /(?<!node_modules.*)[\\/]node_modules[\\/](react|react-dom|scheduler|prop-types)[\\/]/, test: /(?<!node_modules.*)[\\/]node_modules[\\/](react|react-dom|scheduler|prop-types|use-subscription)[\\/]/,
priority: 40, priority: 40,
}, },
lib: { lib: {
......
...@@ -22,6 +22,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE ...@@ -22,6 +22,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
// Modified to be compatible with webpack 4 / Next.js // Modified to be compatible with webpack 4 / Next.js
import React from 'react' import React from 'react'
import { useSubscription } from 'use-subscription'
import { LoadableContext } from './loadable-context' import { LoadableContext } from './loadable-context'
const ALL_INITIALIZERS = [] const ALL_INITIALIZERS = []
...@@ -121,13 +122,19 @@ function createLoadableComponent (loadFn, options) { ...@@ -121,13 +122,19 @@ function createLoadableComponent (loadFn, options) {
options options
) )
let res = null let subscription = null
function init () { function init () {
if (!res) { if (!subscription) {
res = loadFn(opts.loader) const sub = new LoadableSubscription(loadFn, opts)
subscription = {
getCurrentValue: sub.getCurrentValue.bind(sub),
subscribe: sub.subscribe.bind(sub),
retry: sub.retry.bind(sub),
promise: sub.promise.bind(sub)
}
} }
return res.promise return subscription.promise()
} }
// Server only // Server only
...@@ -151,113 +158,128 @@ function createLoadableComponent (loadFn, options) { ...@@ -151,113 +158,128 @@ function createLoadableComponent (loadFn, options) {
}) })
} }
return class LoadableComponent extends React.Component { const LoadableComponent = (props, ref) => {
constructor (props) { init()
super(props)
init() const context = React.useContext(LoadableContext)
const state = useSubscription(subscription)
this.state = {
error: res.error, React.useImperativeHandle(ref, () => ({
pastDelay: false, retry: subscription.retry
timedOut: false, }))
loading: res.loading,
loaded: res.loaded
}
}
static preload () { if (context && Array.isArray(opts.modules)) {
return init() opts.modules.forEach(moduleName => {
context(moduleName)
})
} }
static contextType = LoadableContext if (state.loading || state.error) {
// TODO: change it before next major React release return React.createElement(opts.loading, {
// eslint-disable-next-line isLoading: state.loading,
UNSAFE_componentWillMount() { pastDelay: state.pastDelay,
this._mounted = true timedOut: state.timedOut,
this._loadModule() error: state.error,
retry: subscription.retry
})
} else if (state.loaded) {
return opts.render(state.loaded, props)
} else {
return null
} }
}
_loadModule () { LoadableComponent.preload = () => init()
if (this.context && Array.isArray(opts.modules)) { LoadableComponent.displayName = 'LoadableComponent'
opts.modules.forEach(moduleName => {
this.context(moduleName)
})
}
if (!res.loading) { return React.forwardRef(LoadableComponent)
return }
}
class LoadableSubscription {
constructor (loadFn, opts) {
this._loadFn = loadFn
this._opts = opts
this._callbacks = new Set()
this._delay = null
this._timeout = null
this.retry()
}
promise () {
return this._res.promise
}
retry () {
this._clearTimeouts()
this._res = this._loadFn(this._opts.loader)
this._state = {
pastDelay: false,
timedOut: false
}
const { _res: res, _opts: opts } = this
if (res.loading) {
if (typeof opts.delay === 'number') { if (typeof opts.delay === 'number') {
if (opts.delay === 0) { if (opts.delay === 0) {
this.setState({ pastDelay: true }) this._state.pastDelay = true
} else { } else {
this._delay = setTimeout(() => { this._delay = setTimeout(() => {
this.setState({ pastDelay: true }) this._update({
pastDelay: true
})
}, opts.delay) }, opts.delay)
} }
} }
if (typeof opts.timeout === 'number') { if (typeof opts.timeout === 'number') {
this._timeout = setTimeout(() => { this._timeout = setTimeout(() => {
this.setState({ timedOut: true }) this._update({ timedOut: true })
}, opts.timeout) }, opts.timeout)
} }
}
let update = () => { this._res.promise
if (!this._mounted) { .then(() => {
return this._update()
}
this.setState({
error: res.error,
loaded: res.loaded,
loading: res.loading
})
this._clearTimeouts() this._clearTimeouts()
} })
// eslint-disable-next-line handle-callback-err
res.promise .catch(err => {
.then(() => { this._update()
update() this._clearTimeouts()
}) })
// eslint-disable-next-line handle-callback-err this._update({})
.catch(err => { }
update()
})
}
componentWillUnmount () { _update (partial) {
this._mounted = false this._state = {
this._clearTimeouts() ...this._state,
...partial
} }
this._callbacks.forEach(callback => callback())
}
_clearTimeouts () { _clearTimeouts () {
clearTimeout(this._delay) clearTimeout(this._delay)
clearTimeout(this._timeout) clearTimeout(this._timeout)
} }
retry = () => { getCurrentValue () {
this.setState({ error: null, loading: true, timedOut: false }) return {
res = loadFn(opts.loader) ...this._state,
this._loadModule() error: this._res.error,
loaded: this._res.loaded,
loading: this._res.loading
} }
}
render () { subscribe (callback) {
if (this.state.loading || this.state.error) { this._callbacks.add(callback)
return React.createElement(opts.loading, { return () => {
isLoading: this.state.loading, this._callbacks.delete(callback)
pastDelay: this.state.pastDelay,
timedOut: this.state.timedOut,
error: this.state.error,
retry: this.retry
})
} else if (this.state.loaded) {
return opts.render(this.state.loaded, this.props)
} else {
return null
}
} }
} }
} }
......
...@@ -121,6 +121,7 @@ ...@@ -121,6 +121,7 @@
"terser": "4.0.0", "terser": "4.0.0",
"unfetch": "4.1.0", "unfetch": "4.1.0",
"url": "0.11.0", "url": "0.11.0",
"use-subscription": "1.1.1",
"watchpack": "2.0.0-beta.5", "watchpack": "2.0.0-beta.5",
"webpack": "4.39.0", "webpack": "4.39.0",
"webpack-dev-middleware": "3.7.0", "webpack-dev-middleware": "3.7.0",
......
...@@ -14577,6 +14577,11 @@ url@0.11.0, url@^0.11.0: ...@@ -14577,6 +14577,11 @@ url@0.11.0, url@^0.11.0:
punycode "1.3.2" punycode "1.3.2"
querystring "0.2.0" querystring "0.2.0"
use-subscription@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/use-subscription/-/use-subscription-1.1.1.tgz#5509363e9bb152c4fb334151d4dceb943beaa7bb"
integrity sha512-gk4fPTYvNhs6Ia7u8/+K7bM7sZ7O7AMfWtS+zPO8luH+zWuiGgGcrW0hL4MRWZSzXo+4ofNorf87wZwBKz2YdQ==
use@^3.1.0: use@^3.1.0:
version "3.1.1" version "3.1.1"
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f" resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment