react/packages/react-native-renderer/src/ReactNativeBridgeEventPlugi...

99 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-03-25 06:40:52 +08:00
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
2016-03-25 06:40:52 +08:00
*
* @flow
*/
import type {ReactNativeBaseComponentViewConfig} from './ReactNativeTypes';
import {
accumulateTwoPhaseDispatches,
accumulateDirectDispatches,
} from 'events/EventPropagators';
import SyntheticEvent from 'events/SyntheticEvent';
import invariant from 'fbjs/lib/invariant';
2016-03-25 06:40:52 +08:00
const customBubblingEventTypes = {};
const customDirectEventTypes = {};
2016-03-25 06:40:52 +08:00
const ReactNativeBridgeEventPlugin = {
eventTypes: {},
2016-03-25 06:40:52 +08:00
/**
* @see {EventPluginHub.extractEvents}
*/
extractEvents: function(
topLevelType: string,
targetInst: Object,
nativeEvent: Event,
2017-03-14 08:05:18 +08:00
nativeEventTarget: Object,
2016-03-25 06:40:52 +08:00
): ?Object {
const bubbleDispatchConfig = customBubblingEventTypes[topLevelType];
const directDispatchConfig = customDirectEventTypes[topLevelType];
invariant(
bubbleDispatchConfig || directDispatchConfig,
'Unsupported top level event type "%s" dispatched',
topLevelType,
);
const event = SyntheticEvent.getPooled(
2016-03-25 06:40:52 +08:00
bubbleDispatchConfig || directDispatchConfig,
targetInst,
nativeEvent,
2017-03-14 08:05:18 +08:00
nativeEventTarget,
2016-03-25 06:40:52 +08:00
);
if (bubbleDispatchConfig) {
accumulateTwoPhaseDispatches(event);
2016-03-25 06:40:52 +08:00
} else if (directDispatchConfig) {
accumulateDirectDispatches(event);
2016-03-25 06:40:52 +08:00
} else {
return null;
}
return event;
2016-04-19 07:11:31 +08:00
},
processEventTypes: function(
viewConfig: ReactNativeBaseComponentViewConfig,
): void {
const {bubblingEventTypes, directEventTypes} = viewConfig;
if (__DEV__) {
if (bubblingEventTypes != null && directEventTypes != null) {
for (const topLevelType in directEventTypes) {
invariant(
bubblingEventTypes[topLevelType] == null,
'Event cannot be both direct and bubbling: %s',
topLevelType,
);
}
}
}
if (bubblingEventTypes != null) {
for (const topLevelType in bubblingEventTypes) {
if (customBubblingEventTypes[topLevelType] == null) {
ReactNativeBridgeEventPlugin.eventTypes[
topLevelType
] = customBubblingEventTypes[topLevelType] =
bubblingEventTypes[topLevelType];
}
}
}
if (directEventTypes != null) {
for (const topLevelType in directEventTypes) {
if (customDirectEventTypes[topLevelType] == null) {
ReactNativeBridgeEventPlugin.eventTypes[
topLevelType
] = customDirectEventTypes[topLevelType] =
directEventTypes[topLevelType];
}
}
}
},
2016-03-25 06:40:52 +08:00
};
export default ReactNativeBridgeEventPlugin;