Skip to content

Instantly share code, notes, and snippets.

@Hasiy
Created December 25, 2019 07:31
Show Gist options
  • Save Hasiy/97441ba9fb355c27f9cd5094af48ce66 to your computer and use it in GitHub Desktop.
Save Hasiy/97441ba9fb355c27f9cd5094af48ce66 to your computer and use it in GitHub Desktop.
数据通信总线LiveDataBus
public final class LiveDataBus {
private final Map<String, MutableLiveData<Object>> bus;
private LiveDataBus() {
bus = new HashMap<>();
}
private static class SingletonHolder {
private static final LiveDataBus DATA_BUS = new LiveDataBus();
}
public static LiveDataBus get() {
return SingletonHolder.DATA_BUS;
}
public <T> MutableLiveData<T> getChannel(String target, Class<T> type) {
if (!bus.containsKey(target)) {
bus.put(target, new MutableLiveData<>());
}
return (MutableLiveData<T>) bus.get(target);
}
public MutableLiveData<Object> getChannel(String target) {
return getChannel(target, Object.class);
}
}
@Hasiy
Copy link
Author

Hasiy commented Dec 25, 2019

LiveEventBus具体项目: https://github.com/JeremyLiao/LiveEventBus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment