Skip to content

Commit 5a4e5ee

Browse files
authored
fix(android): fix onMapSteady event crash (#4162)
* fix(android): fix onMapSteady event name causing crash Event key was "topOnMapSteady" which doesn't match the codegen-registered name. Use "onMapSteady" to match the JS prop directly. Fixes #4105 * fix(android): fix event payload and timer bugs in CameraGestureObserver - Override toJSON() in MapSteadyEvent to return flat payload (not wrapped in type/payload structure) so JS receives fields on nativeEvent directly - Fix timer leak in scheduleQuietCheck — cancel previous before scheduling - Fix scheduleTimeout to not reset on every markActivity, enabling periodic heartbeat timeouts during long gestures - Remove emittedForCurrentActivity flag (no longer needed with timer fixes)
1 parent 83641f4 commit 5a4e5ee

3 files changed

Lines changed: 12 additions & 8 deletions

File tree

android/src/main/java/com/rnmapbox/rnmbx/events/MapSteadyEvent.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.facebook.react.bridge.WritableMap
77
/**
88
* Direct event for CameraGestureObserver -> onMapSteady
99
* JS registrationName: onMapSteady
10-
* Native event name (key): topOnMapSteady
10+
* Native event name (key): onMapSteady
1111
*/
1212
class MapSteadyEvent(
1313
view: View?,
@@ -16,7 +16,7 @@ class MapSteadyEvent(
1616
private val lastGestureType: String?
1717
) : AbstractEvent(view, "mapSteady") {
1818
override val key: String
19-
get() = "topOnMapSteady"
19+
get() = "onMapSteady"
2020

2121
override val payload: WritableMap
2222
get() = Arguments.createMap().apply {
@@ -34,6 +34,12 @@ class MapSteadyEvent(
3434
putDouble("timestamp", System.currentTimeMillis().toDouble())
3535
}
3636

37+
override fun toJSON(): WritableMap {
38+
val map = Arguments.createMap()
39+
map.merge(payload)
40+
return map
41+
}
42+
3743
override fun canCoalesce(): Boolean {
3844
// Do not coalesce - each steady/timeout event is significant
3945
return false

android/src/main/java/com/rnmapbox/rnmbx/events/RNMBXCameraGestureObserver.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class RNMBXCameraGestureObserver(
3737
private var lastTransitionEndedAtMs: Double? = null
3838
private var quietRunnable: Runnable? = null
3939
private var timeoutRunnable: Runnable? = null
40-
private var emittedForCurrentActivity: Boolean = false
4140

4241
private val quietMs: Double get() = quietPeriodMs ?: 200.0
4342
private val maxMs: Double? get() = maxIntervalMs
@@ -84,9 +83,9 @@ class RNMBXCameraGestureObserver(
8483
}
8584

8685
private fun scheduleQuietCheck() {
86+
cancelQuietTimer()
8787
val delay = quietMs
8888
if (delay <= 0) {
89-
cancelQuietTimer()
9089
maybeEmitSteady()
9190
return
9291
}
@@ -99,16 +98,17 @@ class RNMBXCameraGestureObserver(
9998
}
10099

101100
private fun scheduleTimeout() {
101+
if (timeoutRunnable != null) return
102102
val delay = maxMs ?: return
103103
val runnable = Runnable {
104+
timeoutRunnable = null
104105
emitTimeout()
105106
}
106107
timeoutRunnable = scheduleTimer(delay, runnable)
107108
}
108109

109110
private fun markActivity(gestureType: String? = null) {
110111
if (gestureType != null) lastGestureType = gestureType
111-
emittedForCurrentActivity = false
112112
scheduleQuietCheck()
113113
scheduleTimeout()
114114
}
@@ -122,7 +122,6 @@ class RNMBXCameraGestureObserver(
122122
}
123123

124124
private fun emitSteady(idleDurationMs: Double) {
125-
if (emittedForCurrentActivity) return
126125
cancelQuietTimer()
127126
cancelTimeoutTimer()
128127
val gesture = lastGestureType
@@ -131,7 +130,6 @@ class RNMBXCameraGestureObserver(
131130
MapSteadyEvent.make(this, "steady", idleDurationMs, gesture)
132131
)
133132
lastGestureType = null
134-
emittedForCurrentActivity = true
135133
}
136134

137135
private fun emitTimeout() {

android/src/main/java/com/rnmapbox/rnmbx/events/RNMBXCameraGestureObserverManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class RNMBXCameraGestureObserverManager(private val mContext: ReactApplicationCo
6565

6666
// Map the native event name to the JS registration name for direct events
6767
override fun customEvents(): Map<String, String> = mapOf(
68-
"topOnMapSteady" to "onMapSteady"
68+
"onMapSteady" to "onMapSteady"
6969
)
7070

7171
companion object {

0 commit comments

Comments
 (0)