<commit_before>var EventHandler = require('./../utilities/event-handler');
var Utilities = require('./../utilities/utilities');
var Injector = require('./injector');
var VirtualDOM = require('./../virtual-dom/virtual-dom');
var DataStore = require('./../data-store/data-store');
var Dispatcher = require('./dispatcher');

var COMPONENT_DELIM = ':';
var PUBLIC_KEY = '$public';
var PRIVATE_KEY = '$private';
var LIFECYCLE_KEY = '$lifecycle';
var DESCENDANT_KEY = 'descendant';
var SELF_KEY = '$self';
var MISS_KEY = '$miss';
var ANY_KEY = '$any';

function Events(eventGroups, name, rootNode) {
    EventHandler.apply(this);
    this.name = name;
    this.dispatcher = new Dispatcher(rootNode);
    this.eventStore = Events.processEventGroups(eventGroups);
}

Events.prototype = Object.create(EventHandler.prototype);
Events.prototype.constructor = Events;

Events.prototype.initializeDescendantEvents = function initializeDescendantEvents(targetRoot, uid) {
    var events;
    var events;
    var selector;
    var eventName;
    for (selector in this.eventStore[DESCENDANT_KEY]) {
        events = this.eventStore[DESCENDANT_KEY][selector];
        for (eventName in events) {
            event = events[eventName];
            if (eventName.indexOf(COMPONENT_DELIM) >= 0) {
                this.setupProxyEvent(event, targetRoot, uid);
            }
            else {
                this.setupDispatchedEvent(event, targetRoot, uid);
            }
        }
    }
}

/*
example:
events:
    '#surface'
        'famous:events:click' ()=>
 */
Events.prototype.setupProxyEvent = function setupProxyEvent(event, targetRoot, uid) {
    var trigger = Events.getDirectEventAction(event.name);
    var triggerParams = Utilities.getParameterNames(trigger);
    var lastDelimIdx = event.name.lastIndexOf(COMPONENT_DELIM);
    var eventName = event.name.slice(lastDelimIdx + 1); // 'famous:events:click' -> 'click'

    var payload = {
        eventName: eventName
    };

    var targets = VirtualDOM.query(targetRoot, event.selector);
    var targetUID;
    var bestComponent;
    var triggerArgs;
    for (var i = targets.length - 1; i >= 0; i--) {
        targetUID = VirtualDOM.getUID(targets[i]);
        bestComponent = DataStore.getComponent(targetUID);

        payload.listener = function(eventPayload) {
            var listenerArgs = Injector.getArgs(event.params, eventPayload, uid);
            event.action.apply(null, listenerArgs);
        }

        triggerArgs = Injector.getArgs(triggerParams, payload, targetUID);
        trigger.apply(null, triggerArgs);
    };
}

/*
example:
events:
    '#surface'
        'custom-event' ()=>
 */
Events.prototype.setupDispatchedEvent = function setupDispatchedEvent(event, targetRoot, uid) {
    var self = this;
    var targets = VirtualDOM.query(targetRoot, event.selector);
    var component;
    var eventChannel;
    for (var i = 0; i < targets.length; i++) {
        component = DataStore.getComponent(VirtualDOM.getUID(targets[i]));
        component.events.dispatcher.on(event.name, function(message) {
            self.sendDescendantMessage(event.name, message, event.selector, uid)
        });
    }
}

