@@ -10,6 +10,7 @@ import kotlinx.coroutines.SupervisorJob
1010import kotlinx.coroutines.flow.collect
1111import kotlinx.coroutines.launch
1212import kotlinx.coroutines.plus
13+ import kotlinx.coroutines.withContext
1314import org.json.JSONException
1415import org.json.JSONObject
1516
@@ -25,7 +26,11 @@ import org.json.JSONObject
2526 */
2627object EventBridgeSubscriber {
2728
28- private val scope = CoroutineScope (SupervisorJob () + Dispatchers .Main )
29+ // Collect on Default so the per-event JSONObject → Map conversion
30+ // (which can be expensive for fat property payloads) runs off the main
31+ // thread; only the MethodChannel dispatch itself, which requires the
32+ // platform thread, is hopped back to Main.
33+ private val scope = CoroutineScope (SupervisorJob () + Dispatchers .Default )
2934 private var job: Job ? = null
3035
3136 @JvmStatic
@@ -34,13 +39,13 @@ object EventBridgeSubscriber {
3439 job = scope.launch {
3540 MixpanelEventBridge .events().collect { event ->
3641 val properties = event.properties?.let { safelyConvert(it) }
37- channel.invokeMethod(
38- " onMixpanelEvent" ,
39- mapOf (
40- " eventName" to event.eventName,
41- " properties" to properties,
42- )
42+ val args = mapOf (
43+ " eventName" to event.eventName,
44+ " properties" to properties,
4345 )
46+ withContext(Dispatchers .Main ) {
47+ channel.invokeMethod(" onMixpanelEvent" , args)
48+ }
4449 }
4550 }
4651 }
0 commit comments