Skip to content

Android Publisher/Subscriber library for lazy people

Notifications You must be signed in to change notification settings

deva666/EventHub

Repository files navigation

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

About

Android Publisher/Subscriber library for lazy people

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published