<commit_before>package bindx.macro;

import bindx.BindSignal.Signal;
import haxe.macro.Expr;
import haxe.macro.Type;
import haxe.macro.Context;
import bindx.BindSignal;

using bindx.macro.MacroUtils;
using haxe.macro.Tools;

class BindSignalProvider implements IBindingSignalProvider {

    /**
     * default value: true
     */
    static inline var LAZY_SIGNAL = "lazySignal";
    /**
     * default value: false
     */
    static inline var INLINE_SIGNAL_GETTER = "inlineSignalGetter";
    
    public function new() {}

    @:extern static inline function signalName(fieldName:String):String return fieldName + SignalTools.SIGNAL_POSTFIX;
    @:extern static inline function signalGetterName(fieldName:String):String return "get_" + signalName(fieldName);
    @:extern static inline function signalPrivateName(fieldName:String):String return "_" + signalName(fieldName);

    public function getFieldDispatcher(field:Field, res:Array<Field>):Void {
        switch (field.kind) {
            case FFun(_):
                generateSignal(field, macro : bindx.BindSignal.MethodSignal, macro new bindx.BindSignal.MethodSignal(), res);
            case FProp(_, _, type, _) | FVar(type, _):
                generateSignal(field, macro : bindx.BindSignal.FieldSignal<$type>, macro new bindx.BindSignal.FieldSignal<$type>(), res);
        }
    }

    public function getFieldChangedExpr(field:Field, oldValue:Expr, newValue:Expr):Expr {
        var args = switch (field.kind) { case FFun(_): []; case _: [oldValue, newValue]; }
        return dispatchSignal(macro this, field.name, args, hasLazy(field.bindableMeta()));
    }

    public function getClassFieldBindExpr(expr:Expr, field:ClassField, listener:Expr):Expr {
        var signalName = signalName(field.name);
        return macro @:privateAccess $expr.$signalName.add($listener);
    }

    public function getClassFieldBindToExpr(expr:Expr, field:ClassField, target:Expr):Expr {
        var signalName = signalName(field.name);
        return switch (field.kind) {
            case FMethod(_):
                var fieldName = field.name;
                macro @:privateAccess {
                    var listener = function () $target = $expr.$fieldName();
                    $expr.$signalName.add(listener);
                    function __unbind__() $expr.$signalName.remove(listener);
                }
            case FVar(_, _):
                var type = field.type.follow().toComplexType();
                macro @:privateAccess {
                    var listener = function (from:$type, to:$type) $target = to;
                    $expr.$signalName.add(listener);
                    function __unbind__() $expr.$signalName.remove(listener);
                }
            }
    }

    public function getClassFieldUnbindExpr(expr:Expr, field:ClassField, listener:Expr):Expr {
        var signalName = signalName(field.name);
        return if (!listener.isNullOrEmpty())
            macro @:privateAccess $expr.$signalName.remove($listener);
        else
            macro @:privateAccess $expr.$signalName.removeAll();
    }

    public function getClassFieldChangedExpr(expr:Expr, field:ClassField, oldValue:Expr, newValue:Expr):Expr {
        var args = switch (field.kind) {
            case FMethod(_): 
                if (!oldValue.isNullOrEmpty())
                    Context.error("method notify doesn't require oldValue", oldValue.pos);
                if (!newValue.isNullOrEmpty())
                    Context.error("method notify doesn't require newValue", newValue.pos);
                [];
            case FVar(_, _):
                [oldValue, newValue];
        }
        return dispatchSignal(expr, field.name, args, hasLazy(field.bindableMeta()));
    }
    
    public function getUnbindAllExpr(expr:ExprOf<IBindable>, type:Type):Expr {
        return macro bindx.BindSignal.SignalTools.unbindAll($expr);
    }

    public function getBindAllExpr(expr:ExprOf<IBindable>, type:Type, listener:Expr, force:Bool = true):Expr {
        return macro bindx.BindSignal.SignalTools.bindAll($expr, $listener, $v{force});
    }<commit_msg>check error in notify<commit_after>package bindx.macro;

import bindx.BindSignal.Signal;
import haxe.macro.Expr;
import haxe.macro.Type;
import haxe.macro.Context;
import bindx.BindSignal;

using bindx.macro.MacroUtils;
using haxe.macro.Tools;

class BindSignalProvider implements IBindingSignalProvider {

