attranslate
Version:
Text Translator for Websites and Apps
70 lines (69 loc) • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormatCache = void 0;
/**
* We use format-caches to hold format-specific structures.
* A general pattern is:
* Prefer "same-file-caches" over other caches, and prefer newer caches over older caches.
*/
class FormatCache {
constructor() {
this.fileCaches = [];
}
findFileCache(path) {
var _a;
return (_a = this.fileCaches.find((fileCache) => fileCache.path === path)) !== null && _a !== void 0 ? _a : null;
}
insertFileCache(fileCache) {
this.fileCaches.push(fileCache);
}
insert(args) {
let fileCache = this.findFileCache(args.path);
if (!fileCache) {
fileCache = { path: args.path, entries: new Map(), auxData: null };
this.insertFileCache(fileCache);
}
fileCache.entries.set(args.key, args.entry);
}
lookup(args) {
const sameFileCache = this.findFileCache(args.path);
if (sameFileCache) {
const sameFileHit = sameFileCache.entries.get(args.key);
if (sameFileHit !== undefined) {
return sameFileHit;
}
}
for (let idx = this.fileCaches.length - 1; idx >= 0; idx--) {
const fileCache = this.fileCaches[idx];
const hit = fileCache.entries.get(args.key);
if (hit !== undefined) {
return hit;
}
}
return null;
}
lookupSameFileAuxdata(args) {
var _a, _b;
return (_b = (_a = this.findFileCache(args.path)) === null || _a === void 0 ? void 0 : _a.auxData) !== null && _b !== void 0 ? _b : null;
}
lookupAuxdata(args) {
const sameFileAuxdata = this.lookupSameFileAuxdata(args);
if (sameFileAuxdata) {
return sameFileAuxdata;
}
if (this.fileCaches.length) {
return this.fileCaches[this.fileCaches.length - 1].auxData;
}
return null;
}
getOldestAuxdata() {
if (!this.fileCaches.length) {
return null;
}
return this.fileCaches[0].auxData;
}
purge() {
this.fileCaches = [];
}
}
exports.FormatCache = FormatCache;