feat(lora-randomizer): prevent early watch triggers by tracking mount state

Add isMounted ref to LoraRandomizerWidget to avoid premature updates from the loras widget watch. The watch now only responds after the component is fully mounted, and the onMounted hook captures the initial loras widget value before enabling the watcher. This prevents the watch from overwriting valid initial data with empty values during component initialization.
This commit is contained in:
Will Miao
2026-01-13 19:22:51 +08:00
parent 430ba84cf7
commit 9e510d64ec
3 changed files with 42 additions and 12 deletions

View File

@@ -51,6 +51,9 @@ const state = useLoraRandomizerState(props.widget)
// Track current loras from the loras widget
const currentLoras = ref<LoraEntry[]>([])
// Track if component is mounted to avoid early watch triggers
const isMounted = ref(false)
// Computed property to check if we can reuse last
const canReuseLast = computed(() => {
const lastUsed = state.lastUsed.value
@@ -142,13 +145,29 @@ const handleReuseLast = () => {
// Watch for changes to the loras widget to track current loras
watch(() => props.node.widgets?.find((w: any) => w.name === 'loras')?.value, (newVal) => {
if (newVal && Array.isArray(newVal)) {
currentLoras.value = newVal
// Only update after component is mounted
if (isMounted.value) {
if (newVal && Array.isArray(newVal)) {
currentLoras.value = newVal
}
}
}, { immediate: true, deep: true })
// Lifecycle
onMounted(async () => {
// IMPORTANT: Save the current loras widget value BEFORE setting isMounted to true
// This prevents the watch from overwriting an empty value
const lorasWidget = props.node.widgets?.find((w: any) => w.name === 'loras')
if (lorasWidget) {
const currentWidgetValue = lorasWidget.value
if (currentWidgetValue && Array.isArray(currentWidgetValue) && currentWidgetValue.length > 0) {
currentLoras.value = currentWidgetValue
}
}
// Mark component as mounted so watch can now respond to changes
isMounted.value = true
// Setup serialization
props.widget.serializeValue = async () => {
const config = state.buildConfig()