LCOV - code coverage report
Current view: top level - src/utils - syncUtil.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 92.9 % 14 13
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { api } from '@/scripts/api'
       2              : import type { UserDataFullInfo } from '@comfyorg/api-client/types'
       3              : 
       4              : /**
       5              :  * Sync entities from the API to the entityByPath map.
       6              :  * @param dir The directory to sync from
       7              :  * @param entityByPath The map to sync to
       8              :  * @param createEntity A function to create an entity from a file
       9              :  * @param updateEntity A function to update an entity from a file
      10              :  * @param exclude A function to exclude an entity
      11              :  * @param beforeDelete Called before deleting an entity that no longer exists
      12              :  * remotely. Return false to keep the entity.
      13              :  */
      14         4860 : export async function syncEntities<T>(
      15              :   dir: string,
      16              :   entityByPath: Record<string, T>,
      17              :   createEntity: (file: UserDataFullInfo & { path: string }) => T,
      18              :   updateEntity: (entity: T, file: UserDataFullInfo & { path: string }) => void,
      19              :   exclude: (file: T) => boolean = () => false,
      20              :   beforeDelete: (entity: T, path: string) => boolean = () => true
      21              : ) {
      22         4860 :   const files = (await api.listUserDataFullInfo(dir)).map((file) => ({
      23              :     ...file,
      24              :     path: dir ? `${dir}/${file.path}` : file.path
      25              :   }))
      26              : 
      27         4860 :   for (const file of files) {
      28         4126 :     const existingEntity = entityByPath[file.path]
      29              : 
      30         4126 :     if (!existingEntity) {
      31              :       // New entity, add it to the map
      32         2070 :       entityByPath[file.path] = createEntity(file)
      33         2056 :     } else if (exclude(existingEntity)) {
      34              :       // Entity has been excluded, skip it
      35            0 :       continue
      36              :     } else {
      37              :       // Entity has been modified, update its properties
      38         2056 :       updateEntity(existingEntity, file)
      39              :     }
      40              :   }
      41              : 
      42              :   // Remove entities that no longer exist
      43         4860 :   for (const [path, entity] of Object.entries(entityByPath)) {
      44        21084 :     if (exclude(entity)) continue
      45        15040 :     if (!files.some((file) => file.path === path)) {
      46          156 :       if (!beforeDelete(entity, path)) continue
      47          121 :       delete entityByPath[path]
      48              :     }
      49              :   }
      50              : }
        

Generated by: LCOV version 2.0-1