MCPcopy Index your code
hub / github.com/MFlisar/RXBus

github.com/MFlisar/RXBus @1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.0 ↗ · + Follow
104 symbols 273 edges 15 files 16 documented · 15%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

RXBus Release Android Arsenal

What does it do?

  • it allows you to post events to a bus
  • it allows you to subscribe to special events whereever you want
  • it allows you to queue events until an activity is resumed (to make sure views are accessable for example)
  • it allows you to queue events as soon as activity is paused and emit events as soon soon as it is resumed
  • it's very lightweight

Gradle (via JitPack.io)

  1. add jitpack to your project's build.gradle:
repositories {
    maven { url "https://jitpack.io" }
}
  1. add the compile statement to your module's build.gradle:
dependencies {
    compile 'com.github.MFlisar:RXBus:1.0'
}

Migration

If you update from version <0.5 or version <0.9, follow this short migration guide: MIGRATION GUIDE

Usage

Content

Demo

Just check out the DemoActivity, it will show the base usage and the difference between the default and the queued RXBus

Simple usage

Use the RXBusBuilder to create subscriptions or simple observables. Just like following:

// Variant 1 - create a simple observable :
Observable<TestEvent> simpleObservable = RXBusBuilder.create(TestEvent.class).build();

// Variant 2 - subscribe with the BUILDER:
Subscription simpleSubscription = RXBusBuilder.create(TestEvent.class)
    .subscribe(new Action1<TestEvent>() {
        @Override
        public void call(TestEvent event) {
            // handle event...

            // event MUST have been send with either of following:
            // RXBus.get().sendEvent(new TestEvent()); => class bound bus usage
            // RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1, true); => key bound bus usage, with sendToDefaultBusAsWell = true, which will result in that all class bound observers (like this one) retrieve this event as well
        }
    });
Sending an event
// Send an event to the bus - all observers that observe this class WITHOUT a key will receive this event
RXBus.get().sendEvent(new TestEvent());
// Send an event to the bus - only observers that observe the class AND key will receive this event
RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1);
// Send an event to the bus - all observers that either observe the class or the class AND key will receive this event
RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1, true);
Advanced usage - QUEUING AND BINDING

You can use this library to subscribe to events and only get them when your activity is resumed, so that you can be sure views are available, for example. Just like following:

RXBusBuilder.create(TestEvent.class)
    // this enables the queuing mode! Passed object must implement IRXBusQueue interface, see the demo app for an example
    .withQueuing(rxBusQueue)
    .withOnNext(new Action1<TestEvent>() {
        @Override
        public void call(TestEvent s) {
            // activity IS resumed, you can safely update your UI for example

            // event MUST have been send with either of following:
            // RXBus.get().sendEvent(new TestEvent()); => class bound bus usage
            // RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1, true); => key bound bus usage, with sendToDefaultBusAsWell = true, which will result in that all class bound observers (like this one) retrieve this event as well
        }
    })
    .buildSubscription();

Additionally, you can bind the subscription to an object and afterwards call RXSubscriptionManager.unsubscribe(boundObject); to unsubcribe ANY subscription that was bound to boundObject just like following:

// boundObject... can be for example your activity
RXBusBuilder.create(TestEvent.class)
    // this enables the queuing mode! Passed object must implement IRXBusQueue interface, see the demo app for an example
    .withQueuing(rxBusQueue)
    .withBound(boundObject)
    .subscribe(new Action1<Object>() {
        @Override
        public void call(TestEvent s) {
            // activity IS resumed, you can safely update your UI for example

            // event MUST have been send with either of following:
            // RXBus.get().sendEvent(new TestEvent()); => class bound bus usage
            // RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1, true); => key bound bus usage, with sendToDefaultBusAsWell = true, which will result in that all class bound observers (like this one) retrieve this event as well
        }
    });
// call this for example in your activities onDestroy or whereever appropriate to unsubscribe ALL subscriptions at once that are bound to the boundOBject
RXSubscriptionManager.unsubscribe(boundObject);
Advanced usage - KEYS

You can use this library to subscribe to events of a typ and ONLY get them when it was send to the bus with a special key (and only when your activity is resumed, as this example shows via .withQueuing()), so that you can distinct event subscriptions of the same class based on a key (the key can be an Integer or a String). Just like following:

RXBusBuilder.create(TestEvent.class)
    // this enables the binding to the key
    .withKey(R.id.observer_key_1) // you can provide multiple keys as well
    .withQueuing(rxBusQueue)
    .withBound(boundObject)
    .subscribe(new Action1<String>() {
        @Override
        public void call(TestEvent event) {
            // activity IS resumed, you can safely update your UI for example

            // event MUST have been with either of those:
            // RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1); => key bound bus usage, class bound observers WON't retrieve this event as well!
            // RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1, true); => key bound bus usage, with sendToDefaultBusAsWell = true, resulting in class bound observers WILL retrieve this event as well!
        }
    });
