Permalink
Browse files

add support for multiple bridge listeners

Reviewed By: AaaChiuuu

Differential Revision: D5200830

fbshipit-source-id: 2a12267edddd2558146721e02a0b80d649755050
  • Loading branch information...
bnham authored and facebook-github-bot committed Jun 9, 2017
1 parent 3b6d2cd commit 80bc07fd603b9ef27a1a2a977f03ee3e5a7b3e61
Showing with 30 additions and 11 deletions.
  1. +30 −11 ReactAndroid/src/main/java/com/facebook/react/bridge/ReactMarker.java
@@ -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 @@
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<>();
@DoNotStrip
public static void addListener(MarkerListener listener) {
synchronized(sListeners) {
if (sListeners.indexOf(listener) == -1) {
sListeners.add(listener);
}
}
}
public static void initialize(MarkerListener listener) {
if (sMarkerListener == null) {
sMarkerListener = listener;
@DoNotStrip
public static void removeListener(MarkerListener listener) {
synchronized(sListeners) {
sListeners.remove(listener);
}
}
@DoNotStrip
public static void clearMarkerListener() {
sMarkerListener = null;
public static void clearMarkerListeners() {
synchronized(sListeners) {
sListeners.clear();
}
}
@DoNotStrip
@@ -47,9 +65,8 @@ public static void logMarker(String name, @Nullable String tag) {
@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 static void logMarker(ReactMarkerConstants name, @Nullable String tag) {
@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);
}
}
}
}

0 comments on commit 80bc07f

Please sign in to comment.