<commit_before>
var Cheese = { routes: {}, db: {}, events: {} };

(function ($, DOM, diff) {
  var applyDiff = diff.applyDiff;
  var createDiff = diff.createDiff;
  var copyObject = diff.copyObject;
  var DOMUtils = DOM;
  
  $.ajax('/socket.io/socket.io.js', { async: false }).done(handleMessages);
  
  var serverDB = {};
  var socket;
  var initialized = false;
  var dbLoaded = function () {};
  var domLoaded = false;
  
  function updateClient () {
    Cheese.db = copyObject(serverDB);
    Cheese.reload();
  }
  
  function updateServer () {
    if (! initialized) return;
    
    if (JSON.stringify(createDiff(serverDB, Cheese.db)) !== '{}') {
      socket.emit('msg', createDiff(serverDB, Cheese.db));
      serverDB = copyObject(Cheese.db);
    }
  };
  
  function initializeEvents () {
    for (var selector in Cheese.events)
      $(selector).off(Cheese.events[selector]).on(Cheese.events[selector]);
  }
  
  function handleMessages () {
    socket = io.connect('http://' + window.location.hostname);
    
    socket.on('msg', function (diff) {
      applyDiff(diff, serverDB);
      updateClient();
    });
    
    socket.on('init', function (db) {
      serverDB = copyObject(db);
      Cheese.db = copyObject(db);
      initialized = true;
      dbLoaded();
      Cheese.reload();
      domLoaded = true;
    });
  };
  
  Cheese.reload = function () {
    updateServer();
    var html = document.createElement('html');
    html.innerHTML = this.routes[window.location.pathname]();
    DOMUtils.updateDOMElement($('html'), $(html));
    initializeEvents();
  };
  
  Cheese.request = function (name) {
    var content;
    $.ajax('/__static/' + name, { async: false }).done(function (data) { content = data; });
    return content;
  };
  
  Cheese.route = function (r, f) {
    this.routes[r] = f;
  };
  
  Cheese.dbLoaded = function (f) {
    dbLoaded = f;
  };
  
  Cheese.event = function (event, selector, handler) {
    if (! this.events[selector]) this.events[selector] = {};
    this.events[selector][event] = function (e) {
      handler(e);
      Cheese.reload();
    };
    if (domLoaded) initializeEvents();
  };
})(jQuery, DOMUtils, diffUtils);

delete window.diffUtils;
delete window.DOMUtils;
delete window.jQuery;
delete window.$;
delete window.io;<commit_msg>Dynamic routing, don't wait for the server to init.<commit_after>var Cheese = { routes: {}, db: {}, events: {} };

(function ($, DOM, diff) {
  var applyDiff = diff.applyDiff;
  var createDiff = diff.createDiff;
  var copyObject = diff.copyObject;
  var DOMUtils = DOM;
  
  $.ajax('/socket.io/socket.io.js', { async: false }).done(handleMessages);
  
  var serverDB = {};
  var socket;
  var initialized = false;
  var dbLoaded = function () {};
  var domLoaded = false;
  
  function routeMatchesPattern (pattern, route) {
    pattern = pattern.split('/').filter(function (i) { return i !== ''; });
    route = route.split('/').filter(function (i) { return i !== ''; });
    
    if (pattern.length !== route.length) return false;
    
    var args = {};
    
    for (var i = 0; i < pattern.length; i++)
      if (pattern[i].charAt(0) === ':') args[i] = pattern[i];
    
    for (var j = 0; j < route.length; j++) {
      console.log(args[j], route[j], pattern[j]);
      if (args[j] === undefined && route[j] !== pattern[j]) return false;
    }
    
    return true;
  }
  
  function argsForRoute (pattern, route) {
    if (! routeMatchesPattern(pattern, route)) return [];
    
    pattern = pattern.split('/').filter(function (i) { return i !== ''; });
    route = route.split('/').filter(function (i) { return i !== ''; });
    var args = [];
    
    for (var i = 0; i < pattern.length; i++)
      if (pattern[i].charAt(0) === ':') args.push(route[i]);
    
    return args;
  }
  
  function updateClient () {
    Cheese.db = copyObject(serverDB);
    Cheese.reload();
  }
  
  function updateServer () {
    if (! initialized) return;
    
    if (JSON.stringify(createDiff(serverDB, Cheese.db)) !== '{}') {
      socket.emit('msg', createDiff(serverDB, Cheese.db));
      serverDB = copyObject(Cheese.db);
    }
  };
  
  function initializeEvents () {
    for (var selector in Cheese.events)
      $(selector).off(Cheese.events[selector]).on(Cheese.events[selector]);
  }
  
  function handleMessages () {
    socket = io.connect('http://' + window.location.hostname);
    
    socket.on('msg', function (diff) {
      applyDiff(diff, serverDB);
      updateClient();
    });
    
    socket.on('init', function (db) {
      serverDB = copyObject(db);
      Cheese.db = copyObject(db);
      initialized = true;
      dbLoaded();
      Cheese.reload();
      domLoaded = true;
    });
  };
  
  Cheese.reload = function () {
    updateServer();
    
    var route = window.location.pathname;
    var args = [];
    for (var i in this.routes)
      if (routeMatchesPattern(i, window.location.pathname)) {
        args = argsForRoute(i, window.location.pathname);
        route = i;
        break;
      }
    
    var html = document.createElement('html');
    html.innerHTML = this.routes[route].apply(window, args);
    DOMUtils.updateDOMElement($('html'), $(html));
    initializeEvents();
  };
  
  Cheese.request = function (name, callback) {
    if (typeof callback === 'function') {
      $.ajax('/__static/' + name, { async: true }).done(callback);
      return undefined;
    }
    
    var content;
    $.ajax('/__static/' + name, { async: false }).done(function (data) { content = data; });
    return content;
  };
  
  Cheese.route = function (r, f) {
    this.routes[r] = f;
    if (routeMatchesPattern(r, window.location.pathname)) Cheese.reload();
  };
  
  Cheese.dbLoaded = function (f) {
    dbLoaded = f;
  };
  
  Cheese.event = function (event, selector, handler) {
    if (! this.events[selector]) this.events[selector] = {};
    this.events[selector][event] = function (e) {
      handler(e);
      Cheese.reload();
    };
    if (domLoaded) initializeEvents();
  };
})(jQuery, DOMUtils, diffUtils);

delete window.diffUtils;
delete window.DOMUtils;
delete window.jQuery;
delete window.$;
delete window.io;
{'fraction_non_alphanumeric': 0.1695715914644079, 'fraction_numerical': 0.0008144648965629581, 'mean_word_length': 2.866498740554156, 'pattern_counts': {'":': 0, '<': 6, '<?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': '53341', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1950, 'n_words': 433}