Android Publisher/Subscriber library for lazy people
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app
gradle/wrapper
.gitignore
.travis.yml
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

Build Status License

EventHub

Android Publisher/Subscriber library for lazy people who don't want to worry about manual unsubscription and memory leaks

  • no reflection
  • no need to unsubscribe manually (forget about memory leaks)

Examples:

Subscribe with weak subscription, internally EventHub stores a weak reference to the subscriber.

EventHub eventHub = new EventHub();
eventHub.subscribe(SomeEvent.class, event -> Log.d("event", "some event published"));
eventHub.publish(new SomeEvent());

You can also subscribe for token, then you control the lifetime of the subscription.

EventHub eventHub = new EventHub();
Token token = eventHub.subscribeForToken(SomeEvent.class, event -> Log.d("event", "this won't be called"));
token.unSubscribe();
eventHub.publish(new SomeEvent());

Want to call the subscription on the main thread, background thread or calling thread? Pass the PublicationMode when subscribing.

EventHub eventHub = new EventHub();
eventHub.subscribe(SomeEvent.class, event -> Log.d("event", "some event published on main thread"), PublicationMode.MAIN_THREAD);
eventHub.publish(new SomeEvent());

Have some custom rule whether the subscription can be invoked? Pass it to subscribe method.

EventHub eventHub = new EventHub();
eventHub.subscribe(SomeEvent.class, event -> Log.d("event", "this won't be called"), () -> false);
eventHub.publish(new SomeEvent());

In Kotlin you can specify the event type as generic parameter of subscribe method.

val eventHub = EventHub()
eventHub.subscribe<SomeEvent> { e -> Log.d("event", "some event called") }
eventHub.publish(SomeEvent())

Written by Marko Devcic

License APL 2.0