Simple event bus


Source link: https://github.com/Edeqa/EventBus

EventBus

Simple event bus.

How to add

Gradle

Step 1. Add the JitPack repository in your root build.gradle at the end of repositories:

allprojects {

  repositories {

maven {
 url "https://jitpack.io" 
}

  
}
 
}
 

Step 2. Add the dependency in the app's build.gradle:

dependencies {

  compile 'com.github.edeqa:eventbus:1.0' 
}
 

Maven

Step 1. Add the JitPack repository to your build file:

<repositories>
  <repository>

<id>jitpack.io</id>

<url>https://jitpack.io</url>
  </repository> </repositories> 

Step 2. Add the dependency:

<dependency>
  <groupId>com.github.edeqa</groupId>
  <artifactId>eventbus</artifactId>
  <version>1.0</version> </dependency> 

How to use

First, create event bus:

eventBus1 = new EventBus("first");
 eventBus2 = new EventBus("second");
 

or just:

eventBus = new EventBus();
 

Make the class implementing EntityHolder:

public class SampleHolder implements EntityHolder<Context> {

  ... 
}
 

But much better is to inherit the class from AbstractEntityHolder and implement onEvent for handle events and make some logic.

public class SampleHolder extends AbstractEntityHolder<Context> {

public SampleHolder(Context context) {

super(context);

  
}

@Override
  public boolean onEvent(String eventName, Object eventObject) {

switch(eventName) {

 case "event1":

  System.out.println("EntityHolder name: " + getType());

  break;

 case "event2":

  System.out.println("Event with argument: " + eventObject);

  break;

 case "event3":

 case "event4":

  break;

}

return true; // if returns false then chain will be interrupted
  
}
 
}
 

Register it in the bus:

eventBus.register(new SampleHolder(this));
 

Posting event

Post event:

eventBus.post("event1");
 eventBus.post("event2", "argument2");
 

Post event to all registered buses:

EventBus.postAll("event3");
 EventBus.postAll("event4", "argument4");
 

Event will be posted to all holders in the order that holders were registered.

Specific task in queue

If you want to run some specific task in the same queue as events then use post#Runnable:

eventBus.post(new Runnable() {

  @Override
  public void run() {

LOGGER.severe("EventBus specific task.");

  
}
 
}
);
 

Limit events spreading

If some AbstractEntityHolder#onEvent returns false then next holders in the queue will not be called.

You can also limit the spreading of events using AbstractEntityHolder#events:

@Override public List<String> events() {

  List<String> list = new ArrayList<>();

  list.add("event1");

  list.add("event2");

  return list; 
}
 

Events event1 and event2 will be posted only to specific holder (or holders).

Updating holder

You can update the holder without losing its position in the queue:

eventBus.update(new SampleHolder(this));
 

Android UI specific

Some of Android tasks (i.e UI interaction) require performing in the main thread. Then, set specific runner for all buses by following code:

final Handler handler = new Handler(Looper.getMainLooper());
 EventBus.Runner runner = new EventBus.Runner() {

  @Override
  public void post(Runnable runnable) {

handler.post(runnable);

  
}
 
}
; EventBus.setMainRunner(runner);
 

Or separately:

eventBus.setRunner(runner);
 

Note that EventBus.setMainRunner overrides all previously defined runners.

Troubleshooting

You can switch log details:

EventBus.LOGGING_LEVEL = Level.ALL; 

Possible values: ALL, FINE, CONFIG, INFO, WARNING, SEVERE. Default is WARNING.

Inspect events

Deep inspection for specific events can be set next way:

EventBus.inspect("event1");
 EventBus.inspect("event2");
 

This will throw the stacktrace when these events happen.

Cancel inspection:

EventBus.inspect(null);
 

Javadoc

See the Javadoc for more details about the API.

History

1.0 - EventBus#getEventBuses; EventBus#getEventBus; EventBus#getOrCreateEventBus; eventBus#getEventBusName; EventBus#setLoggingLevel; -EventBus#postSync; tests; fixes

0.9 - fixes

0.8 - eventBus#registerOrUpdate; eventBus#unregister; javadoc

0.7 - refactoring to interface; fixes

0.6 - eventBus#postRunnable; docs; fixes

0.5 - update#holder; post#Runnable; EventBus#inspect; debug; fixes

0.4 - limit events

0.3 - fixes

0.2 - fixes

0.1 - initial

License

EventBus is licensed under an MIT license. See the LICENSE file for specifics.

Resources

Simple Weibo SDK turns Weibo API into a Java interface with RxJava.

Exponential backoff for Java (Java port of segmentio/backo).

A view to show bling bling stars when you touch it.

A cache lib for SharedPreferences.

Android buttons in various styles.

Gradle plugin for Spoon. Allows you to run spoon with almost no effort under new Android build system.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes