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
// initialisationvar voidSignal = new Signal<Void -> Void>();var eventSignal = new Signal<AnyObject -> String -> Void>();var signal1 = new Signal<Int -> Void>(); // connecting, adding or binding slotvoidSignal{}); // connecting once (disconnected the first time it is called)eventSignaltype) {}, Once); // disconnecting slotfunction slot1(num : Int) : Void { } signal1 // emittingvoidSignaleventSignal"clicked");signal1
Advanced usage
// connecting n times (disconnected the nth time it is called)eventSignaltype) {}, Times // Add slots to groupssignal11); // slot1 added to group 1signal12); // slot added to group 2 // disconnect all slots in group 1signal1 // prioritysignal11, AtBack);signal11, AtFront); // slot1bis is called first then slot1
Responding a signal
// slot that adds 1 to emitted valuefunction add1(value : Int) : Int { return value + 1;} // slot that add 2 to emitted valuefunction add2(value : Int) : Int { return value + 2;} var signalWithResponse = new Signal<Int->Int>();signalWithResponsesignalWithResponse var result = signalWithResponse // 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 integersignalWithResponse= function{ var result = 0; for (i in responses) result += i; return result;} var processedResult = signalWithResponse // 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).
Also
disconnectAll()
// disconects all the slotsblock(slot, true)
// block slot (not called) until block(slot, false) is calledisBlock(slot)
numSlots
// amount of slot connected- and more features are coming