All files / client/src/utils serial-character-renderer.ts

94.73% Statements 54/57
90.9% Branches 30/33
100% Functions 14/14
96.29% Lines 52/54

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168                              55x 55x   55x 55x   5x     55x               43x     43x 1x 1x       43x 41x               41x             73x 73x 23x 23x               8x 8x 3x               65x 65x             10x             9x             26x       44x 44x 44x         1395x         1395x 1395x 1395x     1395x 1143x   1010x       1010x 1010x   1010x 1010x       133x 133x 133x     1143x       1395x 1374x   21x         1395x 3x         1392x 1392x 1392x     1392x      
/**
 * SerialCharacterRenderer
 * 
 * Rendert eingehende Serial-Daten mit baudrate-basierter Verzögerung,
 * um das reale Verhalten eines Arduino Serial Monitors zu simulieren.
 * 
 * Features:
 * - Queue-basiertes Character-Streaming
 * - requestAnimationFrame für smooth Rendering
 * - Baudrate → ms/char Berechnung
 * - Pause/Resume/Clear API
 * - Performance-optimiert für hohe Baudraten
 */
 
export class SerialCharacterRenderer {
  private queue: string = "";
  private paused: boolean = false;
  private baudrate: number | undefined;
  private lastCharTime: number = 0;
  private rafId: number | null = null;
  private readonly onChar: (char: string) => void;
  private static readonly MAX_QUEUE_SIZE = 50000; // ~50KB safety limit
 
  constructor(onChar: (char: string) => void) {
    this.onChar = onChar;
  }
 
  /**
   * Enqueue data for character-by-character rendering
   */
  enqueue(data: string): void {
    // Append new data to queue
    this.queue += data;
    
    // Enforce memory limit: Keep only last MAX_QUEUE_SIZE chars
    if (this.queue.length > SerialCharacterRenderer.MAX_QUEUE_SIZE) {
      const excess = this.queue.length - SerialCharacterRenderer.MAX_QUEUE_SIZE;
      this.queue = this.queue.slice(excess); // Drop oldest chars
    }
    
    // Start rendering if not already running and not paused
    if (!this.rafId && !this.paused && this.queue.length > 0) {
      this.start();
    }
  }
 
  /**
   * Set baudrate (affects rendering speed)
   */
  setBaudrate(baud: number | undefined): void {
    this.baudrate = baud;
  }
 
  /**
   * Pause character rendering
   */
  pause(): void {
    this.paused = true;
    if (this.rafId !== null) {
      cancelAnimationFrame(this.rafId);
      this.rafId = null;
    }
  }
 
  /**
   * Resume character rendering
   */
  resume(): void {
    this.paused = false;
    if (this.queue.length > 0 && this.rafId === null) {
      this.start();
    }
  }
 
  /**
   * Clear queue and stop rendering
   */
  clear(): void {
    this.queue = "";
    this.pause();
  }
 
  /**
   * Get current queue length (for debugging/UI feedback)
   */
  getQueueLength(): number {
    return this.queue.length;
  }
 
  /**
   * Check if renderer is currently active
   */
  isActive(): boolean {
    return this.rafId !== null;
  }
 
  /**
   * Destroy renderer and cleanup
   */
  destroy(): void {
    this.clear();
  }
 
  private start(): void {
    Iif (this.rafId !== null) return;
    this.lastCharTime = performance.now();
    this.rafId = requestAnimationFrame(() => this.tick());
  }
 
  private tick(): void {
    // Stop if paused or queue empty
    Iif (this.paused || this.queue.length === 0) {
      this.rafId = null;
      return;
    }
 
    const now = performance.now();
    const elapsed = now - this.lastCharTime;
    const msPerChar = this.calculateMsPerChar();
 
    // Check if enough time has passed to render next character(s)
    if (elapsed >= msPerChar) {
      if (msPerChar === 0 || msPerChar < 1) {
        // High baudrate or no baudrate: Batch render
        const charsToRender = msPerChar === 0 
          ? this.queue.length  // No baudrate: render all immediately
          : Math.min(Math.floor(elapsed / msPerChar), this.queue.length);
        
        const batch = this.queue.slice(0, charsToRender);
        this.queue = this.queue.slice(charsToRender);
        
        Eif (batch.length > 0) {
          this.onChar(batch);
        }
      } else {
        // Low baudrate: Render 1 character at a time
        const char = this.queue[0];
        this.queue = this.queue.slice(1);
        this.onChar(char);
      }
      
      this.lastCharTime = now;
    }
 
    // Continue if queue has more data
    if (this.queue.length > 0) {
      this.rafId = requestAnimationFrame(() => this.tick());
    } else {
      this.rafId = null;
    }
  }
 
  private calculateMsPerChar(): number {
    if (!this.baudrate) {
      return 0; // Immediate rendering when baudrate not set
    }
 
    // Baud = bits/second
    // Serial frame: 1 start bit + 8 data bits + 1 stop bit = 10 bits per byte
    const bytesPerSecond = this.baudrate / 10;
    const secondsPerByte = 1 / bytesPerSecond;
    const msPerByte = secondsPerByte * 1000;
 
    // Minimum 0.1ms to avoid excessive RAF calls at very high bauds
    return Math.max(0.1, msPerByte);
  }
}