All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cmonbaby.http.rxjava.bus.RxBus Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package com.cmonbaby.http.rxjava.bus;

import rx.Observable;
import rx.subjects.PublishSubject;
import rx.subjects.SerializedSubject;
import rx.subjects.Subject;

/**
 * 

Author: Simon *

QO: 8950764 *

Email: [email protected] *

WebSize: https://www.cmonbaby.com *

Version: 1.0.0 *

Date: 2020/12/28 *

Description: 用于替换EventBus的RxBus实现,同时用做Http响应数据的分发 */ public class RxBus { private static volatile RxBus mInstance; // PublishSubject只会把在订阅发生的时间点之后来自原始Observable的数据发射给观察者 private final Subject bus = new SerializedSubject<>(PublishSubject.create()); private RxBus() { } public static RxBus getInstance() { if (mInstance == null) { synchronized (RxBus.class) { if (mInstance == null) { mInstance = new RxBus(); } } } return mInstance; } // 发送一个新的事件 public void send(Object event) { bus.onNext(event); } // 根据传递的 eventType 类型返回特定类型(eventType)的 被观察者 public Observable take(final Class eventType) { return bus.filter(eventType::isInstance).cast(eventType); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy