UNPKG

scrabble-solver

Version:

Scrabble Solver 2 - Free, open-source, cross-platform, multi-language analysis tool for Scrabble, Scrabble Duel, Super Scrabble, Letter League, Literaki, and Kelimelik. Quickly find the top-scoring words using the given board and tiles.

48 lines (47 loc) 2.04 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Dictionaries = void 0; const logger_1 = require("@scrabble-solver/logger"); const types_1 = require("@scrabble-solver/types"); const fs_1 = __importDefault(require("fs")); const constants_1 = require("./constants"); const lib_1 = require("./lib"); class Dictionaries { constructor() { this.cache = new lib_1.LayeredCache(); this.downloadDictionaryProxies = Object.fromEntries(Object.values(types_1.Locale).map((locale) => [locale, (0, lib_1.createAsyncProxy)(() => (0, lib_1.downloadDictionary)(locale))])); } async get(locale) { if (this.cache.has(locale)) { const trie = await this.cache.get(locale); if (trie) { return trie; } } logger_1.logger.info('Dictionaries - cache miss', { locale }); return this.updateDictionary(locale); } remove() { fs_1.default.rmdirSync(constants_1.OUTPUT_DIRECTORY, { recursive: true }); } async update(force) { const locales = force ? Object.values(types_1.Locale) : this.getLocalesToUpdate(); logger_1.logger.info('Dictionaries - update', { force, locales }); await Promise.all(locales.map((locale) => this.updateDictionary(locale))); } getLocalesToUpdate() { return Object.values(types_1.Locale).filter((locale) => this.cache.isStale(locale) !== false); } async updateDictionary(locale) { logger_1.logger.info('Dictionaries - updateDictionary', { locale }); fs_1.default.mkdirSync(constants_1.OUTPUT_DIRECTORY, { recursive: true }); const downloadDictionaryProxy = this.downloadDictionaryProxies[locale]; const trie = await downloadDictionaryProxy(); await this.cache.set(locale, trie); return trie; } } exports.Dictionaries = Dictionaries;