Skip to content

Commit 581a14d

Browse files
committed
optimization
1 parent 5c2ac31 commit 581a14d

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

packages/mixpanel_flutter/android/src/main/kotlin/com/mixpanel/mixpanel_flutter/EventBridgeSubscriber.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import kotlinx.coroutines.SupervisorJob
1010
import kotlinx.coroutines.flow.collect
1111
import kotlinx.coroutines.launch
1212
import kotlinx.coroutines.plus
13+
import kotlinx.coroutines.withContext
1314
import org.json.JSONException
1415
import org.json.JSONObject
1516

@@ -25,7 +26,11 @@ import org.json.JSONObject
2526
*/
2627
object 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

Comments
 (0)