Simple Haxe Signal API implementation

To install, run:

haxelib install hxsignal 0.9.2 

See using Haxelib in Haxelib documentation for more information.


HxSignal: Simple Haxe Signal

My motivation was to create a simple API for callback functions, I thought that Haxe must have a signals and slots toolkit generic, type safe, flexible and efficient. There are already fantastic signal toolkits for Haxe, but I wanted to give to signal this touch or tweak I think were missing. I like the implementation of msignal, so my first try was to give it my touch but soon I realized it will be different taking C++ Boost and Qt signals as base.

Basic usage

// initialisation
var voidSignal  = new Signal<Void -> Void>();
var eventSignal = new Signal<AnyObject -> String -> Void>();
var signal1     = new Signal<Int -> Void>();

// connecting, adding or binding slot
voidSignal.connect(function() {});

// connecting once (disconnected the first time it is called)
eventSignal.connect(function(origin, type) {}, Once);

// disconnecting slot
function slot1(num : Int) : Void { }


// emitting
eventSignal.emit(this, "clicked");

Advanced usage

// connecting n times (disconnected the nth time it is called)
eventSignal.connect(function(origin, type) {}, Times(n));

// Add slots to groups
signal1.connect(slot1, 1); // slot1 added to group 1
signal1.connect(slot1bis, 2); // slot added to group 2

// disconnect all slots in group 1

// priority
signal1.connect(slot1, 1, AtBack);
signal1.connect(slot1bis, 1, AtFront); // slot1bis is called first then slot1

Responding a signal

// slot that adds 1 to emitted value
function add1(value : Int) : Int {
	return value + 1;

// slot that add 2 to emitted value
function add2(value : Int) : Int {
	return value + 2;

var signalWithResponse = new Signal<Int->Int>();

var result = signalWithResponse.emit(3); // result == 5

Why is result 5? Because signal always return the last result as default behavior, in this case add2(3) has returned 3 + 2 = 5

Handling the signal responses

Let's add a results processor...

// responses == [add1(x), add2(x)] where x is the emitted integer
signalWithResponse.resultsProcessor = function (responses:Array<Int>) {
	var result = 0;
	for (i in responses)
		result += i;
	return result;
var processedResult = signalWithResponse.emit(3); // result == 9

This time emit function returns the processed value and it is 9 because resultsProcessor has added the responses of call add1(3) and add2(3) (4 and 5 respectively).


  • disconnectAll() // disconects all the slots
  • block(slot, true) // block slot (not called) until block(slot, false) is called
  • isBlock(slot)
  • numSlots // amount of slot connected
  • and more features are coming
1 year ago

All libraries are free

Every month, more than 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