Skip to content

Commit 836d1c5

Browse files
authored
ReactNativeBridgeEventPlugin supports lazily-registered event types (facebook#10679)
* ReactNativeBridgeEventPlugin supports lazily-registered event types This accompanies changes on the native side to now specify which event types each view manager supports as part of its view config. Ultimately the goal is to lazily initialize view managers on the native side as well. * Bubbling and direct attributes are optional on view config This should help ease transition for pre-existing JS-only components.
1 parent 3c98b2e commit 836d1c5

File tree

7 files changed

+166
-358
lines changed

7 files changed

+166
-358
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule ReactNativeBridgeEventPlugin
10+
*/
11+
12+
'use strict';
13+
14+
const {
15+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
16+
} = require('ReactNative');
17+
18+
module.exports =
19+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactNativeBridgeEventPlugin;

src/renderers/native/ReactNativeBridgeEventPlugin.js

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,17 @@
1111
*/
1212
'use strict';
1313

14-
var EventPropagators = require('EventPropagators');
15-
var SyntheticEvent = require('SyntheticEvent');
16-
var ReactNativeEventTypes = require('ReactNativeEventTypes');
17-
var invariant = require('fbjs/lib/invariant');
14+
const EventPropagators = require('EventPropagators');
15+
const SyntheticEvent = require('SyntheticEvent');
16+
const invariant = require('fbjs/lib/invariant');
1817

19-
var customBubblingEventTypes = ReactNativeEventTypes.customBubblingEventTypes;
20-
var customDirectEventTypes = ReactNativeEventTypes.customDirectEventTypes;
18+
const customBubblingEventTypes = {};
19+
const customDirectEventTypes = {};
2120

22-
if (__DEV__) {
23-
var warning = require('fbjs/lib/warning');
21+
import type {ReactNativeBaseComponentViewConfig} from 'ReactNativeTypes';
2422

25-
for (var directTypeName in customDirectEventTypes) {
26-
warning(
27-
!customBubblingEventTypes[directTypeName],
28-
'Event cannot be both direct and bubbling: %s',
29-
directTypeName,
30-
);
31-
}
32-
}
33-
34-
var ReactNativeBridgeEventPlugin = {
35-
eventTypes: {...customBubblingEventTypes, ...customDirectEventTypes},
23+
const ReactNativeBridgeEventPlugin = {
24+
eventTypes: {},
3625

3726
/**
3827
* @see {EventPluginHub.extractEvents}
@@ -43,14 +32,14 @@ var ReactNativeBridgeEventPlugin = {
4332
nativeEvent: Event,
4433
nativeEventTarget: Object,
4534
): ?Object {
46-
var bubbleDispatchConfig = customBubblingEventTypes[topLevelType];
47-
var directDispatchConfig = customDirectEventTypes[topLevelType];
35+
const bubbleDispatchConfig = customBubblingEventTypes[topLevelType];
36+
const directDispatchConfig = customDirectEventTypes[topLevelType];
4837
invariant(
4938
bubbleDispatchConfig || directDispatchConfig,
5039
'Unsupported top level event type "%s" dispatched',
5140
topLevelType,
5241
);
53-
var event = SyntheticEvent.getPooled(
42+
const event = SyntheticEvent.getPooled(
5443
bubbleDispatchConfig || directDispatchConfig,
5544
targetInst,
5645
nativeEvent,
@@ -65,6 +54,46 @@ var ReactNativeBridgeEventPlugin = {
6554
}
6655
return event;
6756
},
57+
58+
processEventTypes: function(
59+
viewConfig: ReactNativeBaseComponentViewConfig,
60+
): void {
61+
const {bubblingEventTypes, directEventTypes} = viewConfig;
62+
63+
if (__DEV__) {
64+
if (bubblingEventTypes != null && directEventTypes != null) {
65+
for (const topLevelType in directEventTypes) {
66+
invariant(
67+
bubblingEventTypes[topLevelType] == null,
68+
'Event cannot be both direct and bubbling: %s',
69+
topLevelType,
70+
);
71+
}
72+
}
73+
}
74+
75+
if (bubblingEventTypes != null) {
76+
for (const topLevelType in bubblingEventTypes) {
77+
if (customBubblingEventTypes[topLevelType] == null) {
78+
ReactNativeBridgeEventPlugin.eventTypes[
79+
topLevelType
80+
] = customBubblingEventTypes[topLevelType] =
81+
bubblingEventTypes[topLevelType];
82+
}
83+
}
84+
}
85+
86+
if (directEventTypes != null) {
87+
for (const topLevelType in directEventTypes) {
88+
if (customDirectEventTypes[topLevelType] == null) {
89+
ReactNativeBridgeEventPlugin.eventTypes[
90+
topLevelType
91+
] = customDirectEventTypes[topLevelType] =
92+
directEventTypes[topLevelType];
93+
}
94+
}
95+
}
96+
},
6897
};
6998

7099
module.exports = ReactNativeBridgeEventPlugin;

0 commit comments

Comments
 (0)