Code Monkey home page Code Monkey logo

tinybus's Introduction

This project is @deprecated in favor of RxJava. It offers the same event-driven programming model as TinyBus, but it's more capable and offers better control of threading.


tinybus

TinyBus is the faster implementation of Otto event bus with additional features you missed.

Build Status

TinyBus is

  • tiny (~ 26K jar)
  • fast (optimized for startup and event dispatching)
  • well tested (> 90 junit tests)
  • annotation based (no requirements on method names, no interfaces to implement)

TinyBus API in a nutshell

  • @Subscribe annotates event handler methods running in the main thread.
  • @Subscribe(mode=Mode.Background) annotates event handler methods running in a background thread.
  • @Subscribe(mode=Mode.Background, queue="web") annotates event handler methods running in a serialized background queue with given name. You can have as many queues as you want.
  • @Produce annotates methods returning most recent events (aka sticky events).
  • Bus.register(Object) and Bus.unregister(Object) register and unregister objects with annotated subscriber and producer methods.
  • Bus.hasRegistered(Object) checks, whether given object is already registered.
  • Bus.post(Object) posts given event object to all registered subscribers.
  • Bus.postDelayed(Object, long) and Bus.cancelDelayed(Class) schedules single event delivery for later in time and cancels it.

For a more detailed example check out Getting started step-by-step guide or example application.

Performance reference tests

tinybus

Executed on Nexus 5 device (Android 5.0.1, ART, screen off).

TinyBus extensions (still in 'β')

Extensions is a unique feature of TinyBus. With it you can easily subscribe to commonly used events like battery level, connectivity change, phone shake event or even standard Android broadcast Intents. Here is a short example.

public class MainActivity extends Activity {
    private TinyBus mBus;
        
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // get bus instance 
        mBus = TinyBus.from(this)
        
        if (savedInstanceState == null) {
            // Note: ShakeEventWire stays wired when activity is re-created
            //       on configuration change. That's why we register is 
            //       only once inside if-statement.

            // wire device shake event provider
            mBus.wire(new ShakeEventWire());
        }
    }
    
    @Override
    protected void onStart() {
        super.onStart();
	    mBus.register(this);
	}
	
    @Override
    protected void onStop() {
        mBus.unregister(this);
        super.onStop();
    }
    
    @Subscribe
    public void onShakeEvent(ShakeEvent event) {
        // device has been shaken
    }
}

More detailed usage example can be found in example application.

Gradle dependencies

For pure event bus implementation

dependencies {
    compile 'de.halfbit:tinybus:3.0.2'
}

For event bus with extensions

dependencies {
    compile 'de.halfbit:tinybus:3.0.2'
    compile 'de.halfbit:tinybus-extensions:3.0.2'
}

ProGuard configuration

If you use Gradle build, then you don't need to configure anything, because it will use proper configuration already delivered with Android library archive. Otherwise you can use the configuration below:

-keepclassmembers, allowobfuscation class ** {
    @de.halfbit.tinybus.Subscribe public *;
    @de.halfbit.tinybus.Produce public *;
}

Used by

License

Copyright (c) 2014-2015 Sergej Shafarenka, halfbit.de
Copyright (C) 2012 Square, Inc.
Copyright (C) 2007 The Guava Authors

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.

tinybus's People

Contributors

intrications avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tinybus's Issues

java.lang.RuntimeException

Hello,

In our app we are seeing this java.lang.RuntimeException once in a while.
Unfortunately, there are nothing in the stack trace which indicates where in our code or when this happens.

Version: de.halfbit:tinybus:3.0.2

