Skip to content

Commit

Permalink
fix(core): Add support for recoverable errors
Browse files Browse the repository at this point in the history
  • Loading branch information
giulioz committed May 10, 2022
1 parent e1ac0af commit 521b573
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion packages/fiber/src/core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ export type ReconcilerRoot<TCanvas extends Element> = {
unmount: () => void
}

const logRecoverableError =
typeof reportError === 'function'
? // In modern browsers, reportError will dispatch an error event,
// emulating an uncaught JavaScript error.
reportError
: (error: any) => {
// In older browsers and test environments, fallback to console.error.
console.error(error)
}

type CreateContainerTypeFix = (
containerInfo: Parameters<typeof reconciler.createContainer>[0],
tag: Parameters<typeof reconciler.createContainer>[1],
hydrationCallbacks: Parameters<typeof reconciler.createContainer>[3],
isStrictMode: boolean,
concurrentUpdatesByDefaultOverride: any,
identifierPrefi: any,
onRecoverableError: any,
transitionCallbacks: any,
) => ReturnType<typeof reconciler.createContainer>

function createRoot<TCanvas extends Element>(canvas: TCanvas): ReconcilerRoot<TCanvas> {
// Check against mistaken use of createRoot
let prevRoot = roots.get(canvas)
Expand All @@ -131,7 +152,18 @@ function createRoot<TCanvas extends Element>(canvas: TCanvas): ReconcilerRoot<TC
// Create store
const store = prevStore || createStore(invalidate, advance)
// Create renderer
const fiber = prevFiber || reconciler.createContainer(store, ConcurrentRoot, false, null)
const fiber =
prevFiber ||
(reconciler.createContainer as unknown as CreateContainerTypeFix)(
store,
ConcurrentRoot,
null,
false,
null,
null,
logRecoverableError,
null,
)
// Map it
if (!prevRoot) roots.set(canvas, { fiber, store })

Expand Down

0 comments on commit 521b573

Please sign in to comment.