Skip to content

Commit 33a5918

Browse files
antonisclaude
andcommitted
fix(android,ios): Fix shake detection lifecycle bugs
- iOS: Remove manual `hasListeners = YES` from `enableShakeDetection`; the flag is managed by `startObserving`/`stopObserving` exclusively. Setting it manually could cause spurious events when no JS listeners are registered. - Android: `removeListeners` has no event-type context so it incorrectly decremented the shake counter on any listener removal (e.g. frame/log events), prematurely stopping shake detection. Move shake detection management to `enableShakeDetection`/`disableShakeDetection` which are explicitly called from JS and carry clear intent. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent cfcb5c7 commit 33a5918

2 files changed

Lines changed: 5 additions & 18 deletions

File tree

packages/core/android/src/main/java/io/sentry/react/RNSentryModuleImpl.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ public class RNSentryModuleImpl {
125125

126126
private static final String ON_SHAKE_EVENT = "rn_sentry_on_shake";
127127
private @Nullable SentryShakeDetector shakeDetector;
128-
private int shakeListenerCount = 0;
129128

130129
/** Max trace file size in bytes. */
131130
private long maxTraceFileSize = 5 * 1024 * 1024;
@@ -207,23 +206,15 @@ public void crash() {
207206
}
208207

209208
public void addListener(String eventType) {
210-
if (ON_SHAKE_EVENT.equals(eventType)) {
211-
shakeListenerCount++;
212-
if (shakeListenerCount == 1) {
213-
startShakeDetection();
214-
}
215-
return;
216-
}
217209
// Is must be defined otherwise the generated interface from TS won't be
218210
// fulfilled
219211
logger.log(SentryLevel.ERROR, "addListener of NativeEventEmitter can't be used on Android!");
220212
}
221213

222214
public void removeListeners(double id) {
223-
shakeListenerCount = Math.max(0, shakeListenerCount - (int) id);
224-
if (shakeListenerCount == 0) {
225-
stopShakeDetection();
226-
}
215+
// removeListeners does not carry event-type information, so it cannot be used
216+
// to track shake listeners selectively. Shake detection is managed exclusively
217+
// via enableShakeDetection / disableShakeDetection.
227218
}
228219

229220
private void startShakeDetection() {
@@ -254,14 +245,11 @@ private void stopShakeDetection() {
254245
}
255246

256247
public void enableShakeDetection() {
257-
// On Android, shake detection is started via addListener. This method is a no-op
258-
// because it exists to satisfy the cross-platform spec (on iOS, the NativeEventEmitter
259-
// addListener does not reliably dispatch to native, so an explicit call is needed).
248+
startShakeDetection();
260249
}
261250

262251
public void disableShakeDetection() {
263-
// On Android, shake detection is stopped via removeListeners. This method is a no-op
264-
// for the same reason as enableShakeDetection.
252+
stopShakeDetection();
265253
}
266254

267255
public void fetchModules(Promise promise) {

packages/core/ios/RNSentry.mm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ - (void)handleShakeDetected
322322
[shakeDetector performSelector:@selector(enable)];
323323
#pragma clang diagnostic pop
324324
}
325-
hasListeners = YES;
326325
}
327326

328327
RCT_EXPORT_METHOD(disableShakeDetection)

0 commit comments

Comments
 (0)