upstream, Disposable next, Class> observer) {
43 | ObjectHelper.requireNonNull(next, "next is null");
44 | if (!upstream.compareAndSet(null, next)) {
45 | next.dispose();
46 | if (upstream.get() != DisposableHelper.DISPOSED) {
47 | reportDoubleSubscription(observer);
48 | }
49 | return false;
50 | }
51 | return true;
52 | }
53 |
54 | private void reportDoubleSubscription(Class> consumer) {
55 | RxJavaPlugins.onError(new IllegalStateException(composeMessage(consumer.getName())));
56 | }
57 |
58 | public String composeMessage(String consumer) {
59 | return "It is not allowed to subscribe with a(n) " + consumer + " multiple times. "
60 | + "Please create a fresh instance of " + consumer + " and subscribe that to the target source instead.";
61 | }
62 | /**
63 | * Called once the upstream sets a Subscription on this ResourceObserver.
64 | *
65 | * You can perform initialization at this moment. The default
66 | * implementation does nothing.
67 | */
68 | protected void onStart() {
69 | }
70 |
71 | /**
72 | * Cancels the main disposable (if any) and disposes the resources associated with
73 | * this ResourceObserver (if any).
74 | *
75 | *
This method can be called before the upstream calls onSubscribe at which
76 | * case the main Disposable will be immediately disposed.
77 | */
78 | @CallSuper
79 | @Override
80 | public void dispose() {
81 | if (DisposableHelper.dispose(s)) {
82 | resources.dispose();
83 | }
84 | }
85 |
86 | /**
87 | * Returns true if this ResourceObserver has been disposed/cancelled.
88 | * @return true if this ResourceObserver has been disposed/cancelled
89 | */
90 | @Override
91 | public final boolean isDisposed() {
92 | return DisposableHelper.isDisposed(s.get());
93 | }
94 |
95 | }
96 |
--------------------------------------------------------------------------------
/SimpleRxBus/src/main/java/com/example/simplerxbus/bus/RxBusUtils.java:
--------------------------------------------------------------------------------
1 | package com.example.simplerxbus.bus;
2 |
3 | import android.support.v4.app.Fragment;
4 | import android.support.v4.app.FragmentActivity;
5 |
6 | import com.example.simplerxbus.lifecycle.HolderLifeFragment;
7 |
8 | import io.reactivex.disposables.Disposable;
9 |
10 | public class RxBusUtils {
11 | /**
12 | * @param tag
13 | * @param event
14 | * 发送普通事件
15 | */
16 | public static void post(String tag, Object event) {
17 | RxSimpleBus.getBus().sendMessage(new RxBusMessage(tag, event));
18 | }
19 |
20 | /**
21 | *
22 | * @param filter
23 | * @param receiver
24 | * @return
25 | */
26 | public static Disposable receive(String filter, RxBusReceiver