Skip to content

Commit c5268b8

Browse files
committed
#
1 parent f05f18e commit c5268b8

File tree

4 files changed

+48
-29
lines changed

4 files changed

+48
-29
lines changed

README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,36 @@
33
事件总线(基于SharedFlow)
44

55
- 支持生命周期感知
6-
- 支持自动清除空闲事件
6+
- 支持自动清除空闲事件
77

88

99
## 观察
1010

1111
```kotlin
1212

1313
/**
14-
* 观察流,进入 [minState] 时开始,脱离 [minState] 时停止
14+
* 订阅事件(EventFlow)或状态(StateFlow)
1515
*
16-
* - 对于 [StateFlow],每次进入 [minState] 都会观察一次当前值
17-
* - 对于 [EventFlow],只能收到进入 [minState] 后发射的值
18-
* */
16+
* - 对于 [EventFlow],进入 [minState] 开始订阅,脱离 [minState] 取消订阅
17+
* - 对于 [StateFlow],满足 [minState] 时立刻消费,不足 [minState] 时缓存最近的事件待再次进入时消费
18+
*
19+
*/
1920
fun <T> Flow<T>.observe(owner: LifecycleOwner, minState: Lifecycle.State = Lifecycle.State.STARTED, action: suspend (T) -> Unit)
2021

22+
2123
/**
22-
* 观察流,脱离 [minState] 后会保存最近产生的值,再次进入 [minState] 时消费
24+
* 订阅事件,满足 [minState] 时立刻消费,不足 [minState] 时缓存最近的事件待再次进入时消费
2325
*
24-
* - 对于 [StateFlow],没啥用,效果与 [observe] 没区别
25-
* - 对于 [EventFlow],每次进入 [minState] 时能收到并消费最近产生的值(如果有)
26-
*
27-
* [EventFlow] 场景:在A页订阅事件,然后进入B页发送了事件,但想回到A页时再处理,可用此方法
28-
* */
26+
* 示例:在A页观察流,然后进入B页发送了事件,回到A页消费事件
27+
*/
2928
fun <T> Flow<T>.observeLatest(owner: LifecycleOwner, minState: Lifecycle.State = Lifecycle.State.STARTED, action: suspend (T) -> Unit)
29+
30+
31+
/**
32+
* 进入 [minState] 开始订阅,脱离 [minState] 取消订阅
33+
* */
34+
fun <T> Flow<T>.observeRepeat(owner: LifecycleOwner, minState: Lifecycle.State = Lifecycle.State.STARTED, action: suspend (T) -> Unit)
3035
```
31-
3236

3337
## EventBus 使用
3438

