mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Use weak hash map for react scroll view helper
Summary: This prevents us from leaking things via this static field. Changelog: [Android][Changed] Native ScrollView listeners list maintains weak references to listeners to avoid memory leaks Reviewed By: JoshuaGross Differential Revision: D29317937 fbshipit-source-id: 4daeb8b5533cccaebcb03acf3d595dfa58de7883
This commit is contained in:
committed by
Facebook GitHub Bot
parent
5cb2debbea
commit
b673e352fb
+18
-3
@@ -14,8 +14,9 @@ import android.widget.OverScroller;
|
||||
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.uimanager.UIManagerHelper;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
/** Helper class that deals with emitting Scroll Events. */
|
||||
public class ReactScrollViewHelper {
|
||||
@@ -33,7 +34,8 @@ public class ReactScrollViewHelper {
|
||||
}
|
||||
|
||||
// Support global native listeners for scroll events
|
||||
private static List<ScrollListener> sScrollListeners = new ArrayList<>();
|
||||
private static Set<ScrollListener> sScrollListeners =
|
||||
Collections.newSetFromMap(new WeakHashMap<ScrollListener, Boolean>());
|
||||
|
||||
// If all else fails, this is the hardcoded value in OverScroller.java, in AOSP.
|
||||
// The default is defined here (as of this diff):
|
||||
@@ -156,6 +158,19 @@ public class ReactScrollViewHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a scroll listener.
|
||||
*
|
||||
* <p>Note that you must keep a reference to this scroll listener because this class only keeps a
|
||||
* weak reference to it (to prevent memory leaks). This means that code like <code>
|
||||
* addScrollListener(new ScrollListener() {...})</code> won't work, you need to do this instead:
|
||||
* <code>
|
||||
* mScrollListener = new ScrollListener() {...};
|
||||
* ReactScrollViewHelper.addScrollListener(mScrollListener);
|
||||
* </code> instead.
|
||||
*
|
||||
* @param listener
|
||||
*/
|
||||
public static void addScrollListener(ScrollListener listener) {
|
||||
sScrollListeners.add(listener);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user