<commit_before>/* jshint unused:false */
'use strict';

var Replicant = require('./replicant');
var io = {};
var server = require('./server');
var filteredConfig = require('./config').getFilteredConfig();
var utils = require('./util');
var replicator = require('./replicator');

var inBrowser = typeof document !== 'undefined';

/**
 * Creates a new NodeCG extension API instance.
 * @constructor
 * @param {object} bundle The bundle object to build an API instance from.
 */
function NodeCG(bundle, socket) {
    var self = this;

    // Make bundle name and config publicly accessible
    this.bundleName = bundle.name;
    this.bundleConfig = bundle.config;

    // Make logger publicly accessible
    this.log = require('./logger')(bundle.name);

    this._messageHandlers = [];

    if (inBrowser) {
        // This constructor only works if socket.io is loaded
        if (typeof io === 'undefined') {
            throw new Error('[nodeceg] Socket.IO must be loaded before instantiating the API');
        }

        // If title isn't set, set it to the bundle name
        if (document.title === '') document.title = this.bundleName;

        // Make socket accessible to public methods
        this.socket = socket;
        this.socket.emit('joinRoom', bundle.name);

        // Upon receiving a message, execute any handlers for it
        socket.on('message', function onMessage(data) {
            self.log.trace('[%s] Received message %s (sent to bundle %s) with data:',
                self.bundleName, data.messageName, data.bundleName, data.content);

            self._messageHandlers.forEach(function(handler) {
                if (data.messageName === handler.messageName &&
                    data.bundleName === handler.bundleName) {
                    handler.func(data.content);
                }
            });
        });

        socket.on('error', function(err) {
            if (err.type === 'UnauthorizedError') {
                var url = [location.protocol, '//', location.host, location.pathname].join('');
                window.location.href = '/authError?code='+err.code+'&message='+err.message+'&viewUrl='+url;
            } else {
                self.log.error('Unhandled socket error:', err);
            }
        });<commit_msg>[api] Wait until DOM is ready before checking if title should be defaulted<commit_after>/* jshint unused:false */
'use strict';

var Replicant = require('./replicant');
var io = {};
var server = require('./server');
var filteredConfig = require('./config').getFilteredConfig();
var utils = require('./util');
var replicator = require('./replicator');

var inBrowser = typeof document !== 'undefined';

/**
 * Creates a new NodeCG extension API instance.
 * @constructor
 * @param {object} bundle The bundle object to build an API instance from.
 */
function NodeCG(bundle, socket) {
    var self = this;

    // Make bundle name and config publicly accessible
    this.bundleName = bundle.name;
    this.bundleConfig = bundle.config;

    // Make logger publicly accessible
    this.log = require('./logger')(bundle.name);

    this._messageHandlers = [];

    if (inBrowser) {
        // This constructor only works if socket.io is loaded
        if (typeof io === 'undefined') {
            throw new Error('[nodeceg] Socket.IO must be loaded before instantiating the API');
        }

        // If title isn't set, set it to the bundle name
        document.addEventListener('DOMContentLoaded', function() {
            if (document.title === '') document.title = self.bundleName;
        }, false);

        // Make socket accessible to public methods
        this.socket = socket;
        this.socket.emit('joinRoom', bundle.name);

        // Upon receiving a message, execute any handlers for it
        socket.on('message', function onMessage(data) {
            self.log.trace('[%s] Received message %s (sent to bundle %s) with data:',
                self.bundleName, data.messageName, data.bundleName, data.content);

            self._messageHandlers.forEach(function(handler) {
                if (data.messageName === handler.messageName &&
                    data.bundleName === handler.bundleName) {
                    handler.func(data.content);
                }
            });
        });

        socket.on('error', function(err) {
            if (err.type === 'UnauthorizedError') {
                var url = [location.protocol, '//', location.host, location.pathname].join('');
                window.location.href = '/authError?code='+err.code+'&message='+err.message+'&viewUrl='+url;
            } else {
                self.log.error('Unhandled socket error:', err);
            }
        });
{'fraction_non_alphanumeric': 0.13466865544427872, 'fraction_numerical': 0.0, 'mean_word_length': 2.389169675090253, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1291101', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1210, 'n_words': 355}