<commit_before>var pipes      = require('./pipes');
var $          = require('jquery-browserify');
var Handlebars = require('handlebars');
var esprima    = require('esprima');

var client = new Faye.Client('http://localhost:8000/');
var source   = $("#main-template").html();
var template = Handlebars.compile(source);

var createViewmodel = require('./timeline-view-model');

var currentViewModel;

function onBody(body) {


  $(".main-template-target").html('Running...')

  var module;
  try {
    esprima.parse('('+body+')');
    module = eval('('+body+')')
  } catch(e) {
    $(".main-template-target").html("Error evaluating module<br />"+ e.message)
    return
  }

  console.log("Module was parsed:",module)
  pipes.module(module)
  .runWorld((module.worlds && module.worlds[0]) || {})
  .then(function(timeline) {
    console.log("timeline result", JSON.stringify(timeline, null,2))
    if(currentViewModel)
      currentViewModel.onChange = null;
    currentViewModel = createViewmodel(timeline)

    function render() {
      $(".main-template-target").html(template(currentViewModel))
    }
    render()
    currentViewModel.onChange = render
  })

}


$(document).ready(function() {
  $.ajax({ type: 'GET', url: '/body', complete: function(response) {
    console.log("response", response)
    if (response.status === 200)
      onBody(response.responseText)
  }});
  client.subscribe('/body', onBody);
  $(document).keydown(function(e){
    if (e.keyCode === 40) currentViewModel.arrowDown();
    if (e.keyCode === 38) currentViewModel.arrowUp();<commit_msg>Handle error in running module a bit better<commit_after>var pipes      = require('./pipes');
var $          = require('jquery-browserify');
var Handlebars = require('handlebars');
var esprima    = require('esprima');

var client = new Faye.Client('http://localhost:8000/');
var source   = $("#main-template").html();
var template = Handlebars.compile(source);

var createViewmodel = require('./timeline-view-model');

var currentViewModel;

function onBody(body) {


  $(".main-template-target").html('Running...')

  var module;
  try {
    esprima.parse('('+body+')');
    module = eval('('+body+')')
  } catch(e) {
    $(".main-template-target").html("Error evaluating module<br />"+ e.message)
    return
  }

  console.log("Module was parsed:",module)

  var whenTimeline;

  try {
    whenTimeline = pipes.module(module).runWorld((module.worlds && module.worlds[0]) || {})
  } catch(e) {
    var findFunctionName = e.stack.match(/at\s(.+)\s\(eval/)
    var functionName =  findFunctionName && findFunctionName[1]
    var findLineNumber = e.stack.match(/\d+:\d+\),.+:(\d):\d+\)/)
    var lineNumber = findLineNumber && findLineNumber[1]
    $(".main-template-target").html("Error running module<br />"+ e.message + " (on line " + lineNumber + " in module - in transform <strong>" + functionName + '</strong>)')
    return
  }
  whenTimeline.then(function(timeline) {
    if(currentViewModel)
      currentViewModel.onChange = null;
    currentViewModel = createViewmodel(timeline)

    function render() {
      $(".main-template-target").html(template(currentViewModel))
    }
    render()
    currentViewModel.onChange = render
  })
  .done()
}



$(document).ready(function() {
  $.ajax({ type: 'GET', url: '/body', complete: function(response) {
    console.log("response", response)
    if (response.status === 200)
      onBody(response.responseText)
  }});
  client.subscribe('/body', onBody);
  $(document).keydown(function(e){
    if (e.keyCode === 40) currentViewModel.arrowDown();
    if (e.keyCode === 38) currentViewModel.arrowUp();
{'fraction_non_alphanumeric': 0.18492961634004967, 'fraction_numerical': 0.007452387524151256, 'mean_word_length': 3.884097035040431, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '3561667', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1117, 'n_words': 212}