GithubHelp home page GithubHelp logo

nikosrig / event-dispatcher Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 20 KB

Library for dispatching events

License: MIT License

Java 100.00%
event-bus event-emitter events java event-dispatcher event-dispatching

event-dispatcher's Introduction

Event Dispatcher

Lightweight library for dispatching async and sync events

linkedin Java

Installation

<dependency>
  <groupId>io.github.nikosrig</groupId>
  <artifactId>event-dispatcher</artifactId>
  <version>1.0.0</version>
</dependency>

Events

Events are objects that act as the unit of communication between an emitter and the appropriate event listeners.

public class EventExample implements Event {

	private final ArrayList<String> data = new ArrayList<>();

	public EventExample(String exampleParam) {
		this.data.add(exampleParam);
	}

	public ArrayList<String> getData() {
		return this.data;
	}

	public String getName() {
		return "example-event";
	}
}

Event Listeners

Event listeners are classes that handle the events and are triggered by event dispatchers.

public class EventListenerExample implements EventListener {
	public void handle(Event event) {
		if (!(event instanceof EventExample)) {
			return;
		}

		System.out.println("Event handling");
	}
}

Listener Providers

Listener providers act as groups of event listeners.

public class ListenerProviderExample implements ListenerProvider {

	@Override
	public ArrayList<EventListener> getListenersForEvent(Event event) {
		ArrayList<EventListener> eventListeners = new ArrayList<>();

		if (!(event instanceof EventExample)) {
			return eventListeners;
		}

		eventListeners.add(new EventListenerExample());
		return eventListeners;
	}
}

Event dispatchers

Event dispatchers are a service objects implementing EventDispatcher. They are responsible for retrieving event listeners from listener providers for the dispatched event, and invoking each event listener with that Event.

SyncEventDispatcher example

		SyncEventDispatcher syncEventDispatcher = new SyncEventDispatcher();
		syncEventDispatcher.addListener("testEvent", eventListener);
		syncEventDispatcher.addListenerProvider(listenerProvider);
		syncEventDispatcher.dispatch(event);

AsyncEventDispatcher example

		AsyncEventDispatcher asyncEventDispatcher = new AsyncEventDispatcher();
		asyncEventDispatcher.addListener("testEvent", eventListener);
		asyncEventDispatcher.addListenerProvider(listenerProvider);
		asyncEventDispatcher.dispatch(event);

๐Ÿค Contact

Nikos Rigas - [email protected]

Licence

Distributed under the MIT License

event-dispatcher's People

Contributors

nikosrig avatar

Stargazers

 avatar  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.