import type { AssessorApiClient } from './assessor-api-client'; import type { AssessmentContextResponse } from './assessor-api-contract'; // The client-opening workflow and every mounted reader share the same request. // Weak ownership prevents old datasets (and their context) from being retained. let requests = new WeakMap>>(); export function preparedAssessmentContext( api: Pick, dataset: object, projectId: string, assessmentIds: string[], scanRunIds: string[] = [] ): Promise { const assessments = [...new Set(assessmentIds)].sort(); const scans = [...new Set(scanRunIds)].sort(); const key = JSON.stringify([projectId, assessments, scans]); let scopes = requests.get(dataset); if (!scopes) { scopes = new Map(); requests.set(dataset, scopes); } let request = scopes.get(key); if (!request) { request = api.getAssessmentContext(projectId, assessments, scans); scopes.set(key, request); } // Keep failures too: mounting another reader must not silently retry. Opening // the client again creates a fresh dataset and therefore a fresh attempt. return request; } export function resetPreparedAssessmentContextForTests(): void { requests = new WeakMap(); }