Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ts-strict for modelStore.ts #1241

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/stores/modelStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-strict-ignore
import { api } from '@/scripts/api'
import { defineStore } from 'pinia'

Expand Down Expand Up @@ -54,7 +53,8 @@ export class ComfyModelDef {

constructor(name: string, directory: string) {
this.file_name = name
this.simplified_file_name = name.replaceAll('\\', '/').split('/').pop()
this.simplified_file_name =
name.replaceAll('\\', '/').split('/').pop() ?? ''
if (this.simplified_file_name.endsWith('.safetensors')) {
this.simplified_file_name = this.simplified_file_name.slice(
0,
Expand Down Expand Up @@ -156,12 +156,12 @@ const folderBlacklist = ['configs', 'custom_nodes']
/** Model store handler, wraps individual per-folder model stores */
export const useModelStore = defineStore('modelStore', {
state: () => ({
modelStoreMap: {} as Record<string, ModelStore>,
isLoading: {} as Record<string, Promise<ModelStore>>,
modelStoreMap: {} as Record<string, ModelStore | null>,
isLoading: {} as Record<string, Promise<ModelStore | null> | null>,
modelFolders: [] as string[]
}),
actions: {
async getModelsInFolderCached(folder: string): Promise<ModelStore> {
async getModelsInFolderCached(folder: string): Promise<ModelStore | null> {
if (folder in this.modelStoreMap) {
return this.modelStoreMap[folder]
}
Expand All @@ -174,7 +174,7 @@ export const useModelStore = defineStore('modelStore', {
}
const store = new ModelStore(folder, models)
this.modelStoreMap[folder] = store
this.isLoading[folder] = false
this.isLoading[folder] = null
return store
})
this.isLoading[folder] = promise
Expand Down
7 changes: 6 additions & 1 deletion tests-ui/tests/store/modelStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ describe('useModelStore', () => {
enableMocks()
const folderStore = await store.getModelsInFolderCached('checkpoints')
expect(folderStore).not.toBeNull()
expect(Object.keys(folderStore.models)).toHaveLength(3)
if (!folderStore) return
expect(Object.keys(folderStore.models).length).toBe(3)
})

it('should load model metadata', async () => {
enableMocks()
const folderStore = await store.getModelsInFolderCached('checkpoints')
expect(folderStore).not.toBeNull()
if (!folderStore) return
const model = folderStore.models['sdxl.safetensors']
await model.load()
expect(model.title).toBe('Title of sdxl.safetensors')
Expand All @@ -67,6 +70,8 @@ describe('useModelStore', () => {
it('should handle no metadata', async () => {
enableMocks()
const folderStore = await store.getModelsInFolderCached('checkpoints')
expect(folderStore).not.toBeNull()
if (!folderStore) return
const model = folderStore.models['noinfo.safetensors']
await model.load()
expect(model.file_name).toBe('noinfo.safetensors')
Expand Down
Loading