Skip to content

Commit

Permalink
Deprecate injecting custom event plugins (#11690)
Browse files Browse the repository at this point in the history
* Deprecate injecting custom event plugins

* Fix up tests

* Fix CI

* oh noes
  • Loading branch information
gaearon authored Nov 29, 2017
1 parent 8c1c5d7 commit 3e64b18
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/events/EventPluginRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import type {
} from './PluginModuleType';

import invariant from 'fbjs/lib/invariant';
import lowPriorityWarning from 'shared/lowPriorityWarning';

type NamesToPlugins = {[key: PluginName]: PluginModule<AnyNativeEvent>};
type EventPluginOrder = null | Array<PluginName>;

var shouldWarnOnInjection = false;

/**
* Injectable ordering of event plugins.
*/
Expand All @@ -29,6 +32,10 @@ var eventPluginOrder: EventPluginOrder = null;
*/
var namesToPlugins: NamesToPlugins = {};

export function enableWarningOnInjection() {
shouldWarnOnInjection = true;
}

/**
* Recomputes the plugin list using the injected plugins and plugin ordering.
*
Expand Down Expand Up @@ -221,6 +228,21 @@ export function injectEventPluginOrder(
export function injectEventPluginsByName(
injectedNamesToPlugins: NamesToPlugins,
): void {
if (__DEV__) {
if (shouldWarnOnInjection) {
const names = Object.keys(injectedNamesToPlugins).join(', ');
lowPriorityWarning(
false,
'Injecting custom event plugins (%s) is deprecated ' +
'and will not work in React 17+. Please update your code ' +
'to not depend on React internals. The stack trace for this ' +
'warning should reveal the library that is using them. ' +
'See https://github.com/facebook/react/issues/11689 for a discussion.',
names,
);
}
}

var isOrderingDirty = false;
for (var pluginName in injectedNamesToPlugins) {
if (!injectedNamesToPlugins.hasOwnProperty(pluginName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,21 @@ describe('ReactBrowserEventEmitter', () => {

idCallOrder = [];
tapMoveThreshold = TapEventPlugin.tapMoveThreshold;
spyOnDev(console, 'warn');
EventPluginHub.injection.injectEventPluginsByName({
TapEventPlugin: TapEventPlugin,
});
});

afterEach(() => {
if (__DEV__) {
expect(console.warn.calls.count()).toBe(1);
expect(console.warn.calls.argsFor(0)[0]).toContain(
'Injecting custom event plugins (TapEventPlugin) is deprecated',
);
}
});

it('should store a listener correctly', () => {
registerSimpleTestHandler();
var listener = getListener(CHILD, ON_CLICK_KEY);
Expand Down
22 changes: 22 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOM-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,28 @@ describe('ReactDOM', () => {
}
});

// https://github.com/facebook/react/issues/11689
it('should warn when attempting to inject an event plugin', () => {
spyOnDev(console, 'warn');
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.EventPluginHub.injection.injectEventPluginsByName(
{
TapEventPlugin: {
extractEvents() {},
},
},
);
if (__DEV__) {
expect(console.warn.calls.count()).toBe(1);
expect(console.warn.calls.argsFor(0)[0]).toContain(
'Injecting custom event plugins (TapEventPlugin) is deprecated ' +
'and will not work in React 17+. Please update your code ' +
'to not depend on React internals. The stack trace for this ' +
'warning should reveal the library that is using them. ' +
'See https://github.com/facebook/react/issues/11689 for a discussion.',
);
}
});

it('throws in DEV if jsdom is destroyed by the time setState() is called', () => {
spyOnDev(console, 'error');
class App extends React.Component {
Expand Down
7 changes: 7 additions & 0 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,13 @@ const ReactDOM: Object = {
},
};

if (__DEV__) {
// Show deprecation warnings as we don't want to support injection forever.
// We do it now to let the internal injection happen without warnings.
// https://github.com/facebook/react/issues/11689
EventPluginRegistry.enableWarningOnInjection();
}

type RootOptions = {
hydrate?: boolean,
};
Expand Down

0 comments on commit 3e64b18

Please sign in to comment.