xwc010 / tinybus

Simple, lightweight and fast event bus tailored for Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tinybus

Flattr this git repo

Faster implementation of Otto event bus with additional features you missed.

Version 3.0 (in test)

  • Background processing queues
  • Delayed events

TinyBus is

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

TinyBus API in 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.
  • @Produce annotates methods delivering most recent events (aka sticky events).
  • Bus.register(Object) and Bus.unregister(Object) register and unregister objects with subscriber and producer methods.
  • Bus.hasRegistered(Object) checks, whether given object is registered.
  • Bus.post(Object) posts given event object.
  • Bus.postDelayed(Object, long) and Bus.cancelDelayed(Class) schedules event delivery for later in time and cancels it.

TinyBus quick start

// 1. Create an event class
public class LoadingEvent { /* with some fields, if required */ }
   
// 2. Prepare event callback method inside Activity, Fragment or any other class
@Subscribe
public void onEvent(LoadingEvent event) { /* event handler logic */ }
// ... and register it in the bus
bus.register(this);
   
// 3. post event to all registered subscribers
bus.post(new LoadingEvent());

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 Bus mBus;
        
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // get bus instance and wire device shake event
        mBus = TinyBus.from(this).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 example can be found in example application.

Build

  1. git clone git@github.com:beworker/tinybus.git
  2. cd /tinybus
  3. gradle build (or ant release)

Execute JUnit tests

  1. cd /tinybus-tests
  2. ant test

Gradle dependencies

For pure event bus implementation

dependencies {
    compile 'de.halfbit:tinybus:2.1.+'
}

For event bus with extensions

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

Proguard configuration (Version 2.1.x and below)

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

-keepclassmembers enum com.halfbit.tinybus.Subscribe$Mode {
	public *;
}

Proguard configuration (Version 3)

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

Used in

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.

About

Simple, lightweight and fast event bus tailored for Android

License:Apache License 2.0


Languages

Language:Java 100.0%