Skip to content

Commit

Permalink
Remove use of org.greenrobot.eventbus
Browse files Browse the repository at this point in the history
  • Loading branch information
atavism committed Jun 20, 2024
1 parent 8b67cb3 commit c007ddd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package org.getlantern.lantern.event

import kotlin.coroutines.coroutineContext
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterIsInstance
import org.getlantern.lantern.model.Bandwidth
import org.getlantern.lantern.model.Stats
import kotlin.coroutines.coroutineContext

object EventBus {
private val _events = MutableSharedFlow<Any>()
val events = _events.asSharedFlow()
private val _events = MutableSharedFlow<Any>()
val events = _events.asSharedFlow()

suspend fun publish(event: Any) {
_events.emit(event)
}
suspend fun publish(event: Any) {
_events.emit(event)
}

suspend inline fun <reified T> subscribe(crossinline onEvent: (T) -> Unit) {
events.filterIsInstance<T>()
.collectLatest { event ->
coroutineContext.ensureActive()
onEvent(event)
}
}
suspend inline fun <reified T> subscribe(crossinline onEvent: (T) -> Unit) {
events
.filterIsInstance<T>()
.collectLatest { event ->
coroutineContext.ensureActive()
onEvent(event)
}
}
}

sealed class AppEvent {
data class StatsEvent(val stats: Stats) : AppEvent()
data class StatsEvent(
val stats: Stats,
) : AppEvent()
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@ package org.getlantern.lantern.event

import android.os.Handler
import android.os.Looper
import androidx.lifecycle.LifecycleOwner
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.EventChannel
import org.getlantern.mobilesdk.model.Event
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import org.getlantern.lantern.model.Stats
import org.getlantern.mobilesdk.model.Event
import java.util.EnumMap
import java.util.concurrent.ConcurrentSkipListSet
import java.util.concurrent.atomic.AtomicReference
import org.getlantern.mobilesdk.Logger
import org.getlantern.lantern.model.Stats

abstract class EventManager(
private val name: String,
flutterEngine: FlutterEngine? = null
flutterEngine: FlutterEngine? = null,
) : EventChannel.StreamHandler {

private val activeSubscribers: EnumMap<Event, ConcurrentSkipListSet<Int>> =
EnumMap(Event::class.java)

Expand All @@ -33,12 +30,15 @@ abstract class EventManager(
flutterEngine?.let {
EventChannel(
flutterEngine.dartExecutor,
name
name,
).setStreamHandler(this)
}
}

fun onNewEvent(event: Event, params: MutableMap<String, Any?> = mutableMapOf()) {
fun onNewEvent(
event: Event,
params: MutableMap<String, Any?> = mutableMapOf(),
) {
handler.post {
synchronized(this@EventManager) {
params["eventName"] = event.name
Expand Down Expand Up @@ -68,14 +68,17 @@ abstract class EventManager(

fun subscribeAppEvent(onEvent: (AppEvent) -> Unit) {
CoroutineScope(Dispatchers.Main + SupervisorJob()).launch {
EventBus.subscribe<AppEvent> { appEvent ->
onEvent(appEvent)
}
EventBus.subscribe<AppEvent> { appEvent ->
onEvent(appEvent)
}
}
}

@Synchronized
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
override fun onListen(
arguments: Any?,
events: EventChannel.EventSink?,
) {
activeSink.set(events)
val args = arguments as Map<String, Any>
val subscriberID = args["subscriberID"] as Int
Expand Down

0 comments on commit c007ddd

Please sign in to comment.