Skip to content

Commit

Permalink
add support for multiple bridge listeners
Browse files Browse the repository at this point in the history
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 80bc07f
Showing 1 changed file with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,17 +20,32 @@ public interface MarkerListener {
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
Expand All @@ -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
Expand All @@ -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.