From 80bc07fd603b9ef27a1a2a977f03ee3e5a7b3e61 Mon Sep 17 00:00:00 2001 From: Ben Nham Date: Fri, 9 Jun 2017 06:11:39 -0700 Subject: [PATCH] add support for multiple bridge listeners Reviewed By: AaaChiuuu Differential Revision: D5200830 fbshipit-source-id: 2a12267edddd2558146721e02a0b80d649755050 --- .../facebook/react/bridge/ReactMarker.java | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactMarker.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactMarker.java index e4293fffd03cbd..1d0d802bacaddc 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactMarker.java +++ b/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 @@ 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 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); + } } } }