Skip to content

Commit

Permalink
Support RCTModernEventEmitter+RCTEventEmitter in ReactSwitchEvent Eve…
Browse files Browse the repository at this point in the history
…nt 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
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed Jan 28, 2021
1 parent 1dcb4cf commit aad423d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReactSwitchEvent> {
Expand All @@ -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;
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
};

Expand Down

0 comments on commit aad423d

Please sign in to comment.