UNPKG

ps2census

Version:

Client to connect to the PS2 Event Stream websocket.

62 lines 2.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CharacterManager = void 0; const max_retry_exception_1 = require("../exceptions/max-retry.exception"); const local_cache_1 = require("../utils/local.cache"); const next_tick_1 = __importDefault(require("../../utils/next-tick")); class CharacterManager { constructor(client, { retries, query, cache } = {}) { this.client = client; this.queue = new Map(); this.query = this.client.rest.getQueryBuilder('character'); if (query) this.query = query(this.query); this.maxRetries = retries ?? 1; this.cache = cache ?? new local_cache_1.LocalCache(); } async fetch(id, force = false) { if (this.queue.has(id)) return Object.assign({}, await this.queue.get(id)); if (!force) { const cache = await this.cache.fetch(id); if (cache) return cache; } const request = this.request(id); this.queue.set(id, request); const data = await request; this.queue.delete(id); await this.cache.put(id, data); return data; } async forget(id) { await this.cache.forget(id); } async forgetAll() { await this.cache.forgetAll(); } async request(id) { const conditions = { character_id: id }; let retries = this.maxRetries; const attempts = []; (0, next_tick_1.default)(() => this.client.emit('debug', `Fetching character with id "${id}" from Census`)); do { try { const data = await this.query.get(conditions); if (data.length > 0) return data[0]; attempts.push(new Error(`Census did not return a character for "${id}"`)); } catch (e) { attempts.push(e); } retries--; } while (retries > 0); throw new max_retry_exception_1.MaxRetryException('character', conditions, attempts); } } exports.CharacterManager = CharacterManager; //# sourceMappingURL=character.manager.js.map