<commit_before>'use strict';

var Bundle = require('./../bundle');
var Famous = require('./../famous');
var AppliedController = require('./controllers/applied');
var FlowController = require('./controllers/flow');
var HandlingController = require('./controllers/handling');
var TimeController = require('./controllers/time');

var FUNC = 'function';
var TIME_KEY = '$time';
var SELF_KEY = '$self';
var EVERY_KEY = '$every';
var STATE_KEY = '$state';
var IF_KEY = '$if';
var REPEAT_KEY = '$repeat';
var YIELD_KEY = '$yield';
var FLOW_BEHAVIORS = {};
FLOW_BEHAVIORS[IF_KEY] = true;
FLOW_BEHAVIORS[REPEAT_KEY] = true;
FLOW_BEHAVIORS[YIELD_KEY] = true;

function BehaviorConduit(node) {
    this.node = node;
    var flowBehaviorNames = Object.keys(FLOW_BEHAVIORS);
    this.behaviorList = Bundle.getBehaviorList(this.node.name, flowBehaviorNames);
    this.controlFlowList = Bundle.getMatchingBehaviors(this.node.name, flowBehaviorNames);
    this.controlFlowActionQueue = [[/*if*/], [/*repeat*/], [/*yield*/]];
}

function hasTimeParams(params) {
    return params && params.indexOf(TIME_KEY) !== -1;
}

function isSelfSelector(selector) {
    return selector === SELF_KEY;
}

function isFlowBehavior(name) {
    return (name in FLOW_BEHAVIORS);
}

function wantsAllUpdates(params) {
    return params.indexOf(EVERY_KEY) > -1 ||
           params.indexOf(STATE_KEY) > -1;
}

function stateBehavior(behavior, node) {
    var controller;
    if (isFlowBehavior(behavior.name)) {
        controller = FlowController.create(behavior, node);
    }
    else {
        if (isSelfSelector(behavior.selector)) {
            controller = HandlingController.create(behavior, node);
        }
        else {
            controller = AppliedController.create(behavior, node);
        }
    }

    if (!behavior.params || behavior.params.length < 1) {
        node.stateManager.subscribeOnce(controller);
    }
    else if (wantsAllUpdates(behavior.params)) {
        node.stateManager.subscribe(controller);
    }
    else {
        for (var i = 0; i < behavior.params.length; i++) {
            node.stateManager.subscribeTo(behavior.params[i], controller);
        }
    }

    return controller;
}

function timeBehavior(behavior, node) {
    var controller = TimeController.create(behavior, node);
    Famous.everyFrame(controller);
    return controller;
}

function dynamicBehavior(behavior, node) {
    if (hasTimeParams(behavior.params)) {
        return timeBehavior(behavior, node);
    }
    else {
        return stateBehavior(behavior, node);
    }
}

function staticBehavior(behavior, node) {
    var value = behavior.action;
    behavior.action = function() {
        return value;
    };
    return dynamicBehavior(behavior, node);
}

function setupBehavior(behavior, node) {
    if (behavior.type === FUNC) {
        return dynamicBehavior(behavior, node);
    }
    else {
        return staticBehavior(behavior, node);
    }
}

BehaviorConduit.prototype.prepareControlFlow = function() {
    for (var i = 0; i < this.controlFlowList.length; i++) {
        var controlFlowBehavior = this.controlFlowList[i];
        var controller = setupBehavior(controlFlowBehavior, this.node);
        this.addToControlFlowActionQueue(controlFlowBehavior, controller);
    }
    this.processControlFlow();
};

BehaviorConduit.prototype.prepareNormalBehaviors = function() {
    for (var i = 0; i < this.behaviorList.length; i++) {
        var behavior = this.behaviorList[i];
        setupBehavior(behavior, this.node);
    }
};
<commit_msg>Attach behavior-conduit methods on to prototype<commit_after>'use strict';

var Bundle = require('./../bundle');
var Famous = require('./../famous');
var AppliedController = require('./controllers/applied');
var FlowController = require('./controllers/flow');
var HandlingController = require('./controllers/handling');
var TimeController = require('./controllers/time');

