LCOV - code coverage report
Current view: top level - src/composables - useWidgetValueHistory.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 58.5 % 41 24
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { tryOnScopeDispose } from '@vueuse/core'
       2              : import { cloneDeep, isEqual } from 'es-toolkit'
       3              : import { computed, nextTick } from 'vue'
       4              : 
       5              : import { useWidgetHistoryStore } from '@/stores/widgetHistoryStore'
       6              : import type { WidgetHistoryScope } from '@/stores/widgetHistoryStore'
       7              : 
       8              : interface UseWidgetValueHistoryOptions {
       9              :   scope: () => WidgetHistoryScope | null
      10              :   read: () => unknown
      11              :   apply: (value: unknown) => void
      12              : }
      13              : 
      14          101 : export function useWidgetValueHistory({
      15              :   scope,
      16              :   read,
      17              :   apply
      18              : }: UseWidgetValueHistoryOptions) {
      19          101 :   const store = useWidgetHistoryStore()
      20          101 :   let committed = cloneDeep(read())
      21              : 
      22           75 :   function baseline(target: WidgetHistoryScope | null) {
      23           75 :     const shared = target ? store.baselineOf(target) : undefined
      24           75 :     return shared ? shared.value : committed
      25              :   }
      26              : 
      27           75 :   function adopt(target: WidgetHistoryScope | null, value: unknown) {
      28           75 :     committed = cloneDeep(value)
      29           75 :     if (target) store.setBaseline(target, value)
      30              :   }
      31              : 
      32            0 :   function syncBaseline() {
      33            0 :     adopt(scope(), read())
      34              :   }
      35              : 
      36            0 :   function discardHistory() {
      37            0 :     const target = scope()
      38            0 :     if (target) store.forget(target)
      39            0 :     adopt(target, read())
      40              :   }
      41              : 
      42           75 :   function commit(mergeKey?: string) {
      43           75 :     const target = scope()
      44           75 :     const current = read()
      45           75 :     const previous = baseline(target)
      46           75 :     if (isEqual(current, previous)) return
      47           75 :     if (target) store.record(target, previous, mergeKey)
      48           75 :     adopt(target, current)
      49              :   }
      50              : 
      51            0 :   function travel(take: typeof store.undo) {
      52            0 :     const target = scope()
      53            0 :     const step = target ? take(target, read()) : undefined
      54            0 :     if (!step) return
      55            0 :     apply(step.value)
      56            0 :     adopt(target, step.value)
      57            0 :     void nextTick(syncBaseline)
      58              :   }
      59              : 
      60            0 :   function undo() {
      61            0 :     travel(store.undo)
      62              :   }
      63              : 
      64            0 :   function redo() {
      65            0 :     travel(store.redo)
      66              :   }
      67              : 
      68          101 :   const canUndo = computed(() => {
      69          151 :     const target = scope()
      70          151 :     return !!target && store.canUndo(target)
      71              :   })
      72          101 :   const canRedo = computed(() => {
      73          151 :     const target = scope()
      74          151 :     return !!target && store.canRedo(target)
      75              :   })
      76              : 
      77          101 :   tryOnScopeDispose(
      78              :     store.registerTraveler({
      79              :       scope,
      80              :       canTravel: (direction) =>
      81              :         direction === 'undo' ? canUndo.value : canRedo.value,
      82              :       travel: (direction) => (direction === 'undo' ? undo() : redo())
      83              :     })
      84              :   )
      85              : 
      86          101 :   return {
      87              :     canUndo,
      88              :     canRedo,
      89              :     commit,
      90              :     syncBaseline,
      91              :     discardHistory,
      92              :     undo,
      93              :     redo
      94              :   }
      95              : }
        

Generated by: LCOV version 2.0-1