java.lang.RuntimeException
de.halfbit.tinybus.TinyBus.handleExceptionOnEventDispatch(SourceFile:329)
de.halfbit.tinybus.TinyBus.processQueue(SourceFile:383)
de.halfbit.tinybus.TinyBus$TinyBusImpl.onPostFromBackground(SourceFile:529)
de.halfbit.tinybus.impl.Task.run(SourceFile:87)
android.os.Handler.handleCallback(Handler.java:739)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:158)
android.app.ActivityThread.main(ActivityThread.java:7229)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.reflect.Method.invoke(Native Method)
de.halfbit.tinybus.TinyBus$TinyBusImpl.dispatchEvent(SourceFile:460)
de.halfbit.tinybus.TinyBus.processQueue(SourceFile:380)
de.halfbit.tinybus.TinyBus$TinyBusImpl.onPostFromBackground(SourceFile:529)
de.halfbit.tinybus.impl.Task.run(SourceFile:87)
android.os.Handler.handleCallback(Handler.java:739)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:158)
android.app.ActivityThread.main(ActivityThread.java:7229)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

subclasses do not receive events?

Hi there,

it seems that subclasses won't receive an Event when overriding a superclass method annotated with [at]Subscribe. Could you comment on that? Thanks.

bout stickyevents?

when certain parts of the app aren't in the foreground,
can an event be received when it eventually makes its way to the foreground?

thanks for your work!

Remove allowBackup="false" from TinyBus manifest

Any specific reason you are disallowing backup with this library? Right now to include this library and allow backup for an app you have to use tools:replace="android:allowBackup" in your manifest which is annoying.

Thanks!

Could not find at Maven Central

Tinybus looks really promising but I couldn't find it on Maven Central :). It would be great when we could add this library via a dependency in Gradle to our projects.

Calling unregister() on an unregistered object throws NPE

Hello!

Would be nice to fail silently or at least to throw an IllegalStateException?

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void de.halfbit.tinybus.impl.ObjectsMeta.unregisterFromReceivers(java.lang.Object, java.util.HashMap)' on a null object reference

"Performance comparison tests:" EventBus results

Judging from the results, you are using an outdated EventBus Version. Also please note that you can configure EventBus to disable features tinyBus is not offering to have a fairer comparison: e.g. ignoring hierarchy, no sending of "no-subsriber" events.

GreenRobot comparison

Tinybus is only compared to Otto, it would be very interesting to also compare it to greenrobot/Eventbus which seems to fit the same niche.

Update proguard documentation

In the readme, the proguard configuration lists:

-keepclassmembers class ** {
    @de.halfbit.tinybus.Subscribe public *;
    @de.halfbit.tinybus.Produce public *;
}

Should it not be 'com' instead of 'de':

-keepclassmembers class ** {
    @com.halfbit.tinybus.Subscribe public *;
    @com.halfbit.tinybus.Produce public *;
}

TinyBus can't deliver large objects?

Consider the following event class:

public final class ParseEvent extends Event {

private User user;

public ParseEvent(User user) {
    this.type = Type.ON_USER_PARSE_COMPLETE;
    this.user = user;
}

public User user() {
    return user;
}
}

Calling post() with an instance of this class does not trigger anything. The User object is quite large with a lot of fields, could this be the issue?

Any reason for such high minimum sdk?

Edit: Sorry i didn't read the closed issues and now i know why you use minimum api 14, looks like i will have to stop supporting api <14 users or just use something else :\ .

Hi there i just integrated your awesome library using gradle but it seems your library requires api minimum 14 any particular reason for this?

I ask this because i want to integrate your tiny bus into my app which supports gingerbread devices (API 9)

Question regarding TinyBus limits of ussage

Hi.
I found your library very intersesting and I gave it a try in my custom Views. For now it seems to work, much better than Otto which threw null pointers and sometimes it worked.
I wanted to ask you, can I use it in Long running Tasks, in Services for example, or Broadcast receivers and Volley HTTP call backs.
Thx.

TinyBus minSDK

I implemented an application context bus. It works from jellybean and higher api levels, However on gingerbread api lvl 10 (2.3.6) when initialising the bus in my application context.

mBus = TinyBus.from(this);

it throws a java.lang.NoClassDefFoundError, Logcat is as below.

