<commit_before>import NodeBinding from './nodeBinding';
import { BindingTemplate } from "../bindingTemplate";
import browser from "../browser";
import { TemplateDefinitions, TemplateDefinitionsValueFunction, ValueFunction } from "../elements";
import { Observable } from '../../Observable/observable';

class DataBinding extends NodeBinding {
    private childTemplates: Array<BindingTemplate>;
    private localUpdate: boolean;
    private destroyedTemplates: Array<BindingTemplate>;

    private templateFunction: {(c: {}, i: number): TemplateDefinitions};

    constructor(boundTo: Node, binding: ValueFunction<any>, children: TemplateDefinitionsValueFunction) {
        super(boundTo, binding);
        this.childTemplates = [];
        this.destroyedTemplates = [];

        if(typeof children != 'function')
            this.templateFunction = () => children;
        else
            this.templateFunction = children;
    }

    public Update() {
        var newValue = this.GetValue();
        if(newValue.length < this.childTemplates.length) {
            var oldComponents = this.childTemplates.splice(newValue.length);
            for(var x=0; x<oldComponents.length; x++) {
                if(this.destroyedTemplates.indexOf(oldComponents[x]) < 0)
                    this.destroyedTemplates.push(oldComponents[x]);
            }
        }
        super.Update();
    }

    public Destroy() {
        for(var x=0; x<this.childTemplates.length; x++)
            this.childTemplates[x].Destroy();
            
        super.Destroy();
    }

    protected Apply() {
        var currentLength = this.childTemplates.length;
        var newValue = this.GetValue();

        this.destroyedTemplates.forEach(c => c.Destroy());
        if(currentLength < newValue.length) {
            var frag = browser.createDocumentFragment();
            for(var x=currentLength; x<newValue.length; x++) {
                var temp = this.templateFunction(newValue[x], x);
                var newTemplate = new BindingTemplate(temp);
                newTemplate.AttachTo(frag);
                this.childTemplates.push(newTemplate);
            }
            this.BoundTo.appendChild(frag);
        }
        this.destroyedTemplates = [];
    }

    private GetValue(): Array<any> {
        var newValue = this.Value;
        if(newValue instanceof Observable)
            newValue = newValue.valueOf();

        if(!Array.isArray(newValue))
            newValue = [newValue];<commit_msg>data binding doubles as if<commit_after>import NodeBinding from './nodeBinding';
import { BindingTemplate } from "../bindingTemplate";
import browser from "../browser";
import { TemplateDefinitions, TemplateDefinitionsValueFunction, ValueFunction } from "../elements";
import { Observable } from '../../Observable/observable';

class DataBinding extends NodeBinding {
    private childTemplates: Array<BindingTemplate>;
    private localUpdate: boolean;
    private destroyedTemplates: Array<BindingTemplate>;

    private templateFunction: {(c: {}, i: number): TemplateDefinitions};

    constructor(boundTo: Node, binding: ValueFunction<any>, children: TemplateDefinitionsValueFunction) {
        super(boundTo, binding);
        this.childTemplates = [];
        this.destroyedTemplates = [];

        if(typeof children != 'function')
            this.templateFunction = () => children;
        else
            this.templateFunction = children;
    }

    public Update() {
        var newValue = this.GetValue();
        if(newValue.length < this.childTemplates.length) {
            var oldComponents = this.childTemplates.splice(newValue.length);
            for(var x=0; x<oldComponents.length; x++) {
                if(this.destroyedTemplates.indexOf(oldComponents[x]) < 0)
                    this.destroyedTemplates.push(oldComponents[x]);
            }
        }
        super.Update();
    }

    public Destroy() {
        for(var x=0; x<this.childTemplates.length; x++)
            this.childTemplates[x].Destroy();
            
        super.Destroy();
    }

    protected Apply() {
        var currentLength = this.childTemplates.length;
        var newValue = this.GetValue();

        this.destroyedTemplates.forEach(c => c.Destroy());
        if(currentLength < newValue.length) {
            var frag = browser.createDocumentFragment();
            for(var x=currentLength; x<newValue.length; x++) {
                var temp = this.templateFunction(newValue[x], x);
                var newTemplate = new BindingTemplate(temp);
                newTemplate.AttachTo(frag);
                this.childTemplates.push(newTemplate);
            }
            this.BoundTo.appendChild(frag);
        }
        this.destroyedTemplates = [];
    }

    private GetValue(): Array<any> {
        var newValue = this.Value && this.Value.valueOf();

        if(!newValue)
            return [];

        if(!Array.isArray(newValue))
            return [newValue];
{'fraction_non_alphanumeric': 0.11790833838077933, 'fraction_numerical': 0.0012113870381586917, 'mean_word_length': 2.4426685198054203, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 15, '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': '3285632', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1250, 'n_words': 261}