LCOV - code coverage report
Current view: top level - src/stores - widgetHistoryStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 67.0 % 94 63
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { cloneDeep, maxBy } from 'es-toolkit'
       2              : import { defineStore } from 'pinia'
       3              : import { shallowReactive } from 'vue'
       4              : 
       5              : import { parseWidgetId } from '@comfyorg/graph-model/widgetId'
       6              : import type { WidgetId } from '@comfyorg/graph-model/widgetId'
       7              : 
       8              : export const WIDGET_HISTORY_LIMIT = 10
       9              : 
      10         2356 : const NODE_HISTORY_WIDGET_TYPES: ReadonlySet<string> = new Set([
      11              :   'boundingbox',
      12              :   'boundingboxes',
      13              :   'colors',
      14              :   'curve',
      15              :   'imagecrop',
      16              :   'lightinfo',
      17              :   'painter',
      18              :   'range',
      19              :   'videoedit'
      20              : ])
      21              : 
      22        15492 : export function ownsNodeHistory(widget: { type?: string }): boolean {
      23        15492 :   return !!widget.type && NODE_HISTORY_WIDGET_TYPES.has(widget.type)
      24              : }
      25              : 
      26              : export interface WidgetHistoryScope {
      27              :   rootGraphId: string
      28              :   widgetId: WidgetId
      29              : }
      30              : 
      31              : export type HistoryDirection = 'undo' | 'redo'
      32              : 
      33              : interface HistoryNode {
      34              :   rootGraphId: string
      35              :   graphId: string
      36              :   nodeId: string
      37              : }
      38              : 
      39            0 : function isOnNode(scope: WidgetHistoryScope | null, node: HistoryNode) {
      40            0 :   if (scope?.rootGraphId !== node.rootGraphId) return false
      41            0 :   const { graphId, nodeId } = parseWidgetId(scope.widgetId)
      42            0 :   return graphId === node.graphId && String(nodeId) === node.nodeId
      43              : }
      44              : 
      45              : interface WidgetHistoryTraveler {
      46              :   scope: () => WidgetHistoryScope | null
      47              :   canTravel: (direction: HistoryDirection) => boolean
      48              :   travel: (direction: HistoryDirection) => void | Promise<void>
      49              : }
      50              : 
      51              : interface WidgetHistory {
      52              :   readonly past: readonly StoredStep[]
      53              :   readonly future: readonly StoredStep[]
      54              :   readonly mergeKey?: string
      55              :   readonly baseline?: HistoryStep
      56              : }
      57              : 
      58              : interface HistoryStep {
      59              :   readonly value: unknown
      60              : }
      61              : 
      62              : interface StoredStep extends HistoryStep {
      63              :   readonly order: number
      64              : }
      65              : 
      66         2356 : let lastHistoryOrder = 0
      67              : 
      68           67 : function nextHistoryOrder(): number {
      69           67 :   return ++lastHistoryOrder
      70              : }
      71              : 
      72         2356 : const storedByReference = new WeakSet<object>()
      73              : 
      74              : /** Marks a value that widget history keeps by reference instead of copying. */
      75           18 : export function asHistoryReference<T extends object>(value: T): T {
      76           18 :   storedByReference.add(value)
      77           18 :   return value
      78              : }
      79              : 
      80          127 : function snapshot<T>(value: T): T {
      81          127 :   return typeof value === 'object' &&
      82              :     value !== null &&
      83              :     storedByReference.has(value)
      84              :     ? value
      85              :     : cloneDeep(value)
      86              : }
      87              : 
      88           58 : function storedStep(value: unknown): StoredStep {
      89           58 :   return { value: snapshot(value), order: nextHistoryOrder() }
      90              : }
      91              : 
      92         2356 : const EMPTY_HISTORY: WidgetHistory = { past: [], future: [] }
      93              : 
      94           67 : function recordStep(
      95              :   history: WidgetHistory,
      96              :   previous: unknown,
      97              :   mergeKey: string | undefined
      98              : ): WidgetHistory {
      99           67 :   if (
     100              :     mergeKey !== undefined &&
     101              :     mergeKey === history.mergeKey &&
     102              :     history.future.length === 0
     103              :   ) {
     104            9 :     const merged = history.past.at(-1)
     105            9 :     return merged
     106              :       ? {
     107              :           ...history,
     108              :           past: [
     109              :             ...history.past.slice(0, -1),
     110              :             { ...merged, order: nextHistoryOrder() }
     111              :           ]
     112              :         }
     113              :       : history
     114              :   }
     115           58 :   return {
     116              :     ...history,
     117              :     past: [...history.past, storedStep(previous)].slice(-WIDGET_HISTORY_LIMIT),
     118              :     future: [],
     119              :     mergeKey
     120              :   }
     121              : }
     122              : 
     123         2356 : export const useWidgetHistoryStore = defineStore('widgetHistory', () => {
     124          873 :   const workflows = shallowReactive(
     125              :     new Map<string, Map<WidgetId, WidgetHistory>>()
     126              :   )
     127              : 
     128          873 :   const travelers = new Set<WidgetHistoryTraveler>()
     129              : 
     130          586 :   function historyOf({ rootGraphId, widgetId }: WidgetHistoryScope) {
     131          586 :     return workflows.get(rootGraphId)?.get(widgetId) ?? EMPTY_HISTORY
     132              :   }
     133              : 
     134          118 :   function nextStep(scope: WidgetHistoryScope, direction: HistoryDirection) {
     135          118 :     const { past, future } = historyOf(scope)
     136          118 :     return (direction === 'undo' ? past : future).at(-1)
     137              :   }
     138              : 
     139          117 :   function save(
     140              :     { rootGraphId, widgetId }: WidgetHistoryScope,
     141              :     history: WidgetHistory
     142              :   ) {
     143          117 :     const histories = new Map(workflows.get(rootGraphId))
     144          117 :     histories.set(widgetId, history)
     145          117 :     workflows.set(rootGraphId, histories)
     146              :   }
     147              : 
     148           67 :   function record(
     149              :     scope: WidgetHistoryScope,
     150              :     previous: unknown,
     151              :     mergeKey?: string
     152              :   ): void {
     153           67 :     save(scope, recordStep(historyOf(scope), previous, mergeKey))
     154              :   }
     155              : 
     156            0 :   function undo(
     157              :     scope: WidgetHistoryScope,
     158              :     current: unknown
     159              :   ): HistoryStep | undefined {
     160            0 :     const history = historyOf(scope)
     161            0 :     const { past, future } = history
     162            0 :     const step = past.at(-1)
     163            0 :     if (!step) return undefined
     164            0 :     save(scope, {
     165              :       ...history,
     166              :       mergeKey: undefined,
     167              :       past: past.slice(0, -1),
     168              :       future: [...future, storedStep(current)]
     169              :     })
     170            0 :     return { value: snapshot(step.value) }
     171              :   }
     172              : 
     173            0 :   function redo(
     174              :     scope: WidgetHistoryScope,
     175              :     current: unknown
     176              :   ): HistoryStep | undefined {
     177            0 :     const history = historyOf(scope)
     178            0 :     const { past, future } = history
     179            0 :     const step = future.at(-1)
     180            0 :     if (!step) return undefined
     181            0 :     save(scope, {
     182              :       ...history,
     183              :       mergeKey: undefined,
     184              :       past: [...past, storedStep(current)],
     185              :       future: future.slice(0, -1)
     186              :     })
     187            0 :     return { value: snapshot(step.value) }
     188              :   }
     189              : 
     190           49 :   function baselineOf(scope: WidgetHistoryScope): HistoryStep | undefined {
     191           49 :     return historyOf(scope).baseline
     192              :   }
     193              : 
     194           49 :   function setBaseline(scope: WidgetHistoryScope, value: unknown): void {
     195           49 :     save(scope, { ...historyOf(scope), baseline: { value: snapshot(value) } })
     196              :   }
     197              : 
     198          118 :   function peek(
     199              :     scope: WidgetHistoryScope,
     200              :     direction: HistoryDirection
     201              :   ): HistoryStep | undefined {
     202          118 :     const step = nextStep(scope, direction)
     203          118 :     return step ? { value: snapshot(step.value) } : undefined
     204              :   }
     205              : 
     206          132 :   function registerTraveler(traveler: WidgetHistoryTraveler): () => void {
     207          132 :     travelers.add(traveler)
     208          132 :     return () => {
     209            0 :       travelers.delete(traveler)
     210              :     }
     211              :   }
     212              : 
     213            4 :   function latestTraveler(node: HistoryNode, direction: HistoryDirection) {
     214            4 :     return maxBy(
     215              :       [...travelers].flatMap((traveler) => {
     216            0 :         const scope = traveler.scope()
     217            0 :         const step =
     218              :           scope && isOnNode(scope, node) && traveler.canTravel(direction)
     219              :             ? nextStep(scope, direction)
     220              :             : undefined
     221            0 :         return step
     222              :           ? [{ order: step.order, travel: () => traveler.travel(direction) }]
     223              :           : []
     224              :       }),
     225              :       ({ order }) => order
     226              :     )
     227              :   }
     228              : 
     229          151 :   function canUndo(scope: WidgetHistoryScope): boolean {
     230          151 :     return historyOf(scope).past.length > 0
     231              :   }
     232              : 
     233          151 :   function canRedo(scope: WidgetHistoryScope): boolean {
     234          151 :     return historyOf(scope).future.length > 0
     235              :   }
     236              : 
     237         1455 :   function retainLive(
     238              :     rootGraphId: string,
     239              :     liveWidgetIds: Pick<ReadonlySet<WidgetId>, 'has'>
     240              :   ): void {
     241         1455 :     const histories = workflows.get(rootGraphId)
     242         1455 :     if (!histories) return
     243           13 :     const kept = new Map(
     244              :       [...histories].filter(([widgetId]) => liveWidgetIds.has(widgetId))
     245              :     )
     246           13 :     if (kept.size === histories.size) return
     247            0 :     if (kept.size) workflows.set(rootGraphId, kept)
     248            0 :     else workflows.delete(rootGraphId)
     249              :   }
     250              : 
     251            0 :   function forget({ rootGraphId, widgetId }: WidgetHistoryScope): void {
     252            0 :     const histories = workflows.get(rootGraphId)
     253            0 :     if (!histories?.has(widgetId)) return
     254            0 :     const kept = new Map(histories)
     255            0 :     kept.delete(widgetId)
     256            0 :     if (kept.size) workflows.set(rootGraphId, kept)
     257            0 :     else workflows.delete(rootGraphId)
     258              :   }
     259              : 
     260           50 :   function clearWorkflow(rootGraphId: string): void {
     261           50 :     workflows.delete(rootGraphId)
     262              :   }
     263              : 
     264           41 :   function storedSteps(): { scope: WidgetHistoryScope; value: unknown }[] {
     265           41 :     return [...workflows].flatMap(([rootGraphId, histories]) =>
     266              :       [...histories].flatMap(([widgetId, { past, future }]) =>
     267              :         [...past, ...future].map(({ value }) => ({
     268              :           scope: { rootGraphId, widgetId },
     269              :           value
     270              :         }))
     271              :       )
     272              :     )
     273              :   }
     274              : 
     275            1 :   function dropStep(scope: WidgetHistoryScope, value: unknown): void {
     276            1 :     const history = historyOf(scope)
     277            1 :     save(scope, {
     278              :       ...history,
     279              :       past: history.past.filter((step) => step.value !== value),
     280              :       future: history.future.filter((step) => step.value !== value)
     281              :     })
     282              :   }
     283              : 
     284          873 :   return {
     285              :     record,
     286              :     undo,
     287              :     redo,
     288              :     baselineOf,
     289              :     setBaseline,
     290              :     peek,
     291              :     registerTraveler,
     292              :     latestTraveler,
     293              :     canUndo,
     294              :     canRedo,
     295              :     retainLive,
     296              :     forget,
     297              :     clearWorkflow,
     298              :     storedSteps,
     299              :     dropStep
     300              :   }
     301              : })
        

Generated by: LCOV version 2.0-1