Giter VIP home page Giter VIP logo

eventbus's Introduction

EventBus

Type-safe and thread-safe, one-to-many event bus in Java, optimized for speed and low-garbage on Android.

Event classes can be posted to any object that is registered to receive that particular type. Any registered received can specify a preferred thread to execute the response on, and any number of receivers and objects may register for a given event type.

Available under the Apache 2.0 license.

Basic usage

public class Demo {
  
	class SomethingChanged {
		String content;
	}
	
	public Demo () {
		// Create the EventBus
		EventBus eventBus = new EventBus();
		// Register subscribers
		eventBus.register(this, SomethingChanged.class, somethingChanged());
		// Post an event
		SomethingChanged event = new SomethingChanged();
		event.content = "Hello world!";
		eventBus.post(event);
	}
	
	private EventListener<SomethingChanged> somethingChanged () {
		return new EventListener<SomethingChanged>() {
			
			@Override
			public void onEvent (SomethingChanged event) {
				System.out.println("content=" + event.content);
			}
			
		};
	}
	
}

Multi-threaded usage (Android)

public class Model {
  
	public static EventBus eventBus = new EventBus();
	
	public class WorkComplete { }
	
	public void runTaskInBackground() {
		// Do some work on this background thread
		eventBus.post(new WorkComplete());
	}
	
}

public class Demo extends Activity {
	
	@Override
	public void onPause() {
		super.onPause();
		eventBus.unregister(this);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		eventBus.register(this, createThreadPreference(), WorkComplete.class, workComplete());
	}
	
	private EventListener<WorkComplete> workComplete() {
		return new EventListener<WorkComplete>() {

			@Override
			public void onEvent (WorkComplete event) {
				// This will execute on the UI thread
			}
			
		};
	}
	
	private ThreadPreference createThreadPreference() {
		return new ThreadPreference(new ThreadExecutor() {

			@Override
			public void post (final Object event, final EventListener<Object> listener) {
				// This is invoked on the event-source's thread
				runOnUiThread(new Runnable() { public void run() {
					// Invoke the listener ourselves, on the Activity's UI thread
					listener.onEvent(event);
				}});
			}
			
		});
	}
	
}

eventbus's People

Contributors

rtaylor205 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.