-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove use of org.greenrobot.eventbus
- Loading branch information
Showing
2 changed files
with
33 additions
and
30 deletions.
There are no files selected for viewing
34 changes: 17 additions & 17 deletions
34
android/app/src/main/kotlin/org/getlantern/lantern/event/EventBus.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters