events.hx

A small library that adds a simple event system
https://github.com/notcl4y14/events.hx

To install, run:

haxelib install events.hx 1.1.0 

See using Haxelib in Haxelib documentation for more information.

README.md

events.hx

events.hx is a Haxe library that adds a simple event system.

Example code

package;

import events.EventHandler;

class Sample {
	public static function main() {
		// Creating a new EventHandler
		var eventHandler: EventHandler<String> = new EventHandler();
		
		// Creating a new Event
		eventHandler.createEvent("event");

		// Creating a listener
		var listener = (?options: Dynamic) -> {
			Sys.println("Hello World!");

			if (options != null) {
				Sys.println(options.a);
				Sys.println(options.b);
			}
		};

		// Adding a listener
		eventHandler.addEventListener("event", listener);

		// Call event
		eventHandler.callEvent("event", { a: "b", b: "a" });
	}
}

API

/**
	EventHandler is a class that holds registered
	events.

	EventType can be replaced with something else
	like enum.
**/
class EventHandler<EventType> {
	public function new();

	// Adds and registers an event to EventHandler
	public function createEvent(name: EventType, ?event: EventBase): Void;

	// Removes an event from EventHandler
	public function removeEvent(name: EventType): Void;

	// Adds an Event Listener to Event
	public function addEventListener(name: EventType, call: Function): Void;

	// Removes an Event Listener from Event
	public function removeEventListener(name: EventType, call: Function): Void;

	// Calls event's listeners
	public function callEvent(name: EventType, ?options: Dynamic): Bool;
}

/**
	EventBase is a base class of an Event.
	Can be inherited for custom Event classes.
**/
class EventBase<Options = Any> {
	
	public function new();

	// Adds an Event Listener to Event
	public function addListener(listener: Function): Void;

	// Removes an Event Listener from Event
	public function removeListener(listener: Function): Void;

	// Calls event's listeners
	public function call(?options: Options): Void;
}

/**
	Event is a class that inherits EventBase class.
	It's a default simple Event class.
**/
class Event<Options = Any> extends EventBase<Options> {
	
	public function new();

}
Contributors
notcl4y14
Version
1.1.0
Published
1 year ago
License
MIT

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub