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

66 lines
1.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 {AnyNativeEvent} from 'events/PluginModuleType';
import {
accumulateTwoPhaseDispatches,
accumulateDirectDispatches,
} from 'events/EventPropagators';
import * as ReactNativeViewConfigRegistry from 'ReactNativeViewConfigRegistry';
import SyntheticEvent from 'events/SyntheticEvent';
import invariant from 'fbjs/lib/invariant';
2016-03-25 06:40:52 +08:00
const {
customBubblingEventTypes,
customDirectEventTypes,
eventTypes,
} = ReactNativeViewConfigRegistry;
2016-03-25 06:40:52 +08:00
const ReactNativeBridgeEventPlugin = {
eventTypes: eventTypes,
2016-03-25 06:40:52 +08:00
/**
* @see {EventPluginHub.extractEvents}
*/
extractEvents: function(
topLevelType: string,
targetInst: Object,
nativeEvent: AnyNativeEvent,
2017-03-14 08:05:18 +08:00
nativeEventTarget: Object,
2016-03-25 06:40:52 +08:00
): ?Object {
if (targetInst == null) {
// Probably a node belonging to another renderer's tree.
return null;
}
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
},
2016-03-25 06:40:52 +08:00
};
export default ReactNativeBridgeEventPlugin;