All files / server/services last-compiled-code-store.ts

90% Statements 9/10
75% Branches 3/4
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25            7x   7x     7x 7x 7x 1x 1x 1x         6x      
/**
 * Last successfully compiled code per authenticated subject, for the legacy
 * `start_simulation` without `code`. Keyed by subject so that a start never
 * runs code another user compiled; bounded so idle subjects cannot grow it.
 */
export class LastCompiledCodeStore {
  private readonly codeBySubject = new Map<string, string>();
 
  constructor(private readonly maxSubjects: number) {}
 
  set(subject: string, code: string): void {
    this.codeBySubject.delete(subject);
    this.codeBySubject.set(subject, code);
    while (this.codeBySubject.size > this.maxSubjects) {
      const oldest = this.codeBySubject.keys().next().value;
      Iif (oldest === undefined) break;
      this.codeBySubject.delete(oldest);
    }
  }
 
  get(subject: string): string | null {
    return this.codeBySubject.get(subject) ?? null;
  }
}