@samitouri / QOS-React / commits / 634b70a78b

Add Flow types for EventPluginHub (#11465)

* Add Flow types for EventPluginHub * add boolean and remove extraneous typing

Dean Brophy committed Nov 14, 2017 at 18:48 UTC 634b70a78b684c24bd8c8254a795ec02ba317d88
6 files changed +32 -15
packages/events/EventPluginHub.js
+26 -10
@@ -3,6 +3,7 @@
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 + * @flow
7 */
8
9 import ReactErrorUtils from 'shared/ReactErrorUtils';
@@ -20,11 +21,16 @@ import {
21 import accumulateInto from './accumulateInto';
22 import forEachAccumulated from './forEachAccumulated';
23
24 +import type {PluginModule} from './PluginModuleType';
25 +import type {ReactSyntheticEvent} from './ReactSyntheticEventType';
26 +import type {Fiber} from 'react-reconciler/src/ReactFiber';
27 +import type {AnyNativeEvent} from './PluginModuleType';
28 +
29 /**
30 * Internal queue of events that have accumulated their dispatches and are
31 * waiting to have their dispatches executed.
32 */
27 -var eventQueue = null;
33 +var eventQueue: ?(Array<ReactSyntheticEvent> | ReactSyntheticEvent) = null;
34
35 /**
36 * Dispatches an event and releases it back into the pool, unless persistent.
@@ -33,7 +39,10 @@ var eventQueue = null;
39 * @param {boolean} simulated If the event is simulated (changes exn behavior)
40 * @private
41 */
36 -var executeDispatchesAndRelease = function(event, simulated) {
42 +var executeDispatchesAndRelease = function(
43 + event: ReactSyntheticEvent,
44 + simulated: boolean,
45 +) {
46 if (event) {
47 executeDispatchesInOrder(event, simulated);
48
@@ -120,7 +129,7 @@ export const injection = {
129 * @param {string} registrationName Name of listener (e.g. `onClick`).
130 * @return {?function} The stored callback.
131 */
123 -export function getListener(inst, registrationName) {
132 +export function getListener(inst: Fiber, registrationName: string) {
133 var listener;
134
135 // TODO: shouldPreventMouseEvent is DOM-specific and definitely should not
@@ -156,15 +165,15 @@ export function getListener(inst, registrationName) {
165 * @internal
166 */
167 export function extractEvents(
159 - topLevelType,
160 - targetInst,
161 - nativeEvent,
162 - nativeEventTarget,
168 + topLevelType: string,
169 + targetInst: Fiber,
170 + nativeEvent: AnyNativeEvent,
171 + nativeEventTarget: EventTarget,
172 ) {
173 var events;
174 for (var i = 0; i < plugins.length; i++) {
175 // Not every plugin in the ordering may be loaded at runtime.
167 - var possiblePlugin = plugins[i];
176 + var possiblePlugin: PluginModule<AnyNativeEvent> = plugins[i];
177 if (possiblePlugin) {
178 var extractedEvents = possiblePlugin.extractEvents(
179 topLevelType,
@@ -187,7 +196,9 @@ export function extractEvents(
196 * @param {*} events An accumulation of synthetic events.
197 * @internal
198 */
190 -export function enqueueEvents(events) {
199 +export function enqueueEvents(
200 + events: Array<ReactSyntheticEvent> | ReactSyntheticEvent,
201 +) {
202 if (events) {
203 eventQueue = accumulateInto(eventQueue, events);
204 }
@@ -198,11 +209,16 @@ export function enqueueEvents(events) {
209 *
210 * @internal
211 */
201 -export function processEventQueue(simulated) {
212 +export function processEventQueue(simulated: boolean) {
213 // Set `eventQueue` to null before processing it so that we can tell if more
214 // events get enqueued while processing.
215 var processingEventQueue = eventQueue;
216 eventQueue = null;
217 +
218 + if (!processingEventQueue) {
219 + return;
220 + }
221 +
222 if (simulated) {
223 forEachAccumulated(
224 processingEventQueue,
packages/events/PluginModuleType.js
+1 -1
@@ -26,6 +26,6 @@ export type PluginModule<NativeEvent> = {
26 targetInst: Fiber,
27 nativeTarget: NativeEvent,
28 nativeEventTarget: EventTarget,
29 - ) => null | ReactSyntheticEvent,
29 + ) => ?ReactSyntheticEvent,
30 tapMoveThreshold?: number,
31 };
packages/events/ReactSyntheticEventType.js
+1
@@ -27,4 +27,5 @@ export type ReactSyntheticEvent = {
27 nativeTarget: Event,
28 nativeEventTarget: EventTarget,
29 ) => ReactSyntheticEvent,
30 + isPersistent: () => boolean,
31 } & SyntheticEvent<>;
packages/events/accumulateInto.js
+1 -1
@@ -23,7 +23,7 @@ import invariant from 'fbjs/lib/invariant';
23 */
24
25 function accumulateInto<T>(
26 - current: ?(T | Array<T>),
26 + current: ?(Array<T> | T),
27 next: T | Array<T>,
28 ): T | Array<T> {
29 invariant(
packages/events/forEachAccumulated.js
+1 -1
@@ -17,7 +17,7 @@
17 * @param {?} [scope] Scope used as `this` in a callback.
18 */
19 function forEachAccumulated<T>(
20 - arr: ?(T | Array<T>),
20 + arr: ?(Array<T> | T),
21 cb: (elem: T) => void,
22 scope: ?any,
23 ) {
packages/react-native-renderer/src/ReactNativeBridgeEventPlugin.js
+2 -2
@@ -8,7 +8,7 @@
8 */
9
10 import type {ReactNativeBaseComponentViewConfig} from './ReactNativeTypes';
11 -
11 +import type {AnyNativeEvent} from 'events/PluginModuleType';
12 import {
13 accumulateTwoPhaseDispatches,
14 accumulateDirectDispatches,
@@ -28,7 +28,7 @@ const ReactNativeBridgeEventPlugin = {
28 extractEvents: function(
29 topLevelType: string,
30 targetInst: Object,
31 - nativeEvent: Event,
31 + nativeEvent: AnyNativeEvent,
32 nativeEventTarget: Object,
33 ): ?Object {
34 const bubbleDispatchConfig = customBubblingEventTypes[topLevelType];