Faster implementation of Otto event bus with additional features you missed.
- Background processing queues
- Delayed events
- 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)
@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)
andBus.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)
andBus.cancelDelayed(Class)
schedules event delivery for later in time and cancels it.
// 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.
Executed on Nexus 5 device (Android 5.0.1, ART, screen off).
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.
- git clone git@github.com:beworker/tinybus.git
- cd /tinybus
- gradle build (or ant release)
- cd /tinybus-tests
- ant test
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.+'
}
-keepclassmembers class ** {
@com.halfbit.tinybus.Subscribe public *;
@com.halfbit.tinybus.Produce public *;
}
-keepclassmembers enum com.halfbit.tinybus.Subscribe$Mode {
public *;
}
-keepclassmembers class ** {
@de.halfbit.tinybus.Subscribe public *;
@de.halfbit.tinybus.Produce public *;
}
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.