add support for multiple bridge listeners

Reviewed By: AaaChiuuu

Differential Revision: D5200830

fbshipit-source-id: 2a12267edddd2558146721e02a0b80d649755050
This commit is contained in:
Ben Nham 2017-06-09 06:11:39 -07:00 committed by Facebook Github Bot
parent 3b6d2cd782
commit 80bc07fd60
1 changed files with 30 additions and 11 deletions

View File

@ -2,6 +2,9 @@
package com.facebook.react.bridge;
import java.util.List;
import java.util.ArrayList;
import javax.annotation.Nullable;
import com.facebook.proguard.annotations.DoNotStrip;
@ -17,17 +20,32 @@ public class ReactMarker {
void logMarker(ReactMarkerConstants name, @Nullable String tag, int instanceKey);
};
private static @Nullable MarkerListener sMarkerListener = null;
// Use a list instead of a set here because we expect the number of listeners
// to be very small, and we want listeners to be called in a deterministic
// order.
private static final List<MarkerListener> sListeners = new ArrayList<>();
public static void initialize(MarkerListener listener) {
if (sMarkerListener == null) {
sMarkerListener = listener;
@DoNotStrip
public static void addListener(MarkerListener listener) {
synchronized(sListeners) {
if (sListeners.indexOf(listener) == -1) {
sListeners.add(listener);
}
}
}
@DoNotStrip
public static void clearMarkerListener() {
sMarkerListener = null;
public static void removeListener(MarkerListener listener) {
synchronized(sListeners) {
sListeners.remove(listener);
}
}
@DoNotStrip
public static void clearMarkerListeners() {
synchronized(sListeners) {
sListeners.clear();
}
}
@DoNotStrip
@ -47,9 +65,8 @@ public class ReactMarker {
@DoNotStrip
public static void logMarker(String name, @Nullable String tag, int instanceKey) {
if (sMarkerListener != null) {
sMarkerListener.logMarker(ReactMarkerConstants.valueOf(name), tag, instanceKey);
}
ReactMarkerConstants marker = ReactMarkerConstants.valueOf(name);
logMarker(marker, tag, instanceKey);
}
@DoNotStrip
@ -69,8 +86,10 @@ public class ReactMarker {
@DoNotStrip
public static void logMarker(ReactMarkerConstants name, @Nullable String tag, int instanceKey) {
if (sMarkerListener != null) {
sMarkerListener.logMarker(name, tag, instanceKey);
synchronized(sListeners) {
for (MarkerListener listener : sListeners) {
listener.logMarker(name, tag, instanceKey);
}
}
}
}