tf2-item-format
Version:
Format's TF2 items like backpack.tf does
62 lines • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cache = exports.SchemaCache = exports.SchemaCacheArticle = void 0;
class SchemaCacheArticle {
constructor(version, value) {
this.version = version;
this.value = value;
}
/**
* Is this article outdated and needs to be renewed?
*
* @param version Of current schema
* @returns true, if outdated, otherwise false
*/
isOutdated(version) {
return this.version < version;
}
}
exports.SchemaCacheArticle = SchemaCacheArticle;
class SchemaCache {
constructor() {
this.schemaStorage = new Map();
}
get(schema, key) {
const cache = this.schemaStorage.get(schema);
if (!cache) {
return null;
}
const article = cache.get(key);
if (!article) {
return null;
}
if (article.isOutdated(schema.getVersion())) {
return null;
}
return article.value;
}
save(schema, key, value) {
let cache = this.schemaStorage.get(schema);
if (!cache) {
cache = new Map();
this.schemaStorage.set(schema, cache);
}
const version = schema.getVersion();
let article = cache.get(key);
if (article) {
// Reuse old objects
article.version = version;
article.value = value;
return;
}
article = new SchemaCacheArticle(version, value);
cache.set(key, article);
}
}
exports.SchemaCache = SchemaCache;
/**
* Global cache for all schemas, because
* we index by said schema object.
*/
exports.cache = new SchemaCache();
//# sourceMappingURL=schemaCache.js.map