From aad423d59e99a76cfb10b466bd13463aa4926b80 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Thu, 28 Jan 2021 14:01:07 -0800 Subject: [PATCH] Support RCTModernEventEmitter+RCTEventEmitter in ReactSwitchEvent Event class Summary: Support RCTModernEventEmitter+RCTEventEmitter in an Event class(es). This improves perf in Fabric. Migrate any constructor callsites to the new constructor and deprecate the previous one. Changelog: [Internal] Reviewed By: PeteTheHeat, mdvacca Differential Revision: D26056823 fbshipit-source-id: 1d25afb2d4cfd7e539214d4592e361260f98fc56 --- .../views/switchview/ReactSwitchEvent.java | 28 ++++++++++--------- .../views/switchview/ReactSwitchManager.java | 5 +++- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchEvent.java b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchEvent.java index 9cc34d0e43951a..38d0a76c91e210 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchEvent.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchEvent.java @@ -7,10 +7,10 @@ package com.facebook.react.views.switchview; +import androidx.annotation.Nullable; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.WritableMap; import com.facebook.react.uimanager.events.Event; -import com.facebook.react.uimanager.events.RCTEventEmitter; /** Event emitted by a ReactSwitchManager once a switch is fully switched on/off */ /*package*/ class ReactSwitchEvent extends Event { @@ -19,8 +19,13 @@ private final boolean mIsChecked; + @Deprecated public ReactSwitchEvent(int viewId, boolean isChecked) { - super(viewId); + this(-1, viewId, isChecked); + } + + public ReactSwitchEvent(int surfaceId, int viewId, boolean isChecked) { + super(surfaceId, viewId); mIsChecked = isChecked; } @@ -33,21 +38,18 @@ public String getEventName() { return EVENT_NAME; } + @Nullable @Override - public short getCoalescingKey() { - // All switch events for a given view can be coalesced. - return 0; - } - - @Override - public void dispatch(RCTEventEmitter rctEventEmitter) { - rctEventEmitter.receiveEvent(getViewTag(), getEventName(), serializeEventData()); - } - - private WritableMap serializeEventData() { + protected WritableMap getEventData() { WritableMap eventData = Arguments.createMap(); eventData.putInt("target", getViewTag()); eventData.putBoolean("value", getIsChecked()); return eventData; } + + @Override + public short getCoalescingKey() { + // All switch events for a given view can be coalesced. + return 0; + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java index c2196f3ff142dc..e1a20562ae1032 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java @@ -20,6 +20,7 @@ import com.facebook.react.uimanager.PixelUtil; import com.facebook.react.uimanager.SimpleViewManager; import com.facebook.react.uimanager.ThemedReactContext; +import com.facebook.react.uimanager.UIManagerHelper; import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.ViewManagerDelegate; import com.facebook.react.uimanager.ViewProps; @@ -89,7 +90,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { uiManager .getEventDispatcher() - .dispatchEvent(new ReactSwitchEvent(buttonView.getId(), isChecked)); + .dispatchEvent( + new ReactSwitchEvent( + UIManagerHelper.getSurfaceId(reactContext), buttonView.getId(), isChecked)); } };