Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[react-events] Refine executeUserEventHandler #16662

Merged
merged 2 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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