Class RxBus
This implementation ensures thread-safety and provides a robust mechanism for error handling.
It includes:
- Posting events to the bus.
- Subscribing to events of a specific type.
- Subscribing to events of a specific type with a filter on the execution ID.
- Exposing the bus as an Observable to allow further subscriptions.
Additionally, error handling is provided in the subscription process to log any issues.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic RxBusvoidPosts an event to the bus, notifying all subscribers that a new event has occurred.<T> io.reactivex.rxjava3.disposables.DisposableRegisters a subscriber for a specific event type.<T extends Event>
io.reactivex.rxjava3.disposables.DisposableregisterOnExecutionId(Class<T> eventClass, long executionId, io.reactivex.rxjava3.functions.Consumer<? super Event> onNext) Registers a subscriber for a specific event type and execution ID.io.reactivex.rxjava3.core.Observable<Object> Exposes the event bus as an Observable, allowing subscribers to listen to all events.
-
Constructor Details
-
RxBus
public RxBus()
-
-
Method Details
-
getInstance
-
post
Posts an event to the bus, notifying all subscribers that a new event has occurred.This method is the producer of the bus, and the event can be consumed by any active subscriber that has registered for the event type.
- Parameters:
event- The event to post to the bus.
-
register
public <T> io.reactivex.rxjava3.disposables.Disposable register(Class<T> eventClass, io.reactivex.rxjava3.functions.Consumer<T> onNext) Registers a subscriber for a specific event type. The subscriber will receive events of the specified class type.Subscribers are notified asynchronously when an event of the given class type is posted to the bus. The method ensures that only events of the requested type are passed to the subscriber.
- Type Parameters:
T- The type of event to subscribe to.- Parameters:
eventClass- The class type of the event.onNext- The action to perform when the event is received.- Returns:
- A Disposable object that can be used to unsubscribe from the bus.
-
registerOnExecutionId
public <T extends Event> io.reactivex.rxjava3.disposables.Disposable registerOnExecutionId(Class<T> eventClass, long executionId, io.reactivex.rxjava3.functions.Consumer<? super Event> onNext) Registers a subscriber for a specific event type and execution ID. The subscriber will only receive events of the specified class type that match the given execution ID.This method is useful for filtering events by a specific execution context, ensuring that subscribers only receive relevant events related to their execution ID.
- Type Parameters:
T- The type of event to subscribe to.- Parameters:
eventClass- The class type of the event.executionId- The execution ID to filter the events.onNext- The action to perform when the event is received.- Returns:
- A Disposable object that can be used to unsubscribe from the bus.
-
toObservable
Exposes the event bus as an Observable, allowing subscribers to listen to all events.This is useful if you want to observe all events on the bus without filtering by event type.
- Returns:
- An Observable that emits all events posted to the bus.
-