GithubHelp home page GithubHelp logo

com13117340768 / liveeventbus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lwj1994/liveeventbus

0.0 0.0 0.0 278 KB

LiveData 实现的事件总线,关联 Android 生命周期,绝对安全。

Java 37.75% Kotlin 62.25%

liveeventbus's Introduction

前言

本库是使用 LiveData 实现的 EventBus,LiveEventBus 就是一种优雅的实现方式,没有修改任何源码,也没有任何反射。 LiveEventBus 的优点:

  • 类型安全
  • 没有反射,不修改源码,无需担心 LiveData 版本问题
  • 基于 LiveData 与 Lifecycle 紧密联系,可以自由选择是否关联 Lifecycle 的周期
  • 可选是否支持粘性事件
  • 只有 2 个类十分小巧,你可以直接 copy 去使用
  • 支持直接发射基本数据类型

java 版本: https://github.com/lwj1994/LiveEventBus/blob/main/app/src/main/java/com/lwjlol/liveeventbus/javaversion/LiveEventBus.java

使用方法

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

dependencies {
    implementation 'com.github.lwj1994:LiveEventBus:${latestVersion}'
}
//注册接收事件:
LiveEventBus.instance.on(Event::class.java).observe(lifecycleOwner) {
    //
}


// 发送事件, sticky:是否粘性,默认 true
// forever: 是否一直观察事件,默认 false
// post:是否使用 liveData.postValue 发送,默认会根据是否主线程识别:子线程用 post 主线程用 setValue
LiveEventBus.instance.send(Event(), sticty = true, forever = false

支持直接发射基本数据类型

LiveEventBus.instance.send("key1",1)
LiveEventBus.instance.send("key2",false)
LiveEventBus.instance.send("key3",1L)
LiveEventBus.instance.send("key4",1.0)
LiveEventBus.instance.send("key5",1F)
LiveEventBus.instance.send("key6",'A')
LiveEventBus.instance.send("key7","text")


LiveEventBus.instance.on("key1").observeInt(this){

}

LiveEventBus.instance.on("key2").observeBoolean(this){

}
LiveEventBus.instance.on("key3").observeLong(this){

}
LiveEventBus.instance.on("key4").observeDouble(this){

}
LiveEventBus.instance.on("key5").observeFloat(this){

}
LiveEventBus.instance.on("key6").observeChar(this){

}
LiveEventBus.instance.on("key7").observeString(this){

}

自定义 ownerKey

你可以根据需求,传入自定义的 ownerKey,该库会根据 ownerKey 来决定是否接收事件:一个事件只能被一个 ownerKey 消费一次。 默认 ownerKey = lifecycleOwner::class.qualifiedName

//接收事件:
LiveEventBus.instance.on(Event::class.java).observe(lifecycleOwner, ownerKey) {
    //
}

原理

LiveEventBus 主要内部实现依赖于 EventLiveDataEventLiveData 继承自 LiveData,可以单独拿出来使用。

避免在 onActive 时响应回调的原理很简单,重写了 observe,给 LifecycleOwner 配上一个 ownerKey 参数。

  • 判断当前的 ownerKey 是否已经注册过来实现是否是粘性
  • 为每一个 ownerKey 单独保存 value,消费完一个事件立马置为 UNSET 状态,判断当前的 ownerKey 是否是 UNSET 状态决定是否响应回调
fun observe(
        owner: LifecycleOwner,
        ownerKey: String = getKey(owner),
        observer: Observer<in T>
    )

ownerKey 的默认实现是当前 LifecycleOwner 的全路径类名,可以根据实际需求传入 ownerKey:

fun getKey(owner: LifecycleOwner) = "${owner::class.qualifiedName}"

liveeventbus's People

Contributors

lwj1994 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.