Advanced usage

You can pass in a Observable.Transformer to transform the observed event to whatever you want!

Observable.Transformer<TestEvent, TestEventTransformed> transformer = new Observable.Transformer<TestEvent, TestEventTransformed>() {
    @Override
    public Observable<TestEventTransformed> call(Observable<TestEvent> observable) {
        return observable
                .map(new Func1<TestEvent, TestEventTransformed>() {
                    @Override
                    public TestEventTransformed call(TestEvent event) {
                        return event.transform();
                    }
                });
    }
};
RXBusBuilder.create(TestEvent.class)
    .withQueuing(rxBusQueue)
    .withBound(boundObject)
    .subscribe(new Action1<Object>() {
        @Override
        public void call(String s) {
        }
    }, transformer);
Helper class - RXSubscriptionManager

This class helps to bind subscriptions to objects and offers an easy way to unsubscribe all subscriptions that are bound to an object at once. You can simply use is directly with the RXBusBuilder via the RXBusBuilder.withBound(boundObject). This will automatically add the subscription to the RXSubscriptionManager when you call `RXBusBuilder.subscribe(...).

Directly

Subscription subscription = RXBusBuilder.create(...).build();
RXSubscriptionManager.addSubscription(activity, subscription);

RXBusBuilder

RXBusBuilder.create(...).withBound(activity);

Now you only have to make sure to unsubscribe again like following:

RXSubscriptionManager.unsubscribe(activity);

This will remove ANY subscription that is bound to activity and therefore this can be used in your activity's onDestroy method to make sure ALL subscriptions are unsubscribed at once and that you don't leak the activity.

Credits

The RxValve class is from this gist: https://gist.github.com/akarnokd/1c54e5a4f64f9b1e46bdcf62b4222f08

Extension points exported contracts — how you extend this code

IRXBusQueue (Interface)
Created by Michael on 22.04.2016. [2 implementers]
lib/src/main/java/com/michaelflisar/rxbus/interfaces/IRXBusQueue.java

Core symbols most depended-on inside this repo

get
called by 16
lib/src/main/java/com/michaelflisar/rxbus/RXBus.java
unsubscribe
called by 12
lib/src/main/java/com/michaelflisar/rxbus/rx/RXSubscriptionManager.java
create
called by 11
lib/src/main/java/com/michaelflisar/rxbus/RXBusBuilder.java
sendEvent
called by 9
lib/src/main/java/com/michaelflisar/rxbus/RXBus.java
safetyQueueCheck
called by 9
lib/src/main/java/com/michaelflisar/rxbus/rx/RXUtil.java
getLogMessage
called by 8
demo/src/main/java/com/michaelflisar/rxbus/demo/DemoActivity.java
subscribe
called by 7
lib/src/main/java/com/michaelflisar/rxbus/RXBusBuilder.java
get
called by 7
lib/src/main/java/com/michaelflisar/rxbus/rx/RXSubscriptionManager.java

Shape

Method 87
Class 15
Enum 1
Interface 1

Languages

Java100%

Modules by API surface

lib/src/main/java/com/michaelflisar/rxbus/rx/RxValve.java18 symbols
lib/src/main/java/com/michaelflisar/rxbus/RXBusBuilder.java13 symbols
demo/src/main/java/com/michaelflisar/rxbus/demo/DemoActivity.java12 symbols
lib/src/main/java/com/michaelflisar/rxbus/rx/RXSubscriptionUtil.java8 symbols
lib/src/main/java/com/michaelflisar/rxbus/rx/RXBusUtil.java8 symbols
lib/src/main/java/com/michaelflisar/rxbus/RXBusUtil.java8 symbols
demo/src/main/java/com/michaelflisar/rxbus/demo/PauseAwareActivity.java7 symbols
lib/src/main/java/com/michaelflisar/rxbus/rx/RXSubscriptionManager.java6 symbols
lib/src/main/java/com/michaelflisar/rxbus/rx/RXUtil.java5 symbols
lib/src/main/java/com/michaelflisar/rxbus/RXBus.java5 symbols
lib/src/main/java/com/michaelflisar/rxbus/rx/RXQueueKey.java4 symbols
lib/src/main/java/com/michaelflisar/rxbus/interfaces/IRXBusQueue.java3 symbols

For agents

$ claude mcp add RXBus \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page