Skip to content

Commit

Permalink
Remove an extra allocation for open source bundles (#11797)
Browse files Browse the repository at this point in the history
* Remove EventListener fbjs utility

EventListener normalizes event subscription for <= IE8. This is no
longer necessary. element.addEventListener is sufficient.

* Remove an extra allocation for open source bundles

* Split into two functions to avoid extra runtime checks

* Revert unrelated changes
  • Loading branch information
gaearon authored Dec 7, 2017
1 parent 5301c41 commit f93e34e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
24 changes: 24 additions & 0 deletions packages/react-dom/src/events/EventListener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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
*/

export function addEventBubbleListener(
element: Element,
eventType: string,
listener: Function,
): void {
element.addEventListener(eventType, listener, false);
}

export function addEventCaptureListener(
element: Element,
eventType: string,
listener: Function,
): void {
element.addEventListener(eventType, listener, true);
}
6 changes: 3 additions & 3 deletions packages/react-dom/src/events/ReactDOMEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import {batchedUpdates} from 'events/ReactGenericBatching';
import {isFiberMounted} from 'react-reconciler/reflection';
import {HostRoot} from 'shared/ReactTypeOfWork';
import EventListener from 'fbjs/lib/EventListener';

import {addEventBubbleListener, addEventCaptureListener} from './EventListener';
import getEventTarget from './getEventTarget';
import {getClosestInstanceFromNode} from '../client/ReactDOMComponentTree';

Expand Down Expand Up @@ -124,7 +124,7 @@ export function trapBubbledEvent(topLevelType, handlerBaseName, element) {
if (!element) {
return null;
}
return EventListener.listen(
addEventBubbleListener(
element,
handlerBaseName,
dispatchEvent.bind(null, topLevelType),
Expand All @@ -145,7 +145,7 @@ export function trapCapturedEvent(topLevelType, handlerBaseName, element) {
if (!element) {
return null;
}
return EventListener.capture(
addEventCaptureListener(
element,
handlerBaseName,
dispatchEvent.bind(null, topLevelType),
Expand Down
35 changes: 35 additions & 0 deletions packages/react-dom/src/events/forks/EventListener-www.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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
*/

var EventListenerWWW = require('EventListener');

import typeof * as EventListenerType from '../EventListener';
import typeof * as EventListenerShimType from './EventListener-www';

export function addEventBubbleListener(
element: Element,
eventType: string,
listener: Function,
): void {
EventListenerWWW.listen(element, eventType, listener);
}

export function addEventCaptureListener(
element: Element,
eventType: string,
listener: Function,
): void {
EventListenerWWW.capture(element, eventType, listener);
}

// Flow magic to verify the exports of this file match the original version.
// eslint-disable-next-line no-unused-vars
type Check<_X, Y: _X, X: Y = _X> = null;
// eslint-disable-next-line no-unused-expressions
(null: Check<EventListenerShimType, EventListenerType>);
8 changes: 8 additions & 0 deletions scripts/flow/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ declare module 'ReactFiberErrorDialog' {
showErrorDialog: (error: mixed) => boolean,
};
}

// EventListener www fork
declare module 'EventListener' {
declare module.exports: {
listen: (target: Element, type: string, callback: Function) => mixed,
capture: (target: Element, type: string, callback: Function) => mixed,
};
}
12 changes: 12 additions & 0 deletions scripts/rollup/forks.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ const forks = Object.freeze({
return null;
}
},

// We wrap top-level listeners into guards on www.
'react-dom/src/events/EventListener': (bundleType, entry) => {
switch (bundleType) {
case FB_DEV:
case FB_PROD:
// Use the www fork which is integrated with TimeSlice profiling.
return 'react-dom/src/events/forks/EventListener-www.js';
default:
return null;
}
},
});

module.exports = forks;

0 comments on commit f93e34e

Please sign in to comment.