Coder Social home page Coder Social logo

rxbus-2's Introduction

RxBus

Event bus based on RxJava and optimized for Android.

Usage

We recommend obtaining the single instance of bus through injection or another appropriate mechanism.

Or get singleton like following:

Bus bus = BusProvider.getInstance();

Subscribing

To subscribe to an event, declare and annotate a method with @Subscribe. The method should be public and take only a single parameter.

@Subscribe
public void onEvent(SomeEvent event) {
    // TODO: Do something
}

You can also create subscription like following:

CustomSubscriber<SomeEvent> customSubscriber = bus.obtainSubscriber(SomeEvent.class,
    new Consumer<SomeEvent>() {
        @Override
        public void accept(SomeEvent someEvent) throws Exception {
            // TODO: Do something
        }
    })
    .withFilter(new Predicate<SomeEvent>() {
        @Override
        public boolean test(SomeEvent someEvent) throws Exception {
            return "Specific message".equals(someEvent.message);
        }
    })
    .withScheduler(Schedulers.trampoline());

Register and unregister your observer

To receive events, a class instance needs to register with the bus.

bus.register(this);

The customSubscriber also needs to register with the bus.

bus.registerSubscriber(this, customSubscriber);

Remember to also call the unregister method when appropriate.

bus.unregister(this);

Publishing

To publish a new event, call the post method:

bus.post(new SomeEvent("Message"));

Add RxBus to your project

Gradle:

compile 'com.github.anadea:rxbus:1.0.1'

Maven:

<dependency>
    <groupId>com.github.anadea</groupId>
    <artifactId>rxbus</artifactId>
    <version>1.0.1</version>
    <type>pom</type>
</dependency>

ProGuard

If you are using ProGuard, add the following lines to your ProGuard configuration file.

-keepattributes *Annotation*
-keepclassmembers class ** {
    @com.anadeainc.rxbus.Subscribe public *;
}

License

Copyright (C) 2017 Anadea Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

rxbus-2's People

Watchers

James Cloos avatar github avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.