Permalink
Browse files

Untangle NativeAnimatedNodesManager from UIManager internals.

Reviewed By: AaaChiuuu

Differential Revision: D5861523

fbshipit-source-id: 39eb79b91d4d5e2295d03369f1be585ae7836c75
  • Loading branch information...
Dmitry Zakharov authored and facebook-github-bot committed Sep 19, 2017
1 parent 5180995 commit 4fc9e20fceb11bfe82644f935cbb583b38c638c2
@@ -11,7 +11,6 @@
import android.util.SparseArray;
import com.facebook.infer.annotation.Assertions;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
@@ -59,7 +58,7 @@
// Mapping of a view tag and an event name to a list of event animation drivers. 99% of the time
// there will be only one driver per mapping so all code code should be optimized around that.
private final Map<String, List<EventAnimationDriver>> mEventDrivers = new HashMap<>();
private final Map<String, Map<String, String>> mCustomEventTypes;
private final UIManagerModule.CustomEventNamesResolver mCustomEventNamesResolver;
private final UIImplementation mUIImplementation;
private int mAnimatedGraphBFSColor = 0;
// Used to avoid allocating a new array on every frame in `runUpdates` and `onEventDispatch`.
@@ -68,8 +67,7 @@
public NativeAnimatedNodesManager(UIManagerModule uiManager) {
mUIImplementation = uiManager.getUIImplementation();
uiManager.getEventDispatcher().addListener(this);
Object customEventTypes = Assertions.assertNotNull(uiManager.getConstants()).get("customDirectEventTypes");
mCustomEventTypes = (Map<String, Map<String, String>>) customEventTypes;
mCustomEventNamesResolver = uiManager.getDirectEventNamesResolver();
}
/*package*/ @Nullable AnimatedNode getNodeById(int id) {
@@ -371,12 +369,7 @@ public void onEventDispatch(Event event) {
if (!mEventDrivers.isEmpty()) {
// If the event has a different name in native convert it to it's JS name.
String eventName = event.getEventName();
Map<String, String> customEventType = mCustomEventTypes.get(eventName);
if (customEventType != null) {
eventName = customEventType.get("registrationName");
}
String eventName = mCustomEventNamesResolver.resolveCustomEventName(event.getEventName());
List<EventAnimationDriver> driversForKey = mEventDrivers.get(event.getViewTag() + eventName);
if (driversForKey != null) {
for (EventAnimationDriver driver : driversForKey) {
@@ -70,6 +70,17 @@
public class UIManagerModule extends ReactContextBaseJavaModule implements
OnBatchCompleteListener, LifecycleEventListener, PerformanceCounter {
/**
* Resolves a name coming from native side to a name of the event that is exposed to JS.
*/
public interface CustomEventNamesResolver {
/**
* Returns custom event name by the provided event name.
*/
@Nullable String resolveCustomEventName(String eventName);
}
protected static final String NAME = "UIManager";
private static final boolean DEBUG = false;
@@ -164,6 +175,27 @@ public void onCatalystInstanceDestroy() {
}
}
/**
* Resolves Direct Event name exposed to JS from the one known to the Native side.
*/
public CustomEventNamesResolver getDirectEventNamesResolver() {
return new CustomEventNamesResolver() {
@Override
public @Nullable String resolveCustomEventName(String eventName) {
Map<String, Map> directEventTypes =
(Map<String, Map>) getConstants().get(
UIManagerModuleConstantsHelper.CUSTOM_DIRECT_EVENT_TYPES_KEY);
if (directEventTypes != null) {
Map<String, String> customEventType = (Map<String, String>) directEventTypes.get(eventName);
if (customEventType != null) {
return customEventType.get("registrationName");
}
}
return eventName;
}
};
}
@Override
public Map<String, Long> getPerformanceCounters() {
return mUIImplementation.getProfiledBatchPerfCounters();
@@ -12,7 +12,6 @@
import java.util.List;
import java.util.Map;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.common.MapBuilder;
import com.facebook.systrace.Systrace;
import com.facebook.systrace.SystraceMessage;
@@ -25,8 +24,8 @@
*/
/* package */ class UIManagerModuleConstantsHelper {
private static final String CUSTOM_BUBBLING_EVENT_TYPES_KEY = "customBubblingEventTypes";
private static final String CUSTOM_DIRECT_EVENT_TYPES_KEY = "customDirectEventTypes";
/* package */ static final String CUSTOM_BUBBLING_EVENT_TYPES_KEY = "customBubblingEventTypes";
/* package */ static final String CUSTOM_DIRECT_EVENT_TYPES_KEY = "customDirectEventTypes";
/**
* Generates map of constants that is then exposed by {@link UIManagerModule}.
@@ -41,8 +40,7 @@
* TODO(6845124): Create a test for this
*/
/* package */ static Map<String, Object> createConstants(
List<ViewManager> viewManagers,
boolean lazyViewManagersEnabled) {
List<ViewManager> viewManagers, boolean lazyViewManagersEnabled) {
Map<String, Object> constants = UIManagerModuleConstants.getConstants();
// Generic/default event types:
@@ -69,18 +67,20 @@
if (viewManagerBubblingEvents != null) {
recursiveMerge(allBubblingEventTypes, viewManagerBubblingEvents);
recursiveMerge(viewManagerBubblingEvents, genericBubblingEventTypes);
viewManagerConstants.put("bubblingEventTypes", viewManagerBubblingEvents);
} else {
viewManagerConstants.put("bubblingEventTypes", genericBubblingEventTypes);
viewManagerBubblingEvents = genericBubblingEventTypes;
}
viewManagerConstants.put("bubblingEventTypes", viewManagerBubblingEvents);
Map viewManagerDirectEvents = viewManager.getExportedCustomDirectEventTypeConstants();
if (viewManagerDirectEvents != null) {
recursiveMerge(allDirectEventTypes, viewManagerDirectEvents);
recursiveMerge(viewManagerDirectEvents, genericDirectEventTypes);
viewManagerConstants.put("directEventTypes", viewManagerDirectEvents);
} else {
viewManagerConstants.put("directEventTypes", genericDirectEventTypes);
viewManagerDirectEvents = genericDirectEventTypes;
}
viewManagerConstants.put("directEventTypes", viewManagerDirectEvents);
Map customViewConstants = viewManager.getExportedViewConstants();
if (customViewConstants != null) {
viewManagerConstants.put("Constants", customViewConstants);
@@ -22,6 +22,8 @@
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -108,6 +110,27 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
return MapBuilder.of("customDirectEventTypes", MapBuilder.newHashMap());
}
});
PowerMockito
.when(mUIManagerMock.getDirectEventNamesResolver())
.thenAnswer(new Answer<UIManagerModule.CustomEventNamesResolver>() {
@Override
public UIManagerModule.CustomEventNamesResolver answer(InvocationOnMock invocation) throws Throwable {
return new UIManagerModule.CustomEventNamesResolver() {
@Override
public String resolveCustomEventName(String eventName) {
Map<String, Map> directEventTypes =
(Map<String, Map>) mUIManagerMock.getConstants().get("customDirectEventTypes");
if (directEventTypes != null) {
Map<String, String> customEventType = (Map<String, String>) directEventTypes.get(eventName);
if (customEventType != null) {
return customEventType.get("registrationName");
}
}
return eventName;
}
};
}
});
mNativeAnimatedNodesManager = new NativeAnimatedNodesManager(mUIManagerMock);
}

0 comments on commit 4fc9e20

Please sign in to comment.