01-07 23:07:59.283 1421-1421/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.halfbit.tinybus.TinyBusDepot
at com.halfbit.tinybus.TinyBus.from(TinyBus.java:113)
at com.kenshin.repapp.App.onCreate(App.java:55)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3272)
at android.app.ActivityThread.access$2200(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

I checked the source code the manifests states minsdk 14 but the build.gradle states minsdk 10. And so i found out that the problem is that TinyBusDepot uses ActivityLifecycleCallbacks which is only available in api lvl 14 and above. If i am not wrong build.gradle will always override manifests. So i recommend updating the build.gradle file to minsdk 14 and perhaps indicating in the readme the min sdk level for TinyBus.

Exception on activity start

Happens sporadically on Activity start.

Unexpected error occured at Sunday, 6 September 2015 14:16

java.lang.RuntimeException: Unable to start activity ComponentInfo{net.nakvic.habs/net.nakvic.habs.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'de.halfbit.tinybus.impl.TinyBusDepot$LifecycleCallbacks de.halfbit.tinybus.TinyBus.getLifecycleCallbacks()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'de.halfbit.tinybus.impl.TinyBusDepot$LifecycleCallbacks de.halfbit.tinybus.TinyBus.getLifecycleCallbacks()' on a null object reference
at de.halfbit.tinybus.impl.TinyBusDepot.onActivityCreated(TinyBusDepot.java:124)
at android.app.Application.dispatchActivityCreated(Application.java:189)
at android.app.Activity.onCreate(Activity.java:937)
at android.support.v4.app.BaseFragmentActivityDonut.onCreate(BaseFragmentActivityDonut.java:39)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:242)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:61)
at net.nakvic.habs.BaseActivity.onCreate(BaseActivity.java:51)
at net.nakvic.habs.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
... 10 more

Performance improvements using an annotation processor

Hi @beworker,

congratulation for this project and being referenced on Android Weekly.

I looked at your source code and it looks like you are using a lot of reflection to discover the methods that are annotated to subscribe to or produce events (in https://github.com/beworker/tinybus/blob/master/tinybus/src/com/halfbit/tinybus/ObjectMeta.java).

You could actually get some performance gains by using an annotation processor to detect all such methods at compile time and get references to them. Thus you would not need to scan the classes, you would know in advance where such methods are in a given class.

You can find an example of such an annotation processor in :

if you need more high level explanations, please refer to these slides :
https://speakerdeck.com/stephanenicolas/blender-boosting-guice-with-annotation-processing

Tinybus Android Studio Plugin

Hi,

I've taken over a project at work that use Tinybus events extensively, but I find it hard to keep track of the events and see where they are catched.
I've used Otto before and it has a Android Studio plugin that makes it easy to keep track of the events.
Is there something similar for Tinybus?

java.lang.NoClassDefFoundError: android/os/PersistableBundle crash

After some recent Play Services update this crash has become very common for any app using any kind of Event Bus implementation on pre-21 devices. Happens when trying to call register(Obj).
The only solution I've seen so far is not using anything containing the PersistableBundle, which I don't use anyway and still have this issue.
Do you have any ideas on that?

Not enough information in error message

It would be nice to see both producers: registered and attempted to get registered.

Caused by: java.lang.IllegalArgumentException: Unable to register producer, because another producer is already registered, TopicsLoader{18ee7275 id=-1666337187}
at de.halfbit.tinybus.impl.ObjectsMeta.registerAtProducers(ObjectsMeta.java:231)
at de.halfbit.tinybus.TinyBus.processQueue(TinyBus.java:350)
at de.halfbit.tinybus.TinyBus.post(TinyBus.java:205)
at de.halfbit.tinybus.wires.ConnectivityWire.postEvent(ConnectivityWire.java:169)
at de.halfbit.tinybus.wires.ConnectivityWire$1.onReceive(ConnectivityWire.java:112)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)

Android minSdkVersion

I'm thinking about start using TinyBus in one of my apps, but sadly I still have to maintain compatibility with Gingerbread. Is there any particular reason/feature why you have set minSdkVersion to 14 on the main lib ?
Thanks

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.