Skip to content

Commit

Permalink
[react-events] Refine executeUserEventHandler (#16662)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Sep 4, 2019
1 parent c66edb9 commit e86146e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
7 changes: 5 additions & 2 deletions packages/legacy-events/ReactGenericBatching.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
needsStateRestore,
restoreStateIfNeeded,
} from './ReactControlledComponent';

import {enableFlareAPI} from 'shared/ReactFeatureFlags';
import {invokeGuardedCallbackAndCatchFirstError} from 'shared/ReactErrorUtils';

// Used as a way to call batchedUpdates when we don't have a reference to
// the renderer. Such as when we're dispatching events or if third party
Expand Down Expand Up @@ -76,11 +78,12 @@ export function batchedEventUpdates(fn, a, b) {
}

// This is for the React Flare event system
export function executeUserEventHandler(fn: any => void, value: any): any {
export function executeUserEventHandler(fn: any => void, value: any): void {
const previouslyInEventHandler = isInsideEventHandler;
try {
isInsideEventHandler = true;
return fn(value);
const type = typeof value === 'object' && value !== null ? value.type : '';
invokeGuardedCallbackAndCatchFirstError(type, fn, undefined, value);
} finally {
isInsideEventHandler = previouslyInEventHandler;
}
Expand Down
13 changes: 8 additions & 5 deletions packages/react-dom/src/events/DOMEventResponderSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,30 @@ const eventResponderContext: ReactDOMResponderContext = {
eventValue: any,
eventListener: any => void,
eventPriority: EventPriority,
): any {
): void {
validateResponderContext();
validateEventValue(eventValue);
switch (eventPriority) {
case DiscreteEvent: {
flushDiscreteUpdatesIfNeeded(currentTimeStamp);
return discreteUpdates(() =>
discreteUpdates(() =>
executeUserEventHandler(eventListener, eventValue),
);
break;
}
case UserBlockingEvent: {
if (enableUserBlockingEvents) {
return runWithPriority(UserBlockingPriority, () =>
runWithPriority(UserBlockingPriority, () =>
executeUserEventHandler(eventListener, eventValue),
);
} else {
return executeUserEventHandler(eventListener, eventValue);
executeUserEventHandler(eventListener, eventValue);
}
break;
}
case ContinuousEvent: {
return executeUserEventHandler(eventListener, eventValue);
executeUserEventHandler(eventListener, eventValue);
break;
}
}
},
Expand Down
10 changes: 5 additions & 5 deletions packages/react-events/src/dom/Keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ function dispatchKeyboardEvent(
type,
defaultPrevented,
);
const shouldPropagate = context.dispatchEvent(
syntheticEvent,
listener,
DiscreteEvent,
);
let shouldPropagate;
const listenerWithReturnValue = e => {
shouldPropagate = listener(e);
};
context.dispatchEvent(syntheticEvent, listenerWithReturnValue, DiscreteEvent);
if (shouldPropagate) {
context.continuePropagation();
}
Expand Down

0 comments on commit e86146e

Please sign in to comment.