Skip to content

Commit

Permalink
Consolidate responder implementation logic
Browse files Browse the repository at this point in the history
Reduces the minified size most. Smaller percentage improvement for gzip.
  • Loading branch information
necolas committed Sep 4, 2019
1 parent d04887d commit c8aa125
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 444 deletions.
10 changes: 3 additions & 7 deletions packages/react-dom/src/events/DOMEventResponderSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const eventResponderContext: ReactDOMResponderContext = {
}
}
},
isTargetWithinResponder(target: Element | Document): boolean {
isTargetWithinResponder(target: null | Element | Document): boolean {
validateResponderContext();
if (target != null) {
let fiber = getClosestInstanceFromNode(target);
Expand All @@ -132,7 +132,7 @@ const eventResponderContext: ReactDOMResponderContext = {
}
return false;
},
isTargetWithinResponderScope(target: Element | Document): boolean {
isTargetWithinResponderScope(target: null | Element | Document): boolean {
validateResponderContext();
const componentInstance = ((currentInstance: any): ReactDOMEventResponderInstance);
const responder = componentInstance.responder;
Expand All @@ -155,7 +155,7 @@ const eventResponderContext: ReactDOMResponderContext = {
return false;
},
isTargetWithinNode(
childTarget: Element | Document,
childTarget: null | Element | Document,
parentTarget: Element | Document,
): boolean {
validateResponderContext();
Expand Down Expand Up @@ -380,19 +380,16 @@ function processTimers(

function createDOMResponderEvent(
topLevelType: string,
responderTarget: Element,
nativeEvent: AnyNativeEvent,
nativeEventTarget: Element | Document,
passive: boolean,
passiveSupported: boolean,
): ReactDOMResponderEvent {
const {buttons, pointerType} = (nativeEvent: any);
let eventPointerType = '';
let pointerId = null;

if (pointerType !== undefined) {
eventPointerType = pointerType;
pointerId = (nativeEvent: any).pointerId;
} else if (nativeEvent.key !== undefined) {
eventPointerType = 'keyboard';
} else if (buttons !== undefined) {
Expand All @@ -405,7 +402,6 @@ function createDOMResponderEvent(
nativeEvent: nativeEvent,
passive,
passiveSupported,
pointerId,
pointerType: eventPointerType,
target: nativeEventTarget,
type: topLevelType,
Expand Down
16 changes: 11 additions & 5 deletions packages/react-events/src/dom/Press.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ const pressResponderImpl = {
props: PressProps,
state: PressState,
): void {
const {pointerId, pointerType, type} = event;
const {pointerType, type} = event;

if (props.disabled) {
removeRootEventTypes(context, state);
Expand Down Expand Up @@ -584,7 +584,7 @@ const pressResponderImpl = {
state.pointerType = pointerType;
const pressTarget = (state.pressTarget = context.getResponderNode());
if (isPointerEvent) {
state.activePointerId = pointerId;
state.activePointerId = nativeEvent.pointerId;
} else if (isTouchEvent) {
const touchEvent = getTouchFromPressEvent(nativeEvent);
if (touchEvent === null) {
Expand Down Expand Up @@ -652,7 +652,7 @@ const pressResponderImpl = {
props: PressProps,
state: PressState,
): void {
let {pointerId, pointerType, target, type} = event;
let {pointerType, target, type} = event;

const nativeEvent: any = event.nativeEvent;
const isPressed = state.isPressed;
Expand All @@ -672,7 +672,10 @@ const pressResponderImpl = {
if (previousPointerType !== pointerType) {
return;
}
if (type === 'pointermove' && activePointerId !== pointerId) {
if (
type === 'pointermove' &&
activePointerId !== nativeEvent.pointerId
) {
return;
} else if (type === 'touchmove') {
touchEvent = getTouchById(nativeEvent, activePointerId);
Expand Down Expand Up @@ -733,7 +736,10 @@ const pressResponderImpl = {
const buttons = state.buttons;
let isKeyboardEvent = false;
let touchEvent;
if (type === 'pointerup' && activePointerId !== pointerId) {
if (
type === 'pointerup' &&
activePointerId !== nativeEvent.pointerId
) {
return;
} else if (type === 'touchend') {
touchEvent = getTouchById(nativeEvent, activePointerId);
Expand Down
Loading

0 comments on commit c8aa125

Please sign in to comment.