GithubHelp home page GithubHelp logo

eventemitter's Introduction

Haxe Event Emitter

A Minimal Cross-Platform Events Emitter library implemented in Haxe.

USAGE

class Test
{

	static function main()
	{
		var emitter:EventEmitter = new EventEmitter();
		var calls:Array<Any> = [];
    
		var one:EventCallback = new EventCallback(function(args:Array<Dynamic>)  //Event function to be called when emitted
		{
			calls.push('one');
			calls.push(args[0]); //Get first argument of this function
		});

		var two:EventCallback = new EventCallback(function(args:Array<Dynamic>)
		{
			calls.push('two');
		});
		

		emitter.on('foo', one); //Register a named event listener;
		emitter.on('foo', two);
		emitter.off('foo', two); //Turn off event listener called `foo` that has a function two

		emitter.emit('foo', [1]); //Trigger the event with an array of callback parameters
		trace(calls);  //should print [one, 1]
	}

}

API

EventEmitter() Intance Methods

implements IEmitter

Note: All public instance methods of an Event emitter returns it's instance, unless otherwise stated

on

: Listens on a given event with callback function.

emitter.on(event:String, fn:EventCallback):EventEmitter

addEventListener

: Alias of on.

emitter.addEventListener(event:String, fn:EventCallback):EventEmitter

once

: Adds an event listener that will be invoked a single time then automatically removed.

emitter.once(event:String, fn:EventCallback):EventEmitter

off

: Remove the given callback for event or all registered callbacks.

emitter.off(?event:String, ?fn:EventCallback):EventEmitter

removeListener

: Alias of off

emitter.removeListener(event:String, ?fn:EventCallback):EventEmitter

removeAllListeners

: Alias of off

emitter.removeAllListeners():EventEmitter

removeEventListener

: Alias of off

emitter.removeEventListener(event:String, fn:EventCallback):EventEmitter

emit

: Emit event with the given args.

emitter.emit(event:String, ?args:Array<Dynamic>):EventEmitter

listeners : Array

: Return array of callbacks for event

emitter.listeners(event:String):Array<Dynamic>

hasListeners : Bool

: Check if this emitter has event handlers.

emitter.hasListeners(event:String):Bool

EventCallback(fn)

: This class holds the function to be called when an event is triggered. It also serves as an utility class instance to internally trigger the function and access it's arguments

//The instance
new EventCallback(func:Array<Dynamic>->Void)

//The function
function(args:Array<Dynamic>):Void
{
	//Do something.
}

/**
 * Usage
 */
 
var listener = new EventCallback(function(args:Array<Dynamic>):Void
{
	//Do something.
	trace(args[0]) //Print the first argument
}) 

var emitter:EventEmitter = new EventEmitter();
emitter.on('event-name', listener); //Add this listener to an event called `event-name`
emitter.emit('event-name'); //Internally calls listener or each listener registered to `event-name`
emitter.emit('event-name', ['message']) //Sends an array of arguments to the callback function, then call the function 

Event Emitter Static Extension

: This is a mixin that binds events emitter methods to a dynamic object. Must be used by adding using {path.to.eventemitter.}EmitterTools; at the top of your class;

using EmitterTools;

class Test{
	
	static function main()
	{
		var emitter:EventEmitter = new EventEmitter();
		
		var bar:EventCallback = new EventCallback(function(args:Array<Dynamic>){
			//Do something
			trace(args[0]); //prints 'Bar Mixin'
		});
		
		//Test Mixin/Static Extenion event tools;
		var myObject:Dynamic = {};
		var proto = (myObject).event(); //event() method is bound by the EmitterTools extension
		proto.on('foo', bar);
		proto.emit('foo', ['Bar Mixin']);
	}	
}

Target Platforms supported

: EventEmitter has been tested and works on C++, Java, AS3 and Neko. It fails on Javascript target, because I stored the events listeners in a key-value Map, which is not working properly on javascript target. May have to use an alternative to Haxe's Map API. You should default to native EventEmitter that is built into Javascript runtime.

Haxe Version

: Haxe 3.0+

eventemitter's People

Contributors

darmie avatar

Stargazers

 avatar  avatar

Watchers

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