<commit_before>import path from 'path';
import mkdirp from 'mkdirp';
import {getURLForHunspellDictionary} from '@paulcbetts/spellchecker';
import {requireTaskPool} from 'electron-remote';
import {getInstalledKeyboardLanguages} from 'keyboard-layout';
import {Observable} from 'rx';

import {fs} from './promisify';
import {normalizeLanguageCode} from './utility';

const d = require('debug')('electron-spellchecker:dictionary-sync');

const {downloadFileOrUrl} = process.type === 'browser' ?
  requireTaskPool(require.resolve('electron-remote/remote-ajax')) :
  require('electron-remote/remote-ajax');

export default class DictionarySync {
  constructor(cacheDir) {
    this.cacheDir = cacheDir;
    mkdirp.sync(cacheDir);
  }

  async loadDictionaryForLanguage(langCode, cacheOnly=false) {
    if (process.platform === 'darwin') return new Buffer([]);

    let lang = normalizeLanguageCode(langCode);
    let target = path.join(this.cacheDir, `${lang}.bdic`);

    let fileExists = false;
    try {
      if (await fs.exists(target)) {
        fileExists = true;
        return await fs.readFileSync(target);
      }
    } catch (e) {
      d(`Failed to read file ${target}: ${e.message}`);
    }

    if (fileExists) {
      try {
        await fs.unlink(target);
      } catch (e) {
        d("Can't clear out file, bailing");
        return null;
      }
    }

    try {
      await downloadFileOrUrl(getURLForHunspellDictionary(lang), target);
    } catch (e) {
      d(`Failed to download file ${target}: ${e.message}`);
      try { fs.unlinkSync(target); } catch(e) {}
      return null;
    }

    if (cacheOnly) return target;
    return await fs.readFileSync(target);
  }

  preloadDictionaries() {
    return Observable.from(getInstalledKeyboardLanguages())
      .flatMap((x) => Observable.fromPromise(this.loadDictionaryForLanguage(x, true)))
      .reduce((acc,x) => { acc.push(x); return acc; }, [])
      .toPromise();
  }
}<commit_msg>Make preloadDictionaries take a list<commit_after>import path from 'path';
import mkdirp from 'mkdirp';
import {getURLForHunspellDictionary} from '@paulcbetts/spellchecker';
import {requireTaskPool} from 'electron-remote';
import {getInstalledKeyboardLanguages} from 'keyboard-layout';
import {Observable} from 'rx';

import {fs} from './promisify';
import {normalizeLanguageCode} from './utility';

const d = require('debug')('electron-spellchecker:dictionary-sync');

const {downloadFileOrUrl} = process.type === 'browser' ?
  requireTaskPool(require.resolve('electron-remote/remote-ajax')) :
  require('electron-remote/remote-ajax');

export default class DictionarySync {
  constructor(cacheDir) {
    this.cacheDir = cacheDir;
    mkdirp.sync(cacheDir);
  }

  async loadDictionaryForLanguage(langCode, cacheOnly=false) {
    if (process.platform === 'darwin') return new Buffer([]);

    let lang = normalizeLanguageCode(langCode);
    let target = path.join(this.cacheDir, `${lang}.bdic`);

    let fileExists = false;
    try {
      if (await fs.exists(target)) {
        fileExists = true;
        return await fs.readFileSync(target);
      }
    } catch (e) {
      d(`Failed to read file ${target}: ${e.message}`);
    }

    if (fileExists) {
      try {
        await fs.unlink(target);
      } catch (e) {
        d("Can't clear out file, bailing");
        return null;
      }
    }

    try {
      await downloadFileOrUrl(getURLForHunspellDictionary(lang), target);
    } catch (e) {
      d(`Failed to download file ${target}: ${e.message}`);
      try { fs.unlinkSync(target); } catch(e) {}
      return null;
    }

    if (cacheOnly) return target;
    return await fs.readFileSync(target);
  }

  preloadDictionaries(languageList=null) {
    return Observable.from(languageList || getInstalledKeyboardLanguages())
      .flatMap((x) => Observable.fromPromise(this.loadDictionaryForLanguage(x, true)))
      .reduce((acc,x) => { acc.push(x); return acc; }, [])
      .toPromise();
  }
}
{'fraction_non_alphanumeric': 0.14303733602421795, 'fraction_numerical': 0.0, 'mean_word_length': 3.6158323632130385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2061962', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1230, 'n_words': 273}