LCOV - code coverage report
Current view: top level - src/workbench/extensions/agent/crdt - localWidgetWrites.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 9.1 % 22 2
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { isEqual } from 'es-toolkit'
       2              : 
       3              : import type { GraphOperation } from './graphOperations'
       4              : 
       5              : type SetWidgetOp = Extract<GraphOperation, { op: 'set_widget' }>
       6              : 
       7              : /**
       8              :  * Widget registers the live graph has written but the document has not yet
       9              :  * caught up on. A register is one last-writer-wins value: a remote frame that
      10              :  * writes it while a local write is still in flight holds the value the host
      11              :  * built before that write reached it, so applying the frame would rewind the
      12              :  * widget and lose what was typed since. Such a frame is held back until the
      13              :  * document holds the local value, or the write settles without reaching the
      14              :  * document (host skipped or refused it, lineage broke).
      15              :  *
      16              :  * Only the latest local value per register is kept: an older in-flight write
      17              :  * settling (its echo, its ack) does not lift the hold while a newer one is
      18              :  * still out. Interior (`path`) writes address subgraph definitions the applier
      19              :  * never projects, so they are not tracked.
      20              :  */
      21            1 : export class LocalWidgetWrites {
      22              :   private readonly pending = new Map<string, unknown>()
      23              : 
      24              :   note(operations: readonly GraphOperation[]): void {
      25            0 :     for (const op of topLevelSetWidgetOps(operations)) {
      26            0 :       this.pending.set(registerKey(op.node_id, op.widget), op.value)
      27              :     }
      28              :   }
      29              : 
      30              :   /** Lifts the hold of every register whose settled op carried its latest value. */
      31              :   settle(operations: readonly GraphOperation[]): void {
      32            0 :     for (const op of topLevelSetWidgetOps(operations)) {
      33            0 :       this.holds(String(op.node_id), op.widget, op.value)
      34              :     }
      35              :   }
      36              : 
      37              :   /** Lifts the hold of every register the document has caught up on. */
      38              :   settleAgainst(docValue: (nodeId: string, widget: string) => unknown): void {
      39            0 :     for (const key of Array.from(this.pending.keys())) {
      40            0 :       const [nodeId, widget] = splitKey(key)
      41            0 :       this.holds(nodeId, widget, docValue(nodeId, widget))
      42              :     }
      43              :   }
      44              : 
      45              :   /**
      46              :    * True while a local write to the register is in flight and `docValue` is
      47              :    * not it. A matching value settles the write and lets the frame through.
      48              :    */
      49              :   holds(nodeId: string, widget: string, docValue: unknown): boolean {
      50            0 :     const key = registerKey(nodeId, widget)
      51            0 :     if (!this.pending.has(key)) return false
      52            0 :     if (!isEqual(this.pending.get(key), docValue)) return true
      53            0 :     this.pending.delete(key)
      54            0 :     return false
      55              :   }
      56              : 
      57              :   clear(): void {
      58            0 :     this.pending.clear()
      59              :   }
      60              : }
      61              : 
      62            0 : function topLevelSetWidgetOps(
      63              :   operations: readonly GraphOperation[]
      64              : ): SetWidgetOp[] {
      65            0 :   return operations.filter(
      66              :     (op): op is SetWidgetOp => op.op === 'set_widget' && op.path == null
      67              :   )
      68              : }
      69              : 
      70            1 : const KEY_SEPARATOR = '\u0000'
      71              : 
      72            0 : function registerKey(nodeId: string | number, widget: string): string {
      73            0 :   return `${String(nodeId)}${KEY_SEPARATOR}${widget}`
      74              : }
      75              : 
      76            0 : function splitKey(key: string): [nodeId: string, widget: string] {
      77            0 :   const at = key.indexOf(KEY_SEPARATOR)
      78            0 :   return [key.slice(0, at), key.slice(at + KEY_SEPARATOR.length)]
      79              : }
        

Generated by: LCOV version 2.0-1