From c64b330032cef887ac5f787ac4a50bb4eeca4e1d Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Wed, 10 Apr 2019 09:55:56 +0100 Subject: [PATCH] Move EventTypes to ReactTypes (#15364) --- packages/events/EventTypes.js | 53 ------------------ .../src/events/DOMEventResponderSystem.js | 16 +++--- packages/react-events/src/Drag.js | 11 ++-- packages/react-events/src/Focus.js | 17 +++--- packages/react-events/src/Hover.js | 21 ++++---- packages/react-events/src/Press.js | 29 ++++++---- packages/react-events/src/Swipe.js | 11 ++-- packages/shared/ReactTypes.js | 54 ++++++++++++++++--- 8 files changed, 109 insertions(+), 103 deletions(-) delete mode 100644 packages/events/EventTypes.js diff --git a/packages/events/EventTypes.js b/packages/events/EventTypes.js deleted file mode 100644 index 6f1f5e3b4a631..0000000000000 --- a/packages/events/EventTypes.js +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - */ - -import type {AnyNativeEvent} from 'events/PluginModuleType'; -import type {ReactEventResponderEventType} from 'shared/ReactTypes'; - -export type ResponderEvent = { - nativeEvent: AnyNativeEvent, - target: Element | Document, - type: string, - passive: boolean, - passiveSupported: boolean, -}; - -export type ResponderDispatchEventOptions = { - capture?: boolean, - discrete?: boolean, - stopPropagation?: boolean, -}; - -export type ResponderContext = { - dispatchEvent: ( - eventObject: Object, - otpions: ResponderDispatchEventOptions, - ) => void, - isTargetWithinElement: ( - childTarget: Element | Document, - parentTarget: Element | Document, - ) => boolean, - isTargetWithinEventComponent: (Element | Document) => boolean, - isPositionWithinTouchHitTarget: ( - doc: Document, - x: number, - y: number, - ) => boolean, - addRootEventTypes: ( - document: Document, - rootEventTypes: Array, - ) => void, - removeRootEventTypes: ( - rootEventTypes: Array, - ) => void, - hasOwnership: () => boolean, - requestOwnership: () => boolean, - releaseOwnership: () => boolean, - setTimeout: (func: () => void, timeout: number) => TimeoutID, -}; diff --git a/packages/react-dom/src/events/DOMEventResponderSystem.js b/packages/react-dom/src/events/DOMEventResponderSystem.js index 8b61c69d37426..ddadb18472328 100644 --- a/packages/react-dom/src/events/DOMEventResponderSystem.js +++ b/packages/react-dom/src/events/DOMEventResponderSystem.js @@ -6,11 +6,6 @@ * @flow */ -import type { - ResponderContext, - ResponderEvent, - ResponderDispatchEventOptions, -} from 'events/EventTypes'; import { type EventSystemFlags, IS_PASSIVE, @@ -24,6 +19,9 @@ import { import type { ReactEventResponderEventType, ReactEventComponentInstance, + ReactResponderContext, + ReactResponderEvent, + ReactResponderDispatchEventOptions, } from 'shared/ReactTypes'; import type {DOMTopLevelEventType} from 'events/TopLevelEventTypes'; import {batchedUpdates, interactiveUpdates} from 'events/ReactGenericBatching'; @@ -58,10 +56,10 @@ let currentOwner = null; let currentInstance: ReactEventComponentInstance; let currentEventQueue: EventQueue; -const eventResponderContext: ResponderContext = { +const eventResponderContext: ReactResponderContext = { dispatchEvent( possibleEventObject: Object, - {capture, discrete, stopPropagation}: ResponderDispatchEventOptions, + {capture, discrete, stopPropagation}: ReactResponderDispatchEventOptions, ): void { const eventQueue = currentEventQueue; const {listener, target, type} = possibleEventObject; @@ -260,7 +258,7 @@ function createResponderEvent( nativeEvent: AnyNativeEvent, nativeEventTarget: Element | Document, eventSystemFlags: EventSystemFlags, -): ResponderEvent { +): ReactResponderEvent { return { nativeEvent: nativeEvent, target: nativeEventTarget, @@ -342,7 +340,7 @@ function getTargetEventTypes( function handleTopLevelType( topLevelType: DOMTopLevelEventType, - responderEvent: ResponderEvent, + responderEvent: ReactResponderEvent, eventComponentInstance: ReactEventComponentInstance, isRootLevelEvent: boolean, ): void { diff --git a/packages/react-events/src/Drag.js b/packages/react-events/src/Drag.js index 86cb120af2667..80e1be4966d50 100644 --- a/packages/react-events/src/Drag.js +++ b/packages/react-events/src/Drag.js @@ -7,7 +7,10 @@ * @flow */ -import type {ResponderEvent, ResponderContext} from 'events/EventTypes'; +import type { + ReactResponderEvent, + ReactResponderContext, +} from 'shared/ReactTypes'; import {REACT_EVENT_COMPONENT_TYPE} from 'shared/ReactSymbols'; const targetEventTypes = ['pointerdown', 'pointercancel']; @@ -62,7 +65,7 @@ function createDragEvent( } function dispatchDragEvent( - context: ResponderContext, + context: ReactResponderContext, name: DragEventType, listener: DragEvent => void, state: DragState, @@ -88,8 +91,8 @@ const DragResponder = { }; }, onEvent( - event: ResponderEvent, - context: ResponderContext, + event: ReactResponderEvent, + context: ReactResponderContext, props: Object, state: DragState, ): void { diff --git a/packages/react-events/src/Focus.js b/packages/react-events/src/Focus.js index 30ff7102ba4cc..19f555fe74ff2 100644 --- a/packages/react-events/src/Focus.js +++ b/packages/react-events/src/Focus.js @@ -7,7 +7,10 @@ * @flow */ -import type {ResponderEvent, ResponderContext} from 'events/EventTypes'; +import type { + ReactResponderEvent, + ReactResponderContext, +} from 'shared/ReactTypes'; import {REACT_EVENT_COMPONENT_TYPE} from 'shared/ReactSymbols'; type FocusProps = { @@ -47,8 +50,8 @@ function createFocusEvent( } function dispatchFocusInEvents( - event: ResponderEvent, - context: ResponderContext, + event: ReactResponderEvent, + context: ReactResponderContext, props: FocusProps, ) { const {nativeEvent, target} = event; @@ -69,8 +72,8 @@ function dispatchFocusInEvents( } function dispatchFocusOutEvents( - event: ResponderEvent, - context: ResponderContext, + event: ReactResponderEvent, + context: ReactResponderContext, props: FocusProps, ) { const {nativeEvent, target} = event; @@ -98,8 +101,8 @@ const FocusResponder = { }; }, onEvent( - event: ResponderEvent, - context: ResponderContext, + event: ReactResponderEvent, + context: ReactResponderContext, props: Object, state: FocusState, ): void { diff --git a/packages/react-events/src/Hover.js b/packages/react-events/src/Hover.js index bc438d9c74ad8..116416bc7e865 100644 --- a/packages/react-events/src/Hover.js +++ b/packages/react-events/src/Hover.js @@ -7,7 +7,10 @@ * @flow */ -import type {ResponderEvent, ResponderContext} from 'events/EventTypes'; +import type { + ReactResponderEvent, + ReactResponderContext, +} from 'shared/ReactTypes'; import {REACT_EVENT_COMPONENT_TYPE} from 'shared/ReactSymbols'; type HoverProps = { @@ -64,8 +67,8 @@ function createHoverEvent( } function dispatchHoverChangeEvent( - event: ResponderEvent, - context: ResponderContext, + event: ReactResponderEvent, + context: ReactResponderContext, props: HoverProps, state: HoverState, ): void { @@ -81,8 +84,8 @@ function dispatchHoverChangeEvent( } function dispatchHoverStartEvents( - event: ResponderEvent, - context: ResponderContext, + event: ReactResponderEvent, + context: ReactResponderContext, props: HoverProps, state: HoverState, ): void { @@ -132,8 +135,8 @@ function dispatchHoverStartEvents( } function dispatchHoverEndEvents( - event: ResponderEvent, - context: ResponderContext, + event: ReactResponderEvent, + context: ReactResponderContext, props: HoverProps, state: HoverState, ) { @@ -199,8 +202,8 @@ const HoverResponder = { }; }, onEvent( - event: ResponderEvent, - context: ResponderContext, + event: ReactResponderEvent, + context: ReactResponderContext, props: HoverProps, state: HoverState, ): void { diff --git a/packages/react-events/src/Press.js b/packages/react-events/src/Press.js index 5aacbbb44b018..b20acf52327ac 100644 --- a/packages/react-events/src/Press.js +++ b/packages/react-events/src/Press.js @@ -7,7 +7,10 @@ * @flow */ -import type {ResponderEvent, ResponderContext} from 'events/EventTypes'; +import type { + ReactResponderEvent, + ReactResponderContext, +} from 'shared/ReactTypes'; import {REACT_EVENT_COMPONENT_TYPE} from 'shared/ReactSymbols'; type PressProps = { @@ -89,7 +92,7 @@ function createPressEvent( } function dispatchEvent( - context: ResponderContext, + context: ReactResponderContext, state: PressState, name: PressEventType, listener: (e: Object) => void, @@ -100,7 +103,7 @@ function dispatchEvent( } function dispatchPressChangeEvent( - context: ResponderContext, + context: ReactResponderContext, props: PressProps, state: PressState, ): void { @@ -111,7 +114,7 @@ function dispatchPressChangeEvent( } function dispatchLongPressChangeEvent( - context: ResponderContext, + context: ReactResponderContext, props: PressProps, state: PressState, ): void { @@ -150,7 +153,7 @@ function deactivate(context, props, state) { } function dispatchPressStartEvents( - context: ResponderContext, + context: ReactResponderContext, props: PressProps, state: PressState, ): void { @@ -212,7 +215,7 @@ function dispatchPressStartEvents( } function dispatchPressEndEvents( - context: ResponderContext, + context: ReactResponderContext, props: PressProps, state: PressState, ): void { @@ -265,7 +268,7 @@ function calculateDelayMS(delay: ?number, min = 0, fallback = 0) { } function unmountResponder( - context: ResponderContext, + context: ReactResponderContext, props: PressProps, state: PressState, ): void { @@ -293,8 +296,8 @@ const PressResponder = { }; }, onEvent( - event: ResponderEvent, - context: ResponderContext, + event: ReactResponderEvent, + context: ReactResponderContext, props: PressProps, state: PressState, ): void { @@ -491,12 +494,16 @@ const PressResponder = { } } }, - onUnmount(context: ResponderContext, props: PressProps, state: PressState) { + onUnmount( + context: ReactResponderContext, + props: PressProps, + state: PressState, + ) { unmountResponder(context, props, state); }, // TODO This method doesn't work as of yet onOwnershipChange( - context: ResponderContext, + context: ReactResponderContext, props: PressProps, state: PressState, ) { diff --git a/packages/react-events/src/Swipe.js b/packages/react-events/src/Swipe.js index 9d199d9fce69e..2b40e79d5a972 100644 --- a/packages/react-events/src/Swipe.js +++ b/packages/react-events/src/Swipe.js @@ -7,7 +7,10 @@ * @flow */ -import type {ResponderEvent, ResponderContext} from 'events/EventTypes'; +import type { + ReactResponderEvent, + ReactResponderContext, +} from 'shared/ReactTypes'; import {REACT_EVENT_COMPONENT_TYPE} from 'shared/ReactSymbols'; const targetEventTypes = ['pointerdown', 'pointercancel']; @@ -52,7 +55,7 @@ function createSwipeEvent( } function dispatchSwipeEvent( - context: ResponderContext, + context: ReactResponderContext, name: SwipeEventType, listener: SwipeEvent => void, state: SwipeState, @@ -92,8 +95,8 @@ const SwipeResponder = { }; }, onEvent( - event: ResponderEvent, - context: ResponderContext, + event: ReactResponderEvent, + context: ReactResponderContext, props: Object, state: SwipeState, ): void { diff --git a/packages/shared/ReactTypes.js b/packages/shared/ReactTypes.js index 22ffdcab6b811..7b5f2d1c66c52 100644 --- a/packages/shared/ReactTypes.js +++ b/packages/shared/ReactTypes.js @@ -7,8 +7,6 @@ * @flow */ -import type {ResponderEvent, ResponderContext} from 'events/EventTypes'; - export type ReactNode = | React$Element | ReactPortal @@ -91,18 +89,18 @@ export type ReactEventResponder = { targetEventTypes: Array, createInitialState?: (props: null | Object) => Object, onEvent: ( - event: ResponderEvent, - context: ResponderContext, + event: ReactResponderEvent, + context: ReactResponderContext, props: null | Object, state: null | Object, ) => void, onUnmount: ( - context: ResponderContext, + context: ReactResponderContext, props: null | Object, state: null | Object, ) => void, onOwnershipChange: ( - context: ResponderContext, + context: ReactResponderContext, props: null | Object, state: null | Object, ) => void, @@ -128,3 +126,47 @@ export type ReactEventTarget = {| displayName?: string, type: Symbol | number, |}; + +type AnyNativeEvent = Event | KeyboardEvent | MouseEvent | Touch; + +export type ReactResponderEvent = { + nativeEvent: AnyNativeEvent, + target: Element | Document, + type: string, + passive: boolean, + passiveSupported: boolean, +}; + +export type ReactResponderDispatchEventOptions = { + capture?: boolean, + discrete?: boolean, + stopPropagation?: boolean, +}; + +export type ReactResponderContext = { + dispatchEvent: ( + eventObject: Object, + otpions: ReactResponderDispatchEventOptions, + ) => void, + isTargetWithinElement: ( + childTarget: Element | Document, + parentTarget: Element | Document, + ) => boolean, + isTargetWithinEventComponent: (Element | Document) => boolean, + isPositionWithinTouchHitTarget: ( + doc: Document, + x: number, + y: number, + ) => boolean, + addRootEventTypes: ( + document: Document, + rootEventTypes: Array, + ) => void, + removeRootEventTypes: ( + rootEventTypes: Array, + ) => void, + hasOwnership: () => boolean, + requestOwnership: () => boolean, + releaseOwnership: () => boolean, + setTimeout: (func: () => void, timeout: number) => TimeoutID, +};