<commit_before>require('regenerator/runtime');

var toJson = require('vdom-as-json/toJson');
var Stopwatch = require('../shared/util/stopwatch');
var dbService = require('./databaseService');
var patchMonstersList = require('./patchMonstersList');
var patchMonsterDetail = require('./patchMonsterDetail');
var renderToast = require('../shared/renderToast');
var renderModal = require('../shared/renderModal');

async function onFilterMessage(message) {
  var stopwatch = new Stopwatch();

  var filter = message.filter || '';
  var patch = await patchMonstersList(filter);

  stopwatch.time('patchMonstersList');
  var patchJson = toJson(patch);
  stopwatch.time('toJson');
  var patchJsonAsString = JSON.stringify(patchJson);
  stopwatch.time('JSON.stringify()');
  console.log('patchJsonAsString.length', patchJsonAsString.length);

  self.postMessage({
    type: 'monstersListPatch',
    patch: patchJsonAsString
  });
  stopwatch.totalTime('worker-filter (total)');
}

async function onDetailMessage(message) {
  var {nationalId} = message;
  var stopwatch = new Stopwatch();
  var {patch, themeColor} = await patchMonsterDetail(nationalId);
  stopwatch.time('patchMonsterDetail()');
  var patchJson = toJson(patch);
  stopwatch.time('toJson');
  var patchJsonAsString = JSON.stringify(patchJson);
  stopwatch.time('JSON.stringify()');
  console.log('patchJsonAsString.length', patchJsonAsString.length);
  self.postMessage({
    type: 'monsterDetailPatch',
    patch: patchJsonAsString,
    nationalId: nationalId,
    themeColor: themeColor
  });
  stopwatch.totalTime('worker-detail (total)');
}

async function onOriginMessage(message) {
  dbService.init(message.origin);
}

async function onToastMessage(message) {
  var toast = renderToast(message.toast);
  self.postMessage({
    type: 'toast',
    toast: JSON.stringify(toJson(toast)),
    modal: message.modal
  });
}

async function onModalMessage(message) {
  var modal = renderModal(message.modal);
  self.postMessage({
    type: 'modal',
    modal: JSON.stringify(toJson(modal))
  });
}

async function onMessage(message) {
  switch (message.type) {
    case 'filter':
      await onFilterMessage(message);
      break;
    case 'detail':
      await onDetailMessage(message);
      break;
    case 'origin':
      await onOriginMessage(message);
      break;
    case 'toast':
      await onToastMessage(message);
      break;
    case 'modal':
      await onModalMessage(message);
      break;
  }
}

self.addEventListener('message', e => {
  var message = e.data;
  console.log('worker got message', message);
  onMessage(message).catch(console.log.bind(console));

});<commit_msg>add todo<commit_after>require('regenerator/runtime');

var toJson = require('vdom-as-json/toJson');
var Stopwatch = require('../shared/util/stopwatch');
var dbService = require('./databaseService');
var patchMonstersList = require('./patchMonstersList');
var patchMonsterDetail = require('./patchMonsterDetail');
var renderToast = require('../shared/renderToast');
var renderModal = require('../shared/renderModal');

async function onFilterMessage(message) {
  var stopwatch = new Stopwatch();

  var filter = message.filter || '';
  var patch = await patchMonstersList(filter);

  stopwatch.time('patchMonstersList');
  var patchJson = toJson(patch);
  stopwatch.time('toJson');
  var patchJsonAsString = JSON.stringify(patchJson);
  stopwatch.time('JSON.stringify()');
  console.log('patchJsonAsString.length', patchJsonAsString.length);

  self.postMessage({
    type: 'monstersListPatch',
    patch: patchJsonAsString
  });
  stopwatch.totalTime('worker-filter (total)');
}

async function onDetailMessage(message) {
  var {nationalId} = message;
  var stopwatch = new Stopwatch();
  var {patch, themeColor} = await patchMonsterDetail(nationalId);
  stopwatch.time('patchMonsterDetail()');
  var patchJson = toJson(patch);
  stopwatch.time('toJson');
  var patchJsonAsString = JSON.stringify(patchJson);
  stopwatch.time('JSON.stringify()');
  console.log('patchJsonAsString.length', patchJsonAsString.length);
  self.postMessage({
    type: 'monsterDetailPatch',
    patch: patchJsonAsString,
    nationalId: nationalId,
    themeColor: themeColor
  });
  stopwatch.totalTime('worker-detail (total)');
}

async function onOriginMessage(message) {
  dbService.init(message.origin);
}

async function onToastMessage(message) {
  var toast = renderToast(message.toast);
  self.postMessage({
    type: 'toast',
    toast: JSON.stringify(toJson(toast)),
    modal: message.modal
  });
}

async function onModalMessage(message) {
  var modal = renderModal(message.modal);
  self.postMessage({
    type: 'modal',
    modal: JSON.stringify(toJson(modal))
  });
}

async function onMessage(message) {
  switch (message.type) {
    case 'filter':
      await onFilterMessage(message);
      break;
    case 'detail':
      await onDetailMessage(message);
      break;
    case 'origin':
      await onOriginMessage(message);
      break;
    case 'toast':
      await onToastMessage(message);
      break;
    case 'modal':
      await onModalMessage(message);
      break;
  }
}

self.addEventListener('message', e => {
  var message = e.data;
  console.log('worker got message', message);
  // TODO: handle errors here
  onMessage(message).catch(console.log.bind(console));

});
{'fraction_non_alphanumeric': 0.14022902196358175, 'fraction_numerical': 0.0, 'mean_word_length': 4.590766002098636, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7354577', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1609, 'n_words': 323}