Skip to content

Commit

Permalink
优先级功能支持
Browse files Browse the repository at this point in the history
1、数字越大优先级越高
2、优先级高的先收到消息
3、引用类型的数据可能被高优先级的改变
  • Loading branch information
codyer committed Sep 11, 2022
1 parent 8b0681c commit 2bc968b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 21 deletions.
64 changes: 54 additions & 10 deletions bus/core/src/main/java/cody/bus/ActiveLiveDataWrapper.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
/*
* ************************************************************
* 文件:ActiveLiveDataWrapper.java 模块:ElegantBus.bus.core 项目:ElegantBus
* 当前修改时间:2021年08月15日 01:18:42
* 上次修改时间:2021年08月14日 23:52:48
* 当前修改时间:2022年09月11日 22:06:29
* 上次修改时间:2022年09月11日 21:54:45
* 作者:Cody.yi https://github.com/codyer
*
* 描述:ElegantBus.bus.core
* Copyright (c) 2021
* Copyright (c) 2022
* ************************************************************
*/

package cody.bus;

import android.os.Looper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -35,14 +40,17 @@ public class ActiveLiveDataWrapper<T> implements LiveDataWrapper<T> {
private int mSequence = 0;
private int mStickySequence = -1;
private EventWrapper mEventWrapper;
private final List<ObserverWrapper<T>> mObserverWrappers;
private final MutableLiveData<ValueWrapper<T>> mMutableLiveData;

ActiveLiveDataWrapper() {
mObserverWrappers = new ArrayList<>();
mMutableLiveData = new MutableLiveData<>();
}

ActiveLiveDataWrapper(final EventWrapper eventWrapper) {
mEventWrapper = eventWrapper;
mObserverWrappers = new ArrayList<>();
mMutableLiveData = new MutableLiveData<>();
}

Expand Down Expand Up @@ -113,10 +121,11 @@ public void postToCurrentProcess(@NonNull T value) {

/**
* 跨进程的粘性事件支持,新建进程时,需要初始值时调用,其他情况不要使用
*
* @param value 需要更新的值
*/
@Override
public void postStickyToCurrentProcess(@NonNull T value){
public void postStickyToCurrentProcess(@NonNull T value) {
checkThread(() -> mMutableLiveData.setValue(new ValueWrapper<>(value, 0)));
}

Expand All @@ -141,7 +150,7 @@ public void resetSticky() {
* 重置 Sticky 序列,确保这之后添加的监听,之前的值不回调
*/
@Override
public void resetStickyToCurrentProcess(){
public void resetStickyToCurrentProcess() {
mStickySequence = mSequence;
mSequence++;
}
Expand All @@ -165,7 +174,10 @@ public void setValue(@NonNull T value) {
*/
@Override
public void removeObserver(@NonNull ObserverWrapper<T> observerWrapper) {
checkThread(() -> mMutableLiveData.removeObserver(filterObserver(observerWrapper)));
checkThread(() -> {
mObserverWrappers.remove(observerWrapper);
mMutableLiveData.removeObserver(observerWrapper.observer);
});
}

/**
Expand All @@ -175,7 +187,16 @@ public void removeObserver(@NonNull ObserverWrapper<T> observerWrapper) {
*/
@Override
public void removeObservers(@NonNull LifecycleOwner owner) {
checkThread(() -> mMutableLiveData.removeObservers(owner));
checkThread(() -> {
Iterator<ObserverWrapper<T>> iterator = mObserverWrappers.iterator();
while (iterator.hasNext()) {
ObserverWrapper<T> next = iterator.next();
if (next.owner == owner) {
iterator.remove();
}
}
mMutableLiveData.removeObservers(owner);
});
}

/**
Expand All @@ -185,8 +206,7 @@ public void removeObservers(@NonNull LifecycleOwner owner) {
*/
@Override
public void observeForever(@NonNull final ObserverWrapper<T> observerWrapper) {
observerWrapper.sequence = observerWrapper.sticky ? mStickySequence : mSequence++;
checkThread(() -> mMutableLiveData.observeForever(filterObserver(observerWrapper)));
checkThread(() -> insertObserver(null, observerWrapper));
}

/**
Expand All @@ -212,8 +232,32 @@ public void observeSticky(@NonNull LifecycleOwner owner, @NonNull ObserverWrappe
*/
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull ObserverWrapper<T> observerWrapper) {
checkThread(() -> insertObserver(owner, observerWrapper));
}

private void insertObserver(LifecycleOwner owner, @NonNull ObserverWrapper<T> observerWrapper) {
observerWrapper.sequence = observerWrapper.sticky ? mStickySequence : mSequence++;
checkThread(() -> mMutableLiveData.observe(owner, filterObserver(observerWrapper)));
observerWrapper.owner = owner;
Observer<ValueWrapper<T>> observer = filterObserver(observerWrapper);
int i = mObserverWrappers.size();
for (; i > 0; i--) {
ObserverWrapper<T> previous = mObserverWrappers.get(i - 1);
if (observerWrapper.priority > previous.priority) {
previous.sequence = mSequence++;
mMutableLiveData.removeObserver(previous.observer);
} else {
break;
}
}
mObserverWrappers.add(i, observerWrapper);
for (; i < mObserverWrappers.size(); i++) {
ObserverWrapper<T> next = mObserverWrappers.get(i);
if (next.owner == null) {
mMutableLiveData.observeForever(next.observer);
} else {
mMutableLiveData.observe(next.owner, next.observer);
}
}
}

/**
Expand Down
47 changes: 40 additions & 7 deletions bus/core/src/main/java/cody/bus/ObserverWrapper.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/*
* ************************************************************
* 文件:ObserverWrapper.java 模块:core 项目:ElegantBus
* 当前修改时间:2020年06月15日 00:35:24
* 上次修改时间:2020年06月15日 00:30:33
* 文件:ObserverWrapper.java 模块:ElegantBus.bus.core 项目:ElegantBus
* 当前修改时间:2022年09月11日 21:47:29
* 上次修改时间:2022年09月10日 23:04:39
* 作者:Cody.yi https://github.com/codyer
*
* 描述:core
* Copyright (c) 2020
* 描述:ElegantBus.bus.core
* Copyright (c) 2022
* ************************************************************
*/

package cody.bus;


import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;

/**
Expand All @@ -22,9 +23,12 @@
*/
@SuppressWarnings("unused")
public abstract class ObserverWrapper<T> {
LifecycleOwner owner;// 没有owner就是forever
Observer<ValueWrapper<T>> observer;
// 每个观察者都记录自己序号,只有在进入观察状态之后产生的数据才通知到观察者
int sequence;
Observer<ValueWrapper<T>> observer;
// 优先级高的先收到事件,默认优先级为0,数字越大优先级越高
int priority = 0;
// 默认不是粘性事件,不会收到监听之前发送的事件
boolean sticky = false;
// 默认在主线程监听
Expand All @@ -33,13 +37,22 @@ public abstract class ObserverWrapper<T> {
public ObserverWrapper() {
}


/**
* 构造函数
* @param priority 优先级,数字越大优先级越高
*/
public ObserverWrapper(final int priority) {
this(priority, false, true);
}

/**
* 构造函数
*
* @param sticky 是否粘性事件
*/
public ObserverWrapper(final boolean sticky) {
this.sticky = sticky;
this(sticky, true);
}

/**
Expand All @@ -49,6 +62,26 @@ public ObserverWrapper(final boolean sticky) {
* @param uiThread 是否在UI线程监听回调
*/
public ObserverWrapper(final boolean sticky, final boolean uiThread) {
this(0, sticky, uiThread);
}

/**
* 构造函数
* @param priority 优先级,数字越大优先级越高
* @param sticky 是否粘性事件
*/
public ObserverWrapper(final int priority, final boolean sticky) {
this(priority, sticky, true);
}

/**
* 构造函数
* @param priority 优先级,数字越大优先级越高
* @param sticky 是否粘性事件
* @param uiThread 是否在UI线程监听回调
*/
public ObserverWrapper(final int priority, final boolean sticky, final boolean uiThread) {
this.priority = priority;
this.sticky = sticky;
this.uiThread = uiThread;
}
Expand Down
8 changes: 4 additions & 4 deletions config-versions.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* ************************************************************
* 文件:config-versions.gradle 模块:ElegantBus 项目:ElegantBus
* 当前修改时间:2021年08月15日 17:27:55
* 上次修改时间:2021年08月15日 15:44:12
* 当前修改时间:2022年09月11日 22:06:29
* 上次修改时间:2022年09月11日 21:54:45
* 作者:Cody.yi https://github.com/codyer
*
* 描述:ElegantBus
* Copyright (c) 2021
* Copyright (c) 2022
* ************************************************************
*/

Expand All @@ -17,7 +17,7 @@ ext {
targetSdkVersion = 28
compileSdkVersion = 28
supportLibraryVersion = '28.0.0'
componentVersion = '2.2.3'
componentVersion = '2.3.9'

//libraries dependencies
dependencies = [
Expand Down

0 comments on commit 2bc968b

Please sign in to comment.