Skip to content

Commit

Permalink
Replace ReactFiberErrorLogger injection with static forks
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Nov 30, 2017
1 parent 8cbc16f commit c74f314
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 40 deletions.
3 changes: 0 additions & 3 deletions packages/react-dom/src/client/ReactDOMFB.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import * as ReactFiberTreeReflection from 'react-reconciler/reflection';
import * as ReactInstanceMap from 'shared/ReactInstanceMap';
// TODO: direct imports like some-package/src/* are bad. Fix me.
import * as ReactFiberErrorLogger from 'react-reconciler/src/ReactFiberErrorLogger';
import ReactErrorUtils from 'shared/ReactErrorUtils';
import {addUserTimingListener} from 'shared/ReactFeatureFlags';

Expand All @@ -25,7 +23,6 @@ Object.assign(
// These are real internal dependencies that are trickier to remove:
ReactBrowserEventEmitter,
ReactErrorUtils,
ReactFiberErrorLogger,
ReactFiberTreeReflection,
ReactDOMComponentTree,
ReactInstanceMap,
Expand Down
7 changes: 0 additions & 7 deletions packages/react-native-renderer/src/ReactNativeRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import type {ReactNodeList} from 'shared/ReactTypes';

import './ReactNativeInjection';

// TODO: direct imports like some-package/src/* are bad. Fix me.
import * as ReactFiberErrorLogger from 'react-reconciler/src/ReactFiberErrorLogger';
import * as ReactPortal from 'react-reconciler/src/ReactPortal';
import * as ReactGenericBatching from 'events/ReactGenericBatching';
import TouchHistoryMath from 'events/TouchHistoryMath';
Expand All @@ -22,7 +20,6 @@ import ReactVersion from 'shared/ReactVersion';
// Module provided by RN:
import UIManager from 'UIManager';

import {showDialog} from './ReactNativeFiberErrorDialog';
import NativeMethodsMixin from './NativeMethodsMixin';
import ReactNativeBridgeEventPlugin from './ReactNativeBridgeEventPlugin';
import ReactNativeComponent from './ReactNativeComponent';
Expand All @@ -40,10 +37,6 @@ ReactGenericBatching.injection.injectFiberBatchedUpdates(

const roots = new Map();

// Intercept lifecycle errors and ensure they are shown with the correct stack
// trace within the native redbox component.
ReactFiberErrorLogger.injection.injectDialog(showDialog);

const ReactNativeRenderer: ReactNativeType = {
NativeComponent: ReactNativeComponent,

Expand Down
17 changes: 17 additions & 0 deletions packages/react-reconciler/src/ReactFiberErrorDialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {CapturedError} from './ReactFiberScheduler';

// This module is forked in different environments.
// By default, return `true` to log errors to the console.
// Forks can return `false` if this isn't desirable.
export function showDialog(capturedError: CapturedError): boolean {
return true;
}
24 changes: 1 addition & 23 deletions packages/react-reconciler/src/ReactFiberErrorLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@

import type {CapturedError} from './ReactFiberScheduler';

import invariant from 'fbjs/lib/invariant';

const defaultShowDialog = (capturedError: CapturedError) => true;

let showDialog = defaultShowDialog;
import {showDialog} from './ReactFiberErrorDialog';

export function logCapturedError(capturedError: CapturedError): void {
const logError = showDialog(capturedError);
Expand Down Expand Up @@ -78,21 +74,3 @@ export function logCapturedError(capturedError: CapturedError): void {
console.error(error);
}
}

export const injection = {
/**
* Display custom dialog for lifecycle errors.
* Return false to prevent default behavior of logging to console.error.
*/
injectDialog(fn: (e: CapturedError) => boolean) {
invariant(
showDialog === defaultShowDialog,
'The custom dialog was already injected.',
);
invariant(
typeof fn === 'function',
'Injected showDialog() must be a function.',
);
showDialog = fn;
},
};
17 changes: 17 additions & 0 deletions packages/react-reconciler/src/forks/ReactFiberErrorDialog.www.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {CapturedError} from './ReactFiberScheduler';

// Provided by www
const ReactFiberErrorDialogWWW = require('ReactFiberErrorDialog');

export function showDialog(capturedError: CapturedError): boolean {
return ReactFiberErrorDialogWWW.showDialog(capturedError);
}
7 changes: 0 additions & 7 deletions packages/react-rt-renderer/src/ReactNativeRT.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import type {ReactNodeList} from 'shared/ReactTypes';
import 'InitializeCore';
import './ReactNativeRTEventEmitter';

// TODO: direct imports like some-package/src/* are bad. Fix me.
import * as ReactFiberErrorLogger from 'react-reconciler/src/ReactFiberErrorLogger';
import {showDialog} from 'react-native-renderer/src/ReactNativeFiberErrorDialog';
import * as ReactPortal from 'react-reconciler/src/ReactPortal';
import * as ReactGenericBatching from 'events/ReactGenericBatching';
import ReactVersion from 'shared/ReactVersion';
Expand All @@ -36,10 +33,6 @@ ReactGenericBatching.injection.injectFiberBatchedUpdates(

const roots = new Map();

// Intercept lifecycle errors and ensure they are shown with the correct stack
// trace within the native redbox component.
ReactFiberErrorLogger.injection.injectDialog(showDialog);

const ReactNativeRTFiber: ReactNativeRTType = {
render(element: React$Element<any>, containerTag: any, callback: ?Function) {
let root = roots.get(containerTag);
Expand Down
24 changes: 24 additions & 0 deletions scripts/rollup/forks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const UMD_DEV = bundleTypes.UMD_DEV;
const UMD_PROD = bundleTypes.UMD_PROD;
const FB_DEV = bundleTypes.FB_DEV;
const FB_PROD = bundleTypes.FB_PROD;
const RN_DEV = bundleTypes.RN_DEV;
const RN_PROD = bundleTypes.RN_PROD;

// If you need to replace a file with another file for a specific environment,
// add it to this list with the logic for choosing the right replacement.
Expand Down Expand Up @@ -66,6 +68,28 @@ const forks = Object.freeze({
return null;
}
},

// Different behavior for caught errors.
'react-reconciler/src/ReactFiberErrorDialog': (bundleType, entry) => {
switch (bundleType) {
case FB_DEV:
case FB_PROD:
// Use the www fork which shows an error dialog.
return 'react-reconciler/src/forks/ReactFiberErrorDialog.www.js';
case RN_DEV:
case RN_PROD:
switch (entry) {
case 'react-native-renderer':
case 'react-rt-renderer':
// Use the RN fork which plays well with redbox.
return 'react-reconciler/src/forks/ReactFiberErrorDialog.native.js';
default:
return null;
}
default:
return null;
}
},
});

module.exports = forks;

0 comments on commit c74f314

Please sign in to comment.