Skip to content

Commit fdf0495

Browse files
Dmitry Zakharovfacebook-github-bot
Dmitry Zakharov
authored andcommitted
Make RN Events registered disregarding ViewManager use pattern.
Reviewed By: bvaughn Differential Revision: D6223864 fbshipit-source-id: 322626be193e5fa840d3e5477e86adaa90f2726d
1 parent 3f1b021 commit fdf0495

File tree

3 files changed

+28
-55
lines changed

3 files changed

+28
-55
lines changed

Diff for: Libraries/ReactNative/requireNativeComponent.js

+12-26
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
'use strict';
1414

15+
const Platform = require('Platform');
1516
const ReactNativeBridgeEventPlugin = require('ReactNativeBridgeEventPlugin');
1617
const ReactNativeStyleAttributes = require('ReactNativeStyleAttributes');
1718
const UIManager = require('UIManager');
@@ -47,35 +48,18 @@ const warning = require('fbjs/lib/warning');
4748
*/
4849
import type {ComponentInterface} from 'verifyPropTypes';
4950

51+
let hasAttachedDefaultEventTypes: boolean = false;
52+
5053
function requireNativeComponent(
5154
viewName: string,
5255
componentInterface?: ?ComponentInterface,
5356
extraConfig?: ?{nativeOnly?: Object},
5457
): React$ComponentType<any> | string {
55-
function attachBubblingEventTypes(viewConfig) {
56-
if (UIManager.genericBubblingEventTypes) {
57-
viewConfig.bubblingEventTypes = merge(
58-
viewConfig.bubblingEventTypes,
59-
UIManager.genericBubblingEventTypes,
60-
);
61-
// As genericBubblingEventTypes do not change over time, and there's
62-
// merge of all the events happening in Fiber, we need to pass
63-
// genericBubblingEventTypes to Fiber only once. Therefore, we can delete
64-
// it and forget about it.
65-
delete UIManager.genericBubblingEventTypes;
66-
}
67-
}
68-
69-
function attachDirectEventTypes(viewConfig) {
70-
if (UIManager.genericDirectEventTypes) {
71-
viewConfig.directEventTypes = merge(
72-
viewConfig.directEventTypes,
73-
UIManager.genericDirectEventTypes,
74-
);
75-
// As genericDirectEventTypes do not change over time, and there's merge
76-
// of all the events happening in Fiber, we need to pass genericDirectEventTypes
77-
// to Fiber only once. Therefore, we can delete it and forget about it.
78-
delete UIManager.genericDirectEventTypes;
58+
function attachDefaultEventTypes(viewConfig) {
59+
if (Platform.OS === 'android') {
60+
// This is supported on Android platform only,
61+
// as lazy view managers discovery is Android-specific.
62+
viewConfig = merge(viewConfig, UIManager.getDefaultEventTypes());
7963
}
8064
}
8165

@@ -184,8 +168,10 @@ function requireNativeComponent(
184168
);
185169
}
186170

187-
attachBubblingEventTypes(viewConfig);
188-
attachDirectEventTypes(viewConfig);
171+
if (!hasAttachedDefaultEventTypes) {
172+
attachDefaultEventTypes(viewConfig);
173+
hasAttachedDefaultEventTypes = true;
174+
}
189175

190176
// Register this view's event types with the ReactNative renderer.
191177
// This enables view managers to be initialized lazily, improving perf,

Diff for: ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java

+7-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.facebook.common.logging.FLog;
1818
import com.facebook.debug.holder.PrinterHolder;
1919
import com.facebook.debug.tags.ReactDebugOverlayTags;
20-
import com.facebook.proguard.annotations.DoNotStrip;
2120
import com.facebook.react.animation.Animation;
2221
import com.facebook.react.bridge.Arguments;
2322
import com.facebook.react.bridge.Callback;
@@ -117,11 +116,6 @@ public interface CustomEventNamesResolver {
117116

118117
private int mBatchId = 0;
119118

120-
// Defines if events were already exported to JS. We do not send them more
121-
// than once as they are stored and mixed in with Fiber for every ViewManager
122-
// on JS side.
123-
private boolean mEventsWereSentToJS = false;
124-
125119
public UIManagerModule(
126120
ReactApplicationContext reactContext,
127121
ViewManagerResolver viewManagerResolver,
@@ -235,7 +229,6 @@ private static Map<String, Object> createConstants(
235229
}
236230
}
237231

238-
@DoNotStrip
239232
@ReactMethod(isBlockingSynchronousMethod = true)
240233
public @Nullable WritableMap getConstantsForViewManager(final String viewManagerName) {
241234
ViewManager targetView =
@@ -252,13 +245,8 @@ private static Map<String, Object> createConstants(
252245
try {
253246
Map<String, Object> viewManagerConstants =
254247
UIManagerModuleConstantsHelper.createConstantsForViewManager(
255-
targetView,
256-
mEventsWereSentToJS ? null : UIManagerModuleConstants.getBubblingEventTypeConstants(),
257-
mEventsWereSentToJS ? null : UIManagerModuleConstants.getDirectEventTypeConstants(),
258-
null,
259-
mCustomDirectEvents);
248+
targetView, null, mCustomDirectEvents);
260249
if (viewManagerConstants != null) {
261-
mEventsWereSentToJS = true;
262250
return Arguments.makeNativeMap(viewManagerConstants);
263251
}
264252
return null;
@@ -267,9 +255,12 @@ private static Map<String, Object> createConstants(
267255
}
268256
}
269257

270-
/**
271-
* Resolves Direct Event name exposed to JS from the one known to the Native side.
272-
*/
258+
@ReactMethod(isBlockingSynchronousMethod = true)
259+
public WritableMap getDefaultEventTypes() {
260+
return Arguments.makeNativeMap(UIManagerModuleConstantsHelper.getDefaultExportableEventTypes());
261+
}
262+
263+
/** Resolves Direct Event name exposed to JS from the one known to the Native side. */
273264
public CustomEventNamesResolver getDirectEventNamesResolver() {
274265
return new CustomEventNamesResolver() {
275266
@Override

Diff for: ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstantsHelper.java

+9-13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
/* package */ class UIManagerModuleConstantsHelper {
2626

27+
private static final String BUBBLING_EVENTS_KEY = "bubblingEventTypes";
28+
private static final String DIRECT_EVENTS_KEY = "directEventTypes";
29+
2730
/**
2831
* Generates a lazy discovery enabled version of {@link UIManagerModule} constants. It only
2932
* contains a list of view manager names, so that JS side is aware of the managers there are.
@@ -38,6 +41,12 @@
3841
return constants;
3942
}
4043

44+
/* package */ static Map<String, Object> getDefaultExportableEventTypes() {
45+
return MapBuilder.<String, Object>of(
46+
BUBBLING_EVENTS_KEY, UIManagerModuleConstants.getBubblingEventTypeConstants(),
47+
DIRECT_EVENTS_KEY, UIManagerModuleConstants.getDirectEventTypeConstants());
48+
}
49+
4150
/**
4251
* Generates map of constants that is then exposed by {@link UIManagerModule}.
4352
* Provided list of {@param viewManagers} is then used to populate content of
@@ -84,8 +93,6 @@
8493
try {
8594
Map viewManagerConstants = createConstantsForViewManager(
8695
viewManager,
87-
null,
88-
null,
8996
allBubblingEventTypes,
9097
allDirectEventTypes);
9198
if (!viewManagerConstants.isEmpty()) {
@@ -103,31 +110,20 @@
103110

104111
/* package */ static Map<String, Object> createConstantsForViewManager(
105112
ViewManager viewManager,
106-
@Nullable Map defaultBubblingEvents,
107-
@Nullable Map defaultDirectEvents,
108113
@Nullable Map cumulativeBubblingEventTypes,
109114
@Nullable Map cumulativeDirectEventTypes) {
110-
final String BUBBLING_EVENTS_KEY = "bubblingEventTypes";
111-
final String DIRECT_EVENTS_KEY = "directEventTypes";
112-
113115
Map<String, Object> viewManagerConstants = MapBuilder.newHashMap();
114116

115117
Map viewManagerBubblingEvents = viewManager.getExportedCustomBubblingEventTypeConstants();
116118
if (viewManagerBubblingEvents != null) {
117119
recursiveMerge(cumulativeBubblingEventTypes, viewManagerBubblingEvents);
118-
recursiveMerge(viewManagerBubblingEvents, defaultBubblingEvents);
119120
viewManagerConstants.put(BUBBLING_EVENTS_KEY, viewManagerBubblingEvents);
120-
} else if (defaultBubblingEvents != null) {
121-
viewManagerConstants.put(BUBBLING_EVENTS_KEY, defaultBubblingEvents);
122121
}
123122

124123
Map viewManagerDirectEvents = viewManager.getExportedCustomDirectEventTypeConstants();
125124
if (viewManagerDirectEvents != null) {
126125
recursiveMerge(cumulativeDirectEventTypes, viewManagerDirectEvents);
127-
recursiveMerge(viewManagerDirectEvents, defaultDirectEvents);
128126
viewManagerConstants.put(DIRECT_EVENTS_KEY, viewManagerDirectEvents);
129-
} else if (defaultDirectEvents != null) {
130-
viewManagerConstants.put(DIRECT_EVENTS_KEY, defaultDirectEvents);
131127
}
132128

133129
Map customViewConstants = viewManager.getExportedViewConstants();

0 commit comments

Comments
 (0)