<commit_before>package bindx;

import haxe.Constraints.Function;

class SignalTools {
    static public inline var BIND_SIGNAL_META = "BindSignal";

    static public inline var SIGNAL_POSTFIX = "Changed";
    
    /**
     *  Remove all subscriptions from target, useful for release object (use reflection api)
     *  @param bindable - target object
     */
    static public function unbindAll(bindable:bindx.IBindable):Void {
        var clazz:Class<Dynamic> = std.Type.getClass(bindable);
        while (clazz != null) {
            var meta = haxe.rtti.Meta.getFields(clazz);
            if (meta != null) for (m in std.Reflect.fields(meta)) {
                var data = std.Reflect.field(meta, m);
                if (std.Reflect.hasField(data, BIND_SIGNAL_META)) {
                    var signal:bindx.BindSignal.Signal<Dynamic> = cast std.Reflect.field(bindable, m);
                    if (signal != null) {
                        signal.removeAll();
                        var args:Array<Dynamic> = std.Reflect.field(data, BIND_SIGNAL_META);
                        var lazy:Bool = args[1];
                        if (lazy) std.Reflect.setField(bindable, m, null);
                    }
                }
            }
            clazz = std.Type.getSuperClass(clazz);
        }
    }

    /**
     *  Bind all bindable fields/methods (use reflection api)
     *  @param bindable - target object
     *  @param callback - 
     *  @param force = true - force instantiate all lazy signals
     *  @return Void -> Void
     */
    static public function bindAll(bindable:bindx.IBindable, callback: String -> Dynamic -> Dynamic -> Void, force = true):Void -> Void {
        var listeners = new Map<bindx.BindSignal.Signal<Function>, Function>();

        var signals = getSignals(bindable, force);
        for (name in signals.keys()) {
            var signal = signals.get(name);
            if (signal == null) continue;
            if (std.Std.is(signal, bindx.BindSignal.FieldSignal)) {
                var listener = function (from:Dynamic, to:Dynamic) callback(name, from, to);
                listeners.set(signal, listener);
                signal.add(listener);
            } else {
                var listener = function () callback(name, null, null);
                listeners.set(signal, listener);
                signal.add(listener);
            }
        }

        return function () {
            for (signal in listeners.keys()) {
                var listener = listeners.get(signal);
                signal.remove(listener);
            }
        }
    }

    /**
     *  Bind all bindable fields/methods (use reflection api), include IBindable source object in callback
     *  @param bindable - target object
     *  @param callback - 
     *  @param force = true - force instantiate all lazy signals
     *  @return Void -> Void
     */
    static public function bindAllWithOrigin (bindable:bindx.IBindable, callback: bindx.IBindable -> String -> Dynamic -> Dynamic -> Void, force = true):Void -> Void {
        var listeners = new Map<bindx.BindSignal.Signal<Function>, Function>();

        var signals = getSignals(bindable, force);
        for (name in signals.keys()) {
            var signal = signals.get(name);
            if (signal == null) continue;
            if (std.Std.is(signal, bindx.BindSignal.FieldSignal)) {
                var listener = function (from:Dynamic, to:Dynamic) callback(bindable, name, from, to);
                listeners.set(signal, listener);
                signal.add(listener);
            } else {
                var listener = function () callback(bindable, name, null, null);
                listeners.set(signal, listener);
                signal.add(listener);
            }
        }

        return function () {<commit_msg>small refactor SignalTools<commit_after>package bindx;

import haxe.Constraints.Function;

class SignalTools {
    static public inline var BIND_SIGNAL_META = "BindSignal";

    static public inline var SIGNAL_POSTFIX = "Changed";
    
    /**
     *  Remove all subscriptions from target, useful for release object (use reflection api)
     *  @param bindable - target object
     */
    static public function unbindAll(bindable:bindx.IBindable):Void {
        var clazz:Class<Dynamic> = std.Type.getClass(bindable);
        while (clazz != null) {
            var meta = haxe.rtti.Meta.getFields(clazz);
            if (meta != null) for (m in std.Reflect.fields(meta)) {
                var data = std.Reflect.field(meta, m);
                if (std.Reflect.hasField(data, BIND_SIGNAL_META)) {
                    var signal:bindx.BindSignal.Signal<Dynamic> = cast std.Reflect.field(bindable, m);
                    if (signal != null) {
                        signal.removeAll();
                        var args:Array<Dynamic> = std.Reflect.field(data, BIND_SIGNAL_META);
                        var lazy:Bool = args[1];
                        if (lazy) std.Reflect.setField(bindable, m, null);
                    }
                }
            }
            clazz = std.Type.getSuperClass(clazz);
        }
    }

    /**
     *  Bind all bindable fields/methods (use reflection api)
     *  @param bindable - target object
     *  @param callback - 
     *  @param force = true - force instantiate all lazy signals
     *  @return Void -> Void
     */
    static public function bindAll(bindable:bindx.IBindable, callback: String -> Dynamic -> Dynamic -> Void, force = true):Void -> Void {
        var listeners = new Map<bindx.BindSignal.Signal<Function>, Function>();

        var signals = getSignals(bindable, force);
        for (name in signals.keys()) {
            var signal = signals.get(name);
            if (signal == null) continue;
            var listener : Function = std.Std.is(signal, bindx.BindSignal.FieldSignal)
                ? function (from:Dynamic, to:Dynamic) callback(name, from, to)
                : function () callback(name, null, null);
            listeners.set(signal, listener);
            signal.add(listener);
        }

        return function () {
            for (signal in listeners.keys()) {
                var listener = listeners.get(signal);
                signal.remove(listener);
            }
        }
    }

    /**
     *  Bind all bindable fields/methods (use reflection api), include IBindable source object in callback
     *  @param bindable - target object
     *  @param callback - 
     *  @param force = true - force instantiate all lazy signals
     *  @return Void -> Void
     */
    static public function bindAllWithOrigin (bindable:bindx.IBindable, callback: bindx.IBindable -> String -> Dynamic -> Dynamic -> Void, force = true):Void -> Void {
        var listeners = new Map<bindx.BindSignal.Signal<Function>, Function>();

        var signals = getSignals(bindable, force);
        for (name in signals.keys()) {
            var signal = signals.get(name);
            if (signal == null) continue;
            var listener : Function = std.Std.is(signal, bindx.BindSignal.FieldSignal)
                ? function (from:Dynamic, to:Dynamic) callback(bindable, name, from, to)
                : function () callback(bindable, name, null, null);
            listeners.set(signal, listener);
            signal.add(listener);
        }

        return function () {
{'fraction_non_alphanumeric': 0.10901182547233927, 'fraction_numerical': 0.0002718499388337638, 'mean_word_length': 1.94673608329996, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6579204', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1950, 'n_words': 522}