Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: Use Pinia store instead of window events for model refresh
Replaced window event-based cache invalidation with direct Pinia store
method calls for better type safety and encapsulation.

Changes:
- Added refreshModelsByType() to assetsStore to invalidate all node
  types for a given model category
- Upload wizard now calls store method directly instead of dispatching
  window events
- Removed event listener from useAssetWidgetData - no longer needed

Benefits:
- Better type safety (no CustomEvent casting)
- More testable (no global window dependency)
- Clearer data flow and dependencies

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
  • Loading branch information
luke-mino-altherr and claude committed Dec 8, 2025
commit b45f1796d9d11d72a83bedd6ad779ef93b1ddfae
12 changes: 6 additions & 6 deletions src/platform/assets/composables/useUploadModelWizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { computed, ref, watch } from 'vue'
import { st } from '@/i18n'
import type { AssetMetadata } from '@/platform/assets/schemas/assetSchema'
import { assetService } from '@/platform/assets/services/assetService'
import { useAssetsStore } from '@/stores/assetsStore'

interface WizardData {
url: string
Expand All @@ -18,6 +19,7 @@ interface ModelTypeOption {
}

export function useUploadModelWizard(modelTypes: Ref<ModelTypeOption[]>) {
const assetsStore = useAssetsStore()
const currentStep = ref(1)
const isFetchingMetadata = ref(false)
const isUploading = ref(false)
Expand Down Expand Up @@ -144,12 +146,10 @@ export function useUploadModelWizard(modelTypes: Ref<ModelTypeOption[]>) {
uploadStatus.value = 'success'
currentStep.value = 3

// Emit event for store to invalidate caches
window.dispatchEvent(
new CustomEvent('model-upload-success', {
detail: { modelType: selectedModelType.value }
})
)
// Refresh model caches for the uploaded model type
if (selectedModelType.value) {
await assetsStore.refreshModelsByType(selectedModelType.value)
}

return true
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { computed, toValue, watch } from 'vue'
import type { MaybeRefOrGetter } from 'vue'
import { useEventListener } from '@vueuse/core'

import { isCloud } from '@/platform/distribution/types'
import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
Expand Down Expand Up @@ -81,22 +80,6 @@ export function useAssetWidgetData(
{ immediate: true }
)

// Listen for model upload events and refetch data
if (typeof window !== 'undefined') {
useEventListener(window, 'model-upload-success', async (event: Event) => {
const currentNodeType = toValue(nodeType)
if (!currentNodeType) return

const customEvent = event as CustomEvent<{ modelType: string }>
const uploadedModelType = customEvent.detail?.modelType

// Only refetch if the uploaded model type matches this widget's category
if (uploadedModelType && uploadedModelType === category.value) {
await assetsStore.updateModelsForNodeType(currentNodeType)
}
})
}

return {
category,
assets,
Expand Down
32 changes: 28 additions & 4 deletions src/stores/assetsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,27 +322,50 @@ export const useAssetsStore = defineStore('assets', () => {
}
}

/**
* Invalidate and refresh model assets for all node types that use a specific model category
* @param modelType The model category (e.g., 'checkpoints', 'loras')
*/
async function refreshModelsByType(modelType: string): Promise<void> {
const { useModelToNodeStore } = await import('./modelToNodeStore')
const modelToNodeStore = useModelToNodeStore()

// Get all node types that use this model category
const providers = modelToNodeStore.getAllNodeProviders(modelType)
if (!providers.length) return

// Refresh each node type's cache
await Promise.all(
providers.map((provider) =>
updateModelsForNodeType(provider.nodeDef.name)
)
)
}

return {
modelAssetsByNodeType,
modelLoadingByNodeType,
modelErrorByNodeType,
updateModelsForNodeType
updateModelsForNodeType,
refreshModelsByType
}
}

return {
modelAssetsByNodeType: shallowReactive(new Map<string, AssetItem[]>()),
modelLoadingByNodeType: shallowReactive(new Map<string, boolean>()),
modelErrorByNodeType: shallowReactive(new Map<string, Error | null>()),
updateModelsForNodeType: async () => []
updateModelsForNodeType: async () => [],
refreshModelsByType: async () => {}
}
}

const {
modelAssetsByNodeType,
modelLoadingByNodeType,
modelErrorByNodeType,
updateModelsForNodeType
updateModelsForNodeType,
refreshModelsByType
} = getModelState()

return {
Expand All @@ -369,6 +392,7 @@ export const useAssetsStore = defineStore('assets', () => {
modelAssetsByNodeType,
modelLoadingByNodeType,
modelErrorByNodeType,
updateModelsForNodeType
updateModelsForNodeType,
refreshModelsByType
}
})
Loading