<commit_before>'use strict';

var Args = require('./../../helpers/args');

var IF_KEY = '$if';
var REPEAT_KEY = '$repeat';
var YIELD_KEY = '$yield';
var INITIALIZATION_KEY = 'initialization';
var RUNTIME_KEY = 'runTime';

var CONTROLLER = {};
CONTROLLER[IF_KEY] = require('./if');
CONTROLLER[REPEAT_KEY] = require('./repeat');
CONTROLLER[YIELD_KEY] = require('./yield');

function processControlFlow(onInitialization, behavior, args) {
    return function() {
        var processKey = onInitialization ? INITIALIZATION_KEY : RUNTIME_KEY;
        CONTROLLER[behavior.name][processKey](args);
    };
}

function create(behavior, node) {
    return function(onInitialization) {
        if (!node.lockControlFlow) {
            var behaviorActionArgs = Args.fetch(behavior.params, node.stateManager);
            var payload = behavior.action.apply(null, behaviorActionArgs);

            var controlFlowArgs = {
                payload: payload,
                selector: behavior.selector,
                domStore: node.domStore
            };

            var fn = node.publicEvents[behavior.name] ?
                node.publicEvents[behavior.name].bind(null, node.stateManager, {
                    // TODO: Update components to use domStore
                    payload: payload,
                    selector: behavior.selector,
                    childrenRoot: node.domStore.childrenRoot,
                    surrogateRoot: node.domStore.surrogateRoot,
                    treeSignature: node.domStore.treeSignature
                }) :
                processControlFlow(onInitialization, behavior, controlFlowArgs);

            fn();
        }
    };
}

module.exports = {
    create: create
};<commit_msg>Add add/removeNodes to FlowController for runTime control flow<commit_after>'use strict';

var Args = require('./../../helpers/args');
var VirtualDOM = require('../../virtual-dom');
var NodeStore = require('../../node-store');

var IF_KEY = '$if';
var REPEAT_KEY = '$repeat';
var YIELD_KEY = '$yield';
var INITIALIZATION_KEY = 'initialization';
var RUNTIME_KEY = 'runTime';

var CONTROLLER = {};
CONTROLLER[IF_KEY] = require('./if');
CONTROLLER[REPEAT_KEY] = require('./repeat');
CONTROLLER[YIELD_KEY] = require('./yield');

function processControlFlow(onInitialization, behavior, args, bestNode) {
    return function() {
        var processKey = onInitialization === true ? INITIALIZATION_KEY : RUNTIME_KEY;
        var result = CONTROLLER[behavior.name][processKey](args);

        if (result) {
            addNodes(result.add, bestNode);
            removeNodes(result.remove, bestNode);
        }
    };
}

function addNodes(data, bestNode) {
    var BestNodeConstuctor = bestNode.constructor;
    var parentNode;
    var newNode;
    for (var i = 0; i < data.length; i++) {
        newNode = new BestNodeConstuctor(data[i].blueprintDOMNode);
        parentNode = NodeStore.findNode(data[i].parentUID);
        newNode.prepare(parentNode.famousNode);
        parentNode.domStore.domNode.appendChild(newNode.domStore.domNode);
    }

    bestNode.domStore.buildTreeSignature();
}

function removeNodes(domNodes, bestNode) {
    var nodeToRemove;
    for (var i = 0; i < domNodes.length; i++) {
        nodeToRemove = NodeStore.findNode(VirtualDOM.getUID(domNodes[i]));
        nodeToRemove.teardown();
    }
    bestNode.domStore.buildTreeSignature();
}

function create(behavior, bestNode, controlFlowData) {
    return function(onInitialization) {
        if (!bestNode.lockControlFlow) {
            var behaviorActionArgs = Args.fetch(behavior.params, bestNode.stateManager);
            var payload = behavior.action.apply(null, behaviorActionArgs);

            var controlFlowArgs = {
                payload: payload,
                selector: behavior.selector,
                domStore: bestNode.domStore,
                controlFlowData: controlFlowData
            };

            var fn = bestNode.publicEvents[behavior.name] ?
                bestNode.publicEvents[behavior.name].bind(null, bestNode.stateManager, {
                    // TODO: Update components to use domStore
                    payload: payload,
                    selector: behavior.selector,
                    childrenRoot: bestNode.domStore.childrenRoot,
                    surrogateRoot: bestNode.domStore.surrogateRoot,
                    treeSignature: bestNode.domStore.treeSignature
                }) :
                processControlFlow(onInitialization, behavior, controlFlowArgs, bestNode);

            fn();
        }
    };
}

module.exports = {
    create: create
};
{'fraction_non_alphanumeric': 0.11546840958605664, 'fraction_numerical': 0.00043572984749455336, 'mean_word_length': 2.504580152671756, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6633322', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1254, 'n_words': 250}