Skip to content

Commit 1b71e03

Browse files
Dmitry Zakharovfacebook-github-bot
Dmitry Zakharov
authored andcommitted
Make RN Events registered disregarding ViewManager use pattern.
Reviewed By: fromcelticpark Differential Revision: D6260009 fbshipit-source-id: f3d00162f8473976264ebef040d01163950f2170
1 parent e906525 commit 1b71e03

File tree

3 files changed

+41
-46
lines changed

3 files changed

+41
-46
lines changed

Libraries/ReactNative/requireNativeComponent.js

+24-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,30 @@ 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: any) {
59+
if (Platform.OS === 'android') {
60+
// This is supported on Android platform only,
61+
// as lazy view managers discovery is Android-specific.
62+
if (UIManager.ViewManagerNames) {
63+
// Lazy view managers enabled.
64+
viewConfig = merge(viewConfig, UIManager.getDefaultEventTypes());
65+
} else {
66+
viewConfig.bubblingEventTypes = merge(
67+
viewConfig.bubblingEventTypes,
68+
UIManager.genericBubblingEventTypes,
69+
);
70+
viewConfig.directEventTypes = merge(
71+
viewConfig.directEventTypes,
72+
UIManager.genericDirectEventTypes,
73+
);
74+
}
7975
}
8076
}
8177

@@ -184,8 +180,10 @@ function requireNativeComponent(
184180
);
185181
}
186182

187-
attachBubblingEventTypes(viewConfig);
188-
attachDirectEventTypes(viewConfig);
183+
if (!hasAttachedDefaultEventTypes) {
184+
attachDefaultEventTypes(viewConfig);
185+
hasAttachedDefaultEventTypes = true;
186+
}
189187

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

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java

+8-17
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;
@@ -44,7 +43,7 @@
4443
import java.util.Map;
4544
import javax.annotation.Nullable;
4645

47-
/**
46+
/**
4847
* <p>Native module to allow JS to create and update native Views.</p>
4948
*
5049
* <p>
@@ -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, null, 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

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstantsHelper.java

+9-3
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
@@ -107,9 +116,6 @@
107116
@Nullable Map defaultDirectEvents,
108117
@Nullable Map cumulativeBubblingEventTypes,
109118
@Nullable Map cumulativeDirectEventTypes) {
110-
final String BUBBLING_EVENTS_KEY = "bubblingEventTypes";
111-
final String DIRECT_EVENTS_KEY = "directEventTypes";
112-
113119
Map<String, Object> viewManagerConstants = MapBuilder.newHashMap();
114120

115121
Map viewManagerBubblingEvents = viewManager.getExportedCustomBubblingEventTypeConstants();

0 commit comments

Comments
 (0)