    /**
     * default value: true
     */
    static inline var LAZY_SIGNAL = "lazySignal";
    /**
     * default value: false
     */
    static inline var INLINE_SIGNAL_GETTER = "inlineSignalGetter";
    
    public function new() {}

    @:extern static inline function signalName(fieldName:String):String return fieldName + SignalTools.SIGNAL_POSTFIX;
    @:extern static inline function signalGetterName(fieldName:String):String return "get_" + signalName(fieldName);
    @:extern static inline function signalPrivateName(fieldName:String):String return "_" + signalName(fieldName);

    public function getFieldDispatcher(field:Field, res:Array<Field>):Void {
        switch (field.kind) {
            case FFun(_):
                generateSignal(field, macro : bindx.BindSignal.MethodSignal, macro new bindx.BindSignal.MethodSignal(), res);
            case FProp(_, _, type, _) | FVar(type, _):
                generateSignal(field, macro : bindx.BindSignal.FieldSignal<$type>, macro new bindx.BindSignal.FieldSignal<$type>(), res);
        }
    }

    public function getFieldChangedExpr(field:Field, oldValue:Expr, newValue:Expr):Expr {
        var args = switch (field.kind) { case FFun(_): []; case _: [oldValue, newValue]; }
        return dispatchSignal(macro this, field.name, args, hasLazy(field.bindableMeta()));
    }

    public function getClassFieldBindExpr(expr:Expr, field:ClassField, listener:Expr):Expr {
        var signalName = signalName(field.name);
        return macro @:privateAccess $expr.$signalName.add($listener);
    }

    public function getClassFieldBindToExpr(expr:Expr, field:ClassField, target:Expr):Expr {
        var signalName = signalName(field.name);
        return switch (field.kind) {
            case FMethod(_):
                var fieldName = field.name;
                macro @:privateAccess {
                    var listener = function () $target = $expr.$fieldName();
                    $expr.$signalName.add(listener);
                    function __unbind__() $expr.$signalName.remove(listener);
                }
            case FVar(_, _):
                var type = field.type.follow().toComplexType();
                macro @:privateAccess {
                    var listener = function (from:$type, to:$type) $target = to;
                    $expr.$signalName.add(listener);
                    function __unbind__() $expr.$signalName.remove(listener);
                }
            }
    }

    public function getClassFieldUnbindExpr(expr:Expr, field:ClassField, listener:Expr):Expr {
        var signalName = signalName(field.name);
        return if (!listener.isNullOrEmpty())
            macro @:privateAccess $expr.$signalName.remove($listener);
        else
            macro @:privateAccess $expr.$signalName.removeAll();
    }

    public function getClassFieldChangedExpr(expr:Expr, field:ClassField, oldValue:Expr, newValue:Expr):Expr {
        var args = switch (field.kind) {
            case FMethod(_): 
                if (!oldValue.isNullOrEmpty())
                    Context.error("method notify doesn't require oldValue", oldValue.pos);
                if (!newValue.isNullOrEmpty())
                    Context.error("method notify doesn't require newValue", newValue.pos);
                [];
            case FVar(_, _):
                if (oldValue.isNullOrEmpty() && newValue.isNullOrEmpty())
                    Context.error("method notify require oldValue and newValue", oldValue.pos);
                [oldValue, newValue];
        }
        return dispatchSignal(expr, field.name, args, hasLazy(field.bindableMeta()));
    }
    
    public function getUnbindAllExpr(expr:ExprOf<IBindable>, type:Type):Expr {
        return macro bindx.BindSignal.SignalTools.unbindAll($expr);
    }

    public function getBindAllExpr(expr:ExprOf<IBindable>, type:Type, listener:Expr, force:Bool = true):Expr {
        return macro bindx.BindSignal.SignalTools.bindAll($expr, $listener, $v{force});
    }
{'fraction_non_alphanumeric': 0.11364717070851164, 'fraction_numerical': 0.0, 'mean_word_length': 2.727514399645547, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '1066394', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2259, 'n_words': 491}