All files / src/process registry.ts

100% Statements 16/16
100% Branches 4/4
100% Functions 8/8
100% Lines 16/16

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                                36x             37x               4x                 45x               4x                 2x 2x 1x     1x             1x                   16x 16x 15x 15x   1x               11x      
/**
 * Process Registry
 *
 * In-memory registry for tracking managed processes.
 * Provides CRUD operations for process metadata.
 *
 * Note: This is in-memory for now. Phase 2 adds SQLite persistence.
 */
 
import type { ProcessInfo } from './types.js';
 
/**
 * Registry for tracking managed processes.
 * Stores process information in memory using a Map.
 */
export class ProcessRegistry {
  private processes: Map<string, ProcessInfo> = new Map();
 
  /**
   * Register a new process in the registry.
   * @param info - Process information to register
   */
  register(info: ProcessInfo): void {
    this.processes.set(info.id, info);
  }
 
  /**
   * Remove a process from the registry.
   * @param id - Process ID to unregister
   */
  unregister(id: string): void {
    this.processes.delete(id);
  }
 
  /**
   * Get a process by its ID.
   * @param id - Process ID to look up
   * @returns Process info if found, undefined otherwise
   */
  get(id: string): ProcessInfo | undefined {
    return this.processes.get(id);
  }
 
  /**
   * Get all registered processes.
   * @returns Array of all process info objects
   */
  getAll(): ProcessInfo[] {
    return Array.from(this.processes.values());
  }
 
  /**
   * Find a process by its operating system PID.
   * @param pid - OS process ID to search for
   * @returns Process info if found, undefined otherwise
   */
  getByPid(pid: number): ProcessInfo | undefined {
    for (const process of this.processes.values()) {
      if (process.pid === pid) {
        return process;
      }
    }
    return undefined;
  }
 
  /**
   * Clear all processes from the registry.
   */
  clear(): void {
    this.processes.clear();
  }
 
  /**
   * Update a process's status in the registry.
   * @param id - Process ID to update
   * @param status - New status value
   * @returns true if process was found and updated, false otherwise
   */
  updateStatus(id: string, status: ProcessInfo['status']): boolean {
    const process = this.processes.get(id);
    if (process) {
      process.status = status;
      return true;
    }
    return false;
  }
 
  /**
   * Get the count of registered processes.
   * @returns Number of processes in the registry
   */
  get size(): number {
    return this.processes.size;
  }
}