zigcall 1.0.0

Yet another simple Signals and Slots implementation of the observer pattern.

Released 2018-05-01.

To install, run:

haxelib install zigcall 1.0.0

See using Haxelib in Haxelib documentation for more information.

Maintainerchfoo
Websitehttps://github.com/chfoo/zigcall
Current version1.0.0
StatisticsInstalled 1 times
LicenseMIT
Tags

README.md

ZigCall

ZigCall is, yet another, a simple Signals and Slots implementation of the observer pattern for the Haxe programming language.

ZigCall's feature set is intentionally limited when compared to other Haxe signal & slot libraries. Its goal is to provide a clean and minimal interface to the user.

General comparison of event dispatcher to signal and slots:

Event DispatcherSignal and Slots
EventsSignals
ListenersSlots
DispatchEmit
AddConnect
RemoveDisconnect
String event typesObjects for each signal
Dispatcher uses inheritanceEmitter uses composition
Event objectParameters
Capturing & bubblingNone
CancellingTypically none

Quick Start

Installation

Install the library:

    haxelib install zigcall

Connecting signals and slots

In your class that emits signals, add a Signal field for each signal:

import zigcall.Signal;

class Keyboard {
    var _keyDown:Signal;
    var _keyUp:Signal;
}

Expose a public version of Signal to users:

class Keyboard {
    // [...]
    public var keyDown(default, null):SignalClient;
    public var keyUp(default, null):SignalClient;
}

Initialize the fields:

class Keyboard {
    // [...]
    public function new() {
        keyDown = _keyDown = new Signal();
        keyUp = _keyUp = new Signal();
    }
}

In your class that contains the slots for processing the signals, connect them:

class KeyHandler {
    public function new() {
        // [...]
        keyboard.keyDown.connect(keyDownHandler);
        keyboard.keyUp.connect(keyUpHandler);
    }

    function keyDownHandler() {
        trace("A key was pressed down.");
    }

    function keyUpHandler() {
        trace("A key was released.");
    }
}

Emitting signals

Send a signal by calling emit():

class Keyboard {
    // [...]
    public function processKeyEvent() {
        // [...]
        _keyDown.emit();
    }
}

Disconnecting

To stop receiving signals, call disconnect():

class KeyHandler {
    // [...]
    public function dispose() {
        keyboard.keyDown.disconnect(keyDownHandler);
        keyboard.keyUp.disconnect(keyUpHandler);
    }
}

Parameters

If you need to pass data, use the generic version SignalP:

import zigcall;

typedef ClickedParams = {
    x:Int,
    y:Int
};

class MouseExample {
    public static function main() {
        var clicked = new SignalP<ClickedParams>();

        clicked.client.connect(clickedHandler);
        clicked.emit({ x: 10, y: 15 });
    }

    static function clickedHandler(params:ClickedParams) {
        trace('Clicked ${params.x} ${params.y}');
    }
}

Anonymous slots

If you have anonymous functions as slots, use the ConnectionToken provided when connecting:

class PingedExample {
    public static function main() {
        var pinged = new Signal();

        var token = pinged.client.connect(function () {
            trace("Pinged!");
        });

        pinged.emit();
        token.disconnect();
    }
}

If you need to check whether something is connected, use isConnected():

class IsConnectedExample {
    public static function main() {
        // [...]
        if (mySignal.isConnected(mySlot)) {
            // [...]
        }
        if (myToken.isConnected()) {
            // [...]
        }
    }
}

For details, please check the examples and source code.