GithubHelp home page GithubHelp logo

q4452542007 / flow-event-bus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from biubiuqiu0/flow-event-bus

0.0 0.0 0.0 192 KB

EventBus for Android,消息总线,基于SharedFlow,具有生命周期感知能力,支持Sticky,支持线程切换,支持延迟发送。

Kotlin 100.00%

flow-event-bus's Introduction

背景

跨页面通信是一个比较常见的场景,通常我们会选择使用EventBus,但EventBus无法感知声明周期,收到消息就会回调,所以有了LiveData之后很快就有了LiveEventBus。不过它也有缺点,比如不能切换线程。现在SharedFlow稳定了,那是不是也能搞一波?

于是有了FlowEventBus

常用消息总线对比

消息总线 延迟发送 有序接收消息 Sticky 生命周期感知 跨进程/APP 线程分发
EventBus
RxBus
LiveEventBus
FlowEventBus

设计构思

通过学习 从 LiveData 迁移到 Kotlin 数据流 得到思路:

  • SharedFlow作为事件载体 :
    优点:
  • 依托协程轻松切换线程
  • 可以通过replay实现粘性效果
  • 可以被多个观察者订阅
  • 无观察者自动清除事件不会造成积压

结合 Lifecycle 感知生命周期,做到响应时机可控 。

不仅可以全局范围的事件,也可以单页面内的通信而不透传到别的页面,如:Activity内部,Fragment内部通信。

依赖库版本

关键在于 kotlinx-coroutines > 1.4.xlifecycle-runtime-ktx > 2.3.x

API

以下示例中的XEvent均是随意定义的类,只是测试时为了区分事件而定义的名字

事件发送

//全局范围
postEvent(AppScopeEvent("form TestFragment"))

//Fragment 内部范围 
postEvent(fragment,FragmentEvent("form TestFragment"))

//Activity 内部范围
postEvent(requireActivity(),ActivityEvent("form TestFragment"))

事件监听

//接收 Activity Scope事件
observeEvent<ActivityEvent>(scope = requireActivity()) {
    ...
}

//接收 Fragment Scope事件
observeEvent<FragmentEvent>(scope = fragment) {
    ...
}

//接收 App Scope事件
observeEvent<AppScopeEvent> {
    ...
}

Like ObserveForever:

//此时需要指定协程范围
observeEvent<GlobalEvent>(scope = coroutineScope) {
       ...
}

延迟发送

postEvent(CustomEvent(value = "Hello Word"),1000)

线程切换

observeEvent<ActivityEvent>(Dispatchers.IO) {
    ...
}

指定可感知的最小生命状态

observeEvent<ActivityEvent>(minActiveState = Lifecycle.State.DESTROYED) {
   ...
}

以粘性方式监听

observeEvent<GlobalEvent>(isSticky = true) {
   ...
}

删除粘性事件

移除粘性事件实例

    removeStickyEvent(StickyEvent::class.java)
    removeStickyEvent(fragment,StickyEvent::class.java)
    removeStickyEvent(activity,StickyEvent::class.java)

清除粘性事件

粘性事件实例还在,但没有了ReplayCache,新观察者不会收到回调

    clearStickyEvent(GlobalEvent::class.java)

获取事件监听者数量

    getEventObserverCount(GlobalEvent::class.java)

原理

以上功能依托于Kotlin协程的SharedFlowLifecycle 因此实现起来非常简单。

  • 粘性事件
MutableSharedFlow<Any>(
    replay = if (isSticky) 1 else 0,
    extraBufferCapacity = Int.MAX_VALUE //避免挂起导致数据发送失败
)
  • 生命周期感知
fun <T> LifecycleOwner.launchWhenStateAtLeast(
    minState: Lifecycle.State,
    block: suspend CoroutineScope.() -> T
) {
    lifecycleScope.launch {
        lifecycle.whenStateAtLeast(minState, block)
    }
}
  • 切换线程 whenStateAtLeast 由于执行的block默认是在主线程,因此需要手动切换线程:
lifecycleOwner.launchWhenStateAtLeast(minState) {
    flow.collect { value ->
        lifecycleOwner.lifecycleScope.launch(dispatcher) {
                onReceived.invoke(value as T)
        }
    }
}
  • 延迟事件
viewModelScope.launch {
    delay(time)
    flow.emit(value)
}
  • 有序分发
    Flow本身就是有序的

  • 全局单例
    使用全局ViewModel,主要是因为有ViewModelScope,可以避免使用GlobalScope,如果想要单页面内部组件通信,那就使用ActivityScope的ViewModel就行了:

object ApplicationScopeViewModelProvider : ViewModelStoreOwner {

    private val eventViewModelStore: ViewModelStore = ViewModelStore()

    override fun getViewModelStore(): ViewModelStore {
        return eventViewModelStore
    }

    private val mApplicationProvider: ViewModelProvider by lazy {
        ViewModelProvider(
            ApplicationScopeViewModelProvider,
            ViewModelProvider.AndroidViewModelFactory.getInstance(EventBusInitializer.application)
        )
    }

    fun <T : ViewModel> getApplicationScopeViewModel(modelClass: Class<T>): T {
        return mApplicationProvider[modelClass]
    }
}

ViewModel内部有2个map,分别是粘性和非粘性:

internal class EventBusViewModel : ViewModel() {

    private val eventFlows: HashMap<String, MutableSharedFlow<Any>> = HashMap()
   
    private val stickyEventFlows: HashMap<String, MutableSharedFlow<Any>> = HashMap()
    ...

}

总结

站在巨人的肩膀上的同时也可以简单了解下原理。不过挺复杂的,需要下点功夫😄

kotlinx.coroutines.flow

使用

Add it in your root build.gradle at the end of repositories:

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

Step 2. Add the dependency

dependencies {
	        implementation 'com.github.biubiuqiu0:flow-event-bus:0.0.2'
	}

Step 3. Init

class MyApplication: Application() {
    override fun onCreate() {
        super.onCreate()
        EventBusInitializer.init(this)
    }
}

License

MIT License

Copyright (c) 2021 Compose-Museum

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

flow-event-bus's People

Contributors

biubiuqiu0 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.