Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support concurrent prepasses #37

Merged
merged 3 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/__tests__/concurrency.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React, {
createContext,
useContext,
useState,
useMemo,
useRef,
useCallback
} from 'react'
import renderPrepass from '..'

const CONCURRENCY = 2

const Context = createContext({ test: 3, promise: null, resolved: false })

function makeApp() {
return <App />
}

function App() {
const [state, setState] = useState(() => ({
test: Math.random(),
promise: null,
resolved: false
}))
const refresh = () =>
setState({ test: Math.random(), promise: null, resolved: false })

return (
<Context.Provider value={{ ...state, refresh }}>
<Outer />
</Context.Provider>
)
}

function Outer() {
useRef({
test: 1
})

const [, refresh] = useSuspenseHook()

useMemo(() => {
return { a: 1, b: 2 }
}, [])

return (
<div>
<button onClick={refresh}>Refresh</button>
<Inner />
</div>
)
}

function useSuspenseHook() {
const context = useContext(Context)

useRef({
test: 1
})

if (!context.resolved && !context.promise) {
context.promise = new Promise(resolve =>
setTimeout(resolve, Math.floor(30 + Math.random() * 50))
).then(() => {
context.resolved = true
context.promise = null
})
}

if (context.promise) throw context.promise

return [true, context.refresh]
}

function Inner() {
const [state] = useState({ a: 3 })

useCallback(() => {
return state
}, [state])

return <h4>Inner</h4>
}

test('concurrency', () => {
return expect(
Promise.all(
new Array(CONCURRENCY).fill(0).map(() => renderPrepass(makeApp()))
)
).resolves.not.toThrow()
})
1 change: 1 addition & 0 deletions src/internals/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export function renderWithHooks(
props: any,
refOrContext: any
): any {
workInProgressHook = null
let children = Component(props, refOrContext)

// NOTE: Excessive rerenders won't throw but will instead abort rendering
Expand Down