var FUNC = 'function';
var TIME_KEY = '$time';
var SELF_KEY = '$self';
var EVERY_KEY = '$every';
var STATE_KEY = '$state';
var IF_KEY = '$if';
var REPEAT_KEY = '$repeat';
var YIELD_KEY = '$yield';
var FLOW_BEHAVIORS = {};
FLOW_BEHAVIORS[IF_KEY] = true;
FLOW_BEHAVIORS[REPEAT_KEY] = true;
FLOW_BEHAVIORS[YIELD_KEY] = true;

function BehaviorConduit(node) {
    this.node = node;
    var flowBehaviorNames = Object.keys(FLOW_BEHAVIORS);
    this.behaviorList = Bundle.getBehaviorList(this.node.name, flowBehaviorNames);
    this.controlFlowList = Bundle.getMatchingBehaviors(this.node.name, flowBehaviorNames);
    this.controlFlowActionQueue = [[/*if*/], [/*repeat*/], [/*yield*/]];
}

function hasTimeParams(params) {
    return params && params.indexOf(TIME_KEY) !== -1;
}

function isSelfSelector(selector) {
    return selector === SELF_KEY;
}

function isFlowBehavior(name) {
    return (name in FLOW_BEHAVIORS);
}

function wantsAllUpdates(params) {
    return params.indexOf(EVERY_KEY) > -1 ||
           params.indexOf(STATE_KEY) > -1;
}

BehaviorConduit.prototype.stateBehavior = function(behavior) {
    var controller;
    if (isFlowBehavior(behavior.name)) {
        controller = FlowController.create(behavior, this.node);
    }
    else {
        if (isSelfSelector(behavior.selector)) {
            controller = HandlingController.create(behavior, this.node);
        }
        else {
            controller = AppliedController.create(behavior, this.node);
        }
    }

    if (!behavior.params || behavior.params.length < 1) {
        this.node.stateManager.subscribeOnce(controller);
    }
    else if (wantsAllUpdates(behavior.params)) {
        this.node.stateManager.subscribe(controller);
    }
    else {
        for (var i = 0; i < behavior.params.length; i++) {
            this.node.stateManager.subscribeTo(behavior.params[i], controller);
        }
    }

    return controller;
}

BehaviorConduit.prototype.timeBehavior = function(behavior) {
    var controller = TimeController.create(behavior, this.node);
    Famous.everyFrame(controller);
    return controller;
}

BehaviorConduit.prototype.dynamicBehavior = function(behavior) {
    if (hasTimeParams(behavior.params)) {
        return this.timeBehavior(behavior);
    }
    else {
        return this.stateBehavior(behavior);
    }
}

BehaviorConduit.prototype.staticBehavior = function(behavior) {
    var value = behavior.action;
    behavior.action = function() {
        return value;
    };
    return this.dynamicBehavior(behavior);
}

BehaviorConduit.prototype.setupBehavior = function(behavior) {
    if (behavior.type === FUNC) {
        return this.dynamicBehavior(behavior);
    }
    else {
        return this.staticBehavior(behavior);
    }
}

BehaviorConduit.prototype.prepareControlFlow = function() {
    for (var i = 0; i < this.controlFlowList.length; i++) {
        var controlFlowBehavior = this.controlFlowList[i];
        var controller = this.setupBehavior(controlFlowBehavior);
        this.addToControlFlowActionQueue(controlFlowBehavior, controller);
    }
    this.processControlFlow();
};

BehaviorConduit.prototype.prepareNormalBehaviors = function() {
    for (var i = 0; i < this.behaviorList.length; i++) {
        var behavior = this.behaviorList[i];
        this.setupBehavior(behavior, this.node);
    }
};

{'fraction_non_alphanumeric': 0.13106594773952718, 'fraction_numerical': 0.0019355730678833126, 'mean_word_length': 3.561160151324086, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '3195296', 'n_tokens_mistral': 2245, 'n_tokens_neox': 2156, 'n_words': 424}