-
-
Notifications
You must be signed in to change notification settings - Fork 47
fix(android): Leaking listeners #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f61f02b
83cef16
cf3542d
1afbb37
a12516d
b846df6
762adbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,64 +25,82 @@ public class PerformanceModule extends ReactContextBaseJavaModule implements Tur | |
private static final Queue<PerformanceEntry> markBuffer = new ConcurrentLinkedQueue<>(); | ||
private static boolean didEmit = false; | ||
|
||
private static final ReactMarker.MarkerListener startupMarkerListener = (name, tag, instanceKey) -> { | ||
switch (name) { | ||
case RELOAD: | ||
clearMarkBuffer(); | ||
addMark(new PerformanceMark(BRIDGE_SETUP_START, SystemClock.uptimeMillis())); | ||
break; | ||
case ATTACH_MEASURED_ROOT_VIEWS_END: | ||
case ATTACH_MEASURED_ROOT_VIEWS_START: | ||
case BUILD_NATIVE_MODULE_REGISTRY_END: | ||
case BUILD_NATIVE_MODULE_REGISTRY_START: | ||
case CONTENT_APPEARED: | ||
case CREATE_CATALYST_INSTANCE_END: | ||
case CREATE_CATALYST_INSTANCE_START: | ||
case CREATE_REACT_CONTEXT_END: | ||
case CREATE_REACT_CONTEXT_START: | ||
case CREATE_UI_MANAGER_MODULE_CONSTANTS_END: | ||
case CREATE_UI_MANAGER_MODULE_CONSTANTS_START: | ||
case CREATE_UI_MANAGER_MODULE_END: | ||
case CREATE_UI_MANAGER_MODULE_START: | ||
case CREATE_VIEW_MANAGERS_END: | ||
case CREATE_VIEW_MANAGERS_START: | ||
case DOWNLOAD_END: | ||
case DOWNLOAD_START: | ||
case LOAD_REACT_NATIVE_SO_FILE_END: | ||
case LOAD_REACT_NATIVE_SO_FILE_START: | ||
case PRE_RUN_JS_BUNDLE_START: | ||
case PRE_SETUP_REACT_CONTEXT_END: | ||
case PRE_SETUP_REACT_CONTEXT_START: | ||
case PROCESS_CORE_REACT_PACKAGE_END: | ||
case PROCESS_CORE_REACT_PACKAGE_START: | ||
case REACT_CONTEXT_THREAD_END: | ||
case REACT_CONTEXT_THREAD_START: | ||
case RUN_JS_BUNDLE_END: | ||
case RUN_JS_BUNDLE_START: | ||
case SETUP_REACT_CONTEXT_END: | ||
case SETUP_REACT_CONTEXT_START: | ||
case VM_INIT: | ||
long startTime = SystemClock.uptimeMillis(); | ||
addMark(new PerformanceMark(getMarkName(name), startTime)); | ||
break; | ||
} | ||
}; | ||
|
||
private final ReactMarker.MarkerListener contentAppearedListener = (name, tag, instanceKey) -> { | ||
switch (name) { | ||
case CONTENT_APPEARED: | ||
eventsBuffered = false; | ||
emitNativeStartupTime(); | ||
emitBufferedMarks(); | ||
break; | ||
case RELOAD: | ||
eventsBuffered = true; | ||
break; | ||
} | ||
}; | ||
|
||
public PerformanceModule(@NonNull final ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
setupMarkerListener(); | ||
setupNativeMarkerListener(); | ||
} | ||
|
||
private void setupMarkerListener() { | ||
ReactMarker.addListener( | ||
contentAppearedListener | ||
); | ||
} | ||
|
||
private void setupNativeMarkerListener() { | ||
RNPerformance.getInstance().addListener(this); | ||
} | ||
|
||
// Need to set up the marker listener before the react module is initialized | ||
// to capture all events | ||
public static void setupListener() { | ||
ReactMarker.addListener( | ||
(name, tag, instanceKey) -> { | ||
switch (name) { | ||
case RELOAD: | ||
clearMarkBuffer(); | ||
addMark(new PerformanceMark(BRIDGE_SETUP_START, SystemClock.uptimeMillis())); | ||
break; | ||
case ATTACH_MEASURED_ROOT_VIEWS_END: | ||
case ATTACH_MEASURED_ROOT_VIEWS_START: | ||
case BUILD_NATIVE_MODULE_REGISTRY_END: | ||
case BUILD_NATIVE_MODULE_REGISTRY_START: | ||
case CONTENT_APPEARED: | ||
case CREATE_CATALYST_INSTANCE_END: | ||
case CREATE_CATALYST_INSTANCE_START: | ||
case CREATE_REACT_CONTEXT_END: | ||
case CREATE_REACT_CONTEXT_START: | ||
case CREATE_UI_MANAGER_MODULE_CONSTANTS_END: | ||
case CREATE_UI_MANAGER_MODULE_CONSTANTS_START: | ||
case CREATE_UI_MANAGER_MODULE_END: | ||
case CREATE_UI_MANAGER_MODULE_START: | ||
case CREATE_VIEW_MANAGERS_END: | ||
case CREATE_VIEW_MANAGERS_START: | ||
case DOWNLOAD_END: | ||
case DOWNLOAD_START: | ||
case LOAD_REACT_NATIVE_SO_FILE_END: | ||
case LOAD_REACT_NATIVE_SO_FILE_START: | ||
case PRE_RUN_JS_BUNDLE_START: | ||
case PRE_SETUP_REACT_CONTEXT_END: | ||
case PRE_SETUP_REACT_CONTEXT_START: | ||
case PROCESS_CORE_REACT_PACKAGE_END: | ||
case PROCESS_CORE_REACT_PACKAGE_START: | ||
case REACT_CONTEXT_THREAD_END: | ||
case REACT_CONTEXT_THREAD_START: | ||
case RUN_JS_BUNDLE_END: | ||
case RUN_JS_BUNDLE_START: | ||
case SETUP_REACT_CONTEXT_END: | ||
case SETUP_REACT_CONTEXT_START: | ||
case VM_INIT: | ||
long startTime = SystemClock.uptimeMillis(); | ||
addMark(new PerformanceMark(getMarkName(name), startTime)); | ||
break; | ||
|
||
} | ||
} | ||
); | ||
ReactMarker.addListener(startupMarkerListener); | ||
} | ||
|
||
private static void clearMarkBuffer() { | ||
|
@@ -118,28 +136,19 @@ public String getName() { | |
return PERFORMANCE_MODULE; | ||
} | ||
|
||
public void addListener(String eventName) { | ||
|
||
} | ||
|
||
public void removeListeners(double count) { | ||
|
||
} | ||
|
||
private void emitNativeStartupTime() { | ||
safelyEmitMark(new PerformanceMark("nativeLaunchStart", StartTimeProvider.getStartTime())); | ||
safelyEmitMark(new PerformanceMark("nativeLaunchEnd", StartTimeProvider.getEndTime())); | ||
} | ||
|
||
private void setupMarkerListener() { | ||
ReactMarker.addListener( | ||
(name, tag, instanceKey) -> { | ||
switch (name) { | ||
case CONTENT_APPEARED: | ||
eventsBuffered = false; | ||
emitNativeStartupTime(); | ||
emitBufferedMarks(); | ||
break; | ||
case RELOAD: | ||
eventsBuffered = true; | ||
break; | ||
} | ||
} | ||
); | ||
} | ||
|
||
private void safelyEmitMark(PerformanceEntry entry) { | ||
if (eventsBuffered) { | ||
addMark(entry); | ||
|
@@ -215,13 +224,9 @@ public void logMarker(PerformanceEntry entry) { | |
} | ||
|
||
@Override | ||
public void onCatalystInstanceDestroy() { | ||
super.onCatalystInstanceDestroy(); | ||
public void invalidate() { | ||
super.invalidate(); | ||
RNPerformance.getInstance().removeListener(this); | ||
} | ||
|
||
// Fix new arch runtime error | ||
public void addListener(String eventName) { | ||
|
||
ReactMarker.removeListener(contentAppearedListener); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we remove the other listener too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I.e.:
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need a separate method for it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I wanted to make this look similar to
setupNativeMarkerListener
which was already in the codebase