@@ -68,7 +72,7 @@ repositories {
6872
maven { url "https://gitee.com/ezy/repo/raw/cosmo/"}
6973
}
7074
dependencies {
71-
implementation "me.reezy.cosmo:flowbus:0.8.0"
75+
implementation "me.reezy.cosmo:flowbus:0.9.0"
7276
}
7377
```
7478

flowbus/src/main/java/me/reezy/cosmo/flowbus/EventBus.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class EventBus : ViewModel() {
7878

7979

8080
fun <T> observe(eventClazz: Class<T>, owner: LifecycleOwner, eventName: String, minState: Lifecycle.State = Lifecycle.State.STARTED, action: (T) -> Unit) {
81-
get<T>("$eventClazz:$eventName", owner).observe(owner, minState, action)
81+
get<T>("$eventClazz:$eventName", owner).observeRepeat(owner, minState, action)
8282
}
8383

8484
fun <T> observeLatest(eventClazz: Class<T>, owner: LifecycleOwner, eventName: String, minState: Lifecycle.State = Lifecycle.State.STARTED, action: (T) -> Unit) {

flowbus/src/main/java/me/reezy/cosmo/flowbus/utility.kt

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,43 @@ package me.reezy.cosmo.flowbus
22

33
import androidx.lifecycle.Lifecycle
44
import androidx.lifecycle.LifecycleOwner
5+
import androidx.lifecycle.flowWithLifecycle
56
import androidx.lifecycle.lifecycleScope
67
import androidx.lifecycle.repeatOnLifecycle
8+
import kotlinx.coroutines.CoroutineScope
79
import kotlinx.coroutines.channels.BufferOverflow
810
import kotlinx.coroutines.flow.Flow
911
import kotlinx.coroutines.flow.MutableSharedFlow
1012
import kotlinx.coroutines.flow.StateFlow
13+
import kotlinx.coroutines.flow.collect
14+
import kotlinx.coroutines.flow.collectLatest
1115
import kotlinx.coroutines.launch
1216

1317

1418
@Suppress("NOTHING_TO_INLINE", "FunctionName")
1519
inline fun <T> EventFlow() = MutableSharedFlow<T>(0, 1, BufferOverflow.DROP_OLDEST)
1620

21+
1722
/**
18-
* 观察流,进入 [minState] 时开始,脱离 [minState] 时停止
23+
* 订阅事件(EventFlow)或状态(StateFlow)
1924
*
20-
* - 对于 [StateFlow],每次进入 [minState] 都会观察一次当前值
21-
* - 对于 [EventFlow],只能收到进入 [minState] 后发射的值
22-
* */
25+
* - 对于 [EventFlow],进入 [minState] 开始订阅,脱离 [minState] 取消订阅
26+
* - 对于 [StateFlow],满足 [minState] 时立刻消费,不足 [minState] 时缓存最近的事件待再次进入时消费
27+
*
28+
*/
2329
fun <T> Flow<T>.observe(owner: LifecycleOwner, minState: Lifecycle.State = Lifecycle.State.STARTED, action: suspend (T) -> Unit) {
24-
owner.lifecycleScope.launch {
25-
owner.lifecycle.repeatOnLifecycle(minState) {
26-
collect(action)
27-
}
30+
if (this is StateFlow) {
31+
observeLatest(owner, minState, action)
32+
} else {
33+
observeRepeat(owner, minState, action)
2834
}
2935
}
3036

3137
/**
32-
* 观察流,脱离 [minState] 后会保存最近产生的值,再次进入 [minState] 时消费
33-
*
34-
* - 对于 [StateFlow],没啥用,效果与 [observe] 没区别
35-
* - 对于 [EventFlow],每次进入 [minState] 时能收到并消费最近产生的值(如果有)
38+
* 订阅事件,满足 [minState] 时立刻消费,不足 [minState] 时缓存最近的事件待再次进入时消费
3639
*
37-
* [EventFlow] 场景:在A页观察流,然后进入B页发送了事件,但想回到A页时再处理,可用此方法
38-
* */
40+
* 示例:在A页观察流,然后进入B页发送了事件,回到A页消费事件
41+
*/
3942
fun <T> Flow<T>.observeLatest(owner: LifecycleOwner, minState: Lifecycle.State = Lifecycle.State.STARTED, action: suspend (T) -> Unit) {
4043
var data: T? = null
4144
owner.lifecycleScope.launch {
@@ -59,3 +62,15 @@ fun <T> Flow<T>.observeLatest(owner: LifecycleOwner, minState: Lifecycle.State =
5962
}
6063

6164

65+
/**
66+
* 进入 [minState] 开始订阅,脱离 [minState] 取消订阅
67+
* */
68+
fun <T> Flow<T>.observeRepeat(owner: LifecycleOwner, minState: Lifecycle.State = Lifecycle.State.STARTED, action: suspend (T) -> Unit) {
69+
owner.lifecycleScope.launch {
70+
owner.lifecycle.repeatOnLifecycle(minState) {
71+
collect(action)
72+
}
73+
}
74+
}
75+
76+

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ cosmoRepoPath=https://gitee.com/ezy/repo/raw/cosmo
2828
buildGradlePath=https://gitee.com/ezy/gradle/raw/cosmo
2929

3030
LIBRARY_GROUP=me.reezy.cosmo
31-
LIBRARY_VERSION=0.8.0
31+
LIBRARY_VERSION=0.9.0

0 commit comments

Comments
 (0)