LCOV - code coverage report
Current view: top level - src/platform/surveys - useFeatureUsageTracker.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 84.6 % 39 33
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { useStorage } from '@vueuse/core'
       2              : import { computed } from 'vue'
       3              : 
       4              : interface FeatureUsage {
       5              :   useCount: number
       6              :   firstUsed: number
       7              :   lastUsed: number
       8              : }
       9              : 
      10              : type FeatureUsageRecord = Partial<Record<string, FeatureUsage>>
      11              : 
      12         2355 : const STORAGE_KEY = 'Comfy.FeatureUsage'
      13         2355 : const pendingUsage = new Map<string, FeatureUsage>()
      14              : 
      15          128 : function latestUsage(
      16              :   storedUsage: FeatureUsage | undefined,
      17              :   currentUsage: FeatureUsage | undefined
      18              : ) {
      19          128 :   if (!storedUsage) return currentUsage
      20           60 :   if (!currentUsage) return storedUsage
      21          128 :   return storedUsage.useCount >= currentUsage.useCount
      22              :     ? storedUsage
      23              :     : currentUsage
      24              : }
      25              : 
      26          128 : function incrementUsage(
      27              :   usage: FeatureUsage | undefined,
      28              :   now: number
      29              : ): FeatureUsage {
      30          128 :   return {
      31              :     useCount: (usage?.useCount ?? 0) + 1,
      32              :     firstUsed: usage?.firstUsed ?? now,
      33              :     lastUsed: now
      34              :   }
      35              : }
      36              : 
      37          128 : function applyPendingUsage(usageData: FeatureUsageRecord) {
      38          128 :   return {
      39              :     ...usageData,
      40              :     ...Object.fromEntries(
      41              :       [...pendingUsage].map(([featureId, usage]) => [
      42              :         featureId,
      43              :         latestUsage(usageData[featureId], usage)
      44              :       ])
      45              :     )
      46              :   }
      47              : }
      48              : 
      49          128 : function persistUsageData(
      50              :   featureId: string,
      51              :   currentUsageData: FeatureUsageRecord,
      52              :   now: number
      53              : ) {
      54          128 :   try {
      55          128 :     const oldValue = localStorage.getItem(STORAGE_KEY)
      56              :     const storedUsageData = oldValue
      57              :       ? (JSON.parse(oldValue) as FeatureUsageRecord)
      58              :       : {}
      59          128 :     const mergedUsageData = applyPendingUsage(storedUsageData)
      60          128 :     const usageData = {
      61              :       ...mergedUsageData,
      62              :       [featureId]: incrementUsage(
      63              :         latestUsage(mergedUsageData[featureId], currentUsageData[featureId]),
      64              :         now
      65              :       )
      66              :     }
      67          128 :     const newValue = JSON.stringify(usageData)
      68              : 
      69          128 :     localStorage.setItem(STORAGE_KEY, newValue)
      70          128 :     pendingUsage.clear()
      71          128 :     window.dispatchEvent(
      72              :       new StorageEvent('storage', {
      73              :         key: STORAGE_KEY,
      74              :         oldValue,
      75              :         newValue,
      76              :         storageArea: localStorage
      77              :       })
      78              :     )
      79          128 :     return usageData
      80              :   } catch {
      81            0 :     return
      82              :   }
      83              : }
      84              : 
      85              : /**
      86              :  * Tracks feature usage for survey eligibility.
      87              :  * Persists to localStorage.
      88              :  */
      89         6825 : export function useFeatureUsageTracker(featureId: string) {
      90         6825 :   const usageData = useStorage<FeatureUsageRecord>(STORAGE_KEY, {})
      91              : 
      92         6825 :   const usage = computed(() => usageData.value[featureId])
      93         6825 :   const useCount = computed(() => usage.value?.useCount ?? 0)
      94              : 
      95          128 :   function trackUsage() {
      96          128 :     const now = Date.now()
      97          128 :     const currentUsageData = usageData.value
      98          128 :     const existing = currentUsageData[featureId]
      99              : 
     100          128 :     const persistedUsageData = persistUsageData(
     101              :       featureId,
     102              :       currentUsageData,
     103              :       now
     104              :     )
     105          128 :     const nextUsageData = persistedUsageData ?? {
     106              :       ...currentUsageData,
     107              :       [featureId]: incrementUsage(
     108              :         latestUsage(pendingUsage.get(featureId), existing),
     109              :         now
     110              :       )
     111              :     }
     112          128 :     if (!persistedUsageData) {
     113            0 :       const nextUsage = nextUsageData[featureId]
     114            0 :       if (nextUsage) pendingUsage.set(featureId, nextUsage)
     115              :     }
     116          128 :     usageData.value = nextUsageData
     117              :   }
     118              : 
     119            0 :   function reset() {
     120            0 :     delete usageData.value[featureId]
     121            0 :     pendingUsage.delete(featureId)
     122              :   }
     123              : 
     124         6825 :   return {
     125              :     usage,
     126              :     useCount,
     127              :     trackUsage,
     128              :     reset
     129              :   }
     130              : }
        

Generated by: LCOV version 2.0-1