import { inject, Injectable } from '@angular/core'; import { Observable, shareReplay } from 'rxjs'; import { Dominio, IndicadorDatos, TipoPeriodo } from './indicadores.models'; import { IndicadoresService } from './indicadores.service'; import { IndicadoresRequests } from './indicadores-requests'; @Injectable({ providedIn: 'root' }) export class IndicadoresKpiQueries { private readonly service = inject(IndicadoresService); private readonly requests = inject(IndicadoresRequests); // Only resolved KPI payloads survive navigation. Errors and cancelled requests reset. private readonly cache = new Map>(); datos(domain: Dominio, id: string, tipo: TipoPeriodo, period: string) { const key = JSON.stringify([domain, id, tipo, period]); let request = this.cache.get(key); if (!request) { request = this.requests.run(() => this.service.datos(domain, id, tipo, [period])).pipe( shareReplay({ bufferSize: 1, refCount: true })); this.cache.set(key, request); } return request; } }