(Translated by https://www.hiragana.jp/)
GitHub - czy1121/flowbus: 事件总线(基于SharedFlow)
Skip to content

czy1121/flowbus

Repository files navigation

flowbus

事件じけん总线(もと于SharedFlow)

  • 支持しじ生命せいめい周期しゅうき感知かんち
  • 支持しじ动清じょそら事件じけん

观察

/**
 * 订阅事件じけん(EventFlow)あるじょう态(StateFlow)
 *
 * - 对于 [EventFlow],进入 [minState] 开始订阅,だつ离 [minState] 取消とりけし订阅
 * - 对于 [StateFlow],满足 [minState] 时立こくしょう费,不足ふそく [minState] 时缓そん最近さいきんてき事件じけんまちさいつぎ进入时消费
 *
 */
fun <T> Flow<T>.observe(owner: LifecycleOwner, minState: Lifecycle.State = Lifecycle.State.STARTED, action: suspend (T) -> Unit)


/**
 * 订阅事件じけん,满足 [minState] 时立こくしょう费,不足ふそく [minState] 时缓そん最近さいきんてき事件じけんまちさいつぎ进入时消费
 *
 * しめせれいざいA页观察流,しかきさき进入B页发送りょう事件じけんかいいたA页消费事件じけん
 */
fun <T> Flow<T>.observeLatest(owner: LifecycleOwner, minState: Lifecycle.State = Lifecycle.State.STARTED, action: suspend (T) -> Unit)


/**
 * 进入 [minState] 开始订阅,だつ离 [minState] 取消とりけし订阅
 * */
fun <T> Flow<T>.observeRepeat(owner: LifecycleOwner, minState: Lifecycle.State = Lifecycle.State.STARTED, action: suspend (T) -> Unit)

EventBus 使用しよう

data class FooEvent(val value: String)
data class BarEvent(val value: String)

object Global {
    val eventFoo = EventFlow<FooEvent>()

    val stateCount = MutableStateFlow(0)
}

Global.stateCount.observe(this) {
    binding.state.text = "count = $it"
}
Global.eventFoo.observeLatest(this) {
    log("Global => $it")
} 
EventBus.observe<BarEvent>(this) {
    log("EventBus => $it")
}

binding.fooEvent.setOnClickListener {
    Global.eventFoo.tryEmit(FooEvent("clicked in main"))
}
binding.barEvent.setOnClickListener {
    EventBus.emit(BarEvent("clicked in main"))
}

Gradle

repositories {
    maven { url "https://gitee.com/ezy/repo/raw/cosmo/"}
}
dependencies {
    implementation "me.reezy.cosmo:flowbus:0.9.0"
}

LICENSE

The Component is open-sourced software licensed under the Apache license.

About

事件じけん总线(もと于SharedFlow)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages