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.

50 lines (49 loc) 1.95 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DiskCache = void 0; const trie_1 = require("@kamilmielnik/trie"); const fs_1 = __importDefault(require("fs")); const constants_1 = require("../constants"); const getDictionaryFilepath_1 = require("./getDictionaryFilepath"); class DiskCache { async get(locale) { if (!this.has(locale)) { return undefined; } const filepath = (0, getDictionaryFilepath_1.getDictionaryFilepath)(locale); const serialized = await fs_1.default.promises.readFile(filepath, 'utf-8'); const trie = trie_1.Trie.deserialize(serialized); return trie; } getLastModifiedTimestamp(locale) { const filepath = (0, getDictionaryFilepath_1.getDictionaryFilepath)(locale); if (!fs_1.default.existsSync(filepath)) { return undefined; } const stats = fs_1.default.statSync(filepath); return stats.mtimeMs; } has(locale) { const filepath = (0, getDictionaryFilepath_1.getDictionaryFilepath)(locale); return fs_1.default.existsSync(filepath); } isStale(locale) { if (!this.has(locale)) { return undefined; } const lastModifiedTimestamp = this.getLastModifiedTimestamp(locale); if (typeof lastModifiedTimestamp === 'undefined') { return undefined; } const timeSinceModification = Math.abs(lastModifiedTimestamp - Date.now()); return timeSinceModification > constants_1.CACHE_STALE_THRESHOLD; } async set(locale, trie) { const filepath = (0, getDictionaryFilepath_1.getDictionaryFilepath)(locale); await fs_1.default.promises.writeFile(filepath, trie.serialize()); } } exports.DiskCache = DiskCache;