Events.prototype.sendMessage = function sendMessage(key, value, uid) {
    var publicEvents = this.eventStore[PUBLIC_KEY];
    var eventsToFire = [];
    if (publicEvents[ANY_KEY]) {
        eventsToFire.push(publicEvents[key]);
    }
    if (publicEvents[key]) {
        eventsToFire.push(publicEvents[key]);
    }
    else {
        if (publicEvents[MISS_KEY]) {
            eventsToFire.push(publicEvents[MISS_KEY])
        }
    }

    if (eventsToFire.length < 1) {
        throw new Error('Unknown public event `' + key + '` for `' + this.name + '`');
    }

    var event;<commit_msg>Use Logger to post error message for unfound public event<commit_after>var EventHandler = require('./../utilities/event-handler');
var Utilities = require('./../utilities/utilities');
var Injector = require('./injector');
var VirtualDOM = require('./../virtual-dom/virtual-dom');
var DataStore = require('./../data-store/data-store');
var Logger = require('./../logger/logger');
var Dispatcher = require('./dispatcher');

var COMPONENT_DELIM = ':';
var PUBLIC_KEY = '$public';
var PRIVATE_KEY = '$private';
var LIFECYCLE_KEY = '$lifecycle';
var DESCENDANT_KEY = 'descendant';
var SELF_KEY = '$self';
var MISS_KEY = '$miss';
var ANY_KEY = '$any';

function Events(eventGroups, name, rootNode) {
    EventHandler.apply(this);
    this.name = name;
    this.dispatcher = new Dispatcher(rootNode);
    this.eventStore = Events.processEventGroups(eventGroups);
}

Events.prototype = Object.create(EventHandler.prototype);
Events.prototype.constructor = Events;

Events.prototype.initializeDescendantEvents = function initializeDescendantEvents(targetRoot, uid) {
    var events;
    var events;
    var selector;
    var eventName;
    for (selector in this.eventStore[DESCENDANT_KEY]) {
        events = this.eventStore[DESCENDANT_KEY][selector];
        for (eventName in events) {
            event = events[eventName];
            if (eventName.indexOf(COMPONENT_DELIM) >= 0) {
                this.setupProxyEvent(event, targetRoot, uid);
            }
            else {
                this.setupDispatchedEvent(event, targetRoot, uid);
            }
        }
    }
}

/*
example:
events:
    '#surface'
        'famous:events:click' ()=>
 */
Events.prototype.setupProxyEvent = function setupProxyEvent(event, targetRoot, uid) {
    var trigger = Events.getDirectEventAction(event.name);
    var triggerParams = Utilities.getParameterNames(trigger);
    var lastDelimIdx = event.name.lastIndexOf(COMPONENT_DELIM);
    var eventName = event.name.slice(lastDelimIdx + 1); // 'famous:events:click' -> 'click'

    var payload = {
        eventName: eventName
    };

    var targets = VirtualDOM.query(targetRoot, event.selector);
    var targetUID;
    var bestComponent;
    var triggerArgs;
    for (var i = targets.length - 1; i >= 0; i--) {
        targetUID = VirtualDOM.getUID(targets[i]);
        bestComponent = DataStore.getComponent(targetUID);

        payload.listener = function(eventPayload) {
            var listenerArgs = Injector.getArgs(event.params, eventPayload, uid);
            event.action.apply(null, listenerArgs);
        }

        triggerArgs = Injector.getArgs(triggerParams, payload, targetUID);
        trigger.apply(null, triggerArgs);
    };
}

/*
example:
events:
    '#surface'
        'custom-event' ()=>
 */
Events.prototype.setupDispatchedEvent = function setupDispatchedEvent(event, targetRoot, uid) {
    var self = this;
    var targets = VirtualDOM.query(targetRoot, event.selector);
    var component;
    var eventChannel;
    for (var i = 0; i < targets.length; i++) {
        component = DataStore.getComponent(VirtualDOM.getUID(targets[i]));
        component.events.dispatcher.on(event.name, function(message) {
            self.sendDescendantMessage(event.name, message, event.selector, uid)
        });
    }
}

Events.prototype.sendMessage = function sendMessage(key, value, uid) {
    var publicEvents = this.eventStore[PUBLIC_KEY];
    var eventsToFire = [];
    if (publicEvents[ANY_KEY]) {
        eventsToFire.push(publicEvents[key]);
    }
    if (publicEvents[key]) {
        eventsToFire.push(publicEvents[key]);
    }
    else {
        if (publicEvents[MISS_KEY]) {
            eventsToFire.push(publicEvents[MISS_KEY])
        }
    }

    if (eventsToFire.length < 1) {
        Logger.log('Unknown public event `' + key + '` for `' + this.name + '`', 1);
    }

    var event;
{'fraction_non_alphanumeric': 0.1313503553566728, 'fraction_numerical': 0.001710976572782311, 'mean_word_length': 3.1298913043478263, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5680922', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2252, 'n_words': 458}