Skip to content

Commit 4bcf417

Browse files
committed
fix(snippets): prevent type watcher from overwriting fetched editor data
The type watcher was firing after fetchSnippet set the correct value in typeToValueMap, overwriting it with a stale format conversion. Guard with isFetching flag and await nextTick so the deferred watcher sees the flag.
1 parent ed02bd3 commit 4bcf417

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/views/extra-features/snippets/composables/use-snippet-editor.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { omit } from 'es-toolkit/compat'
22
import { dump, load } from 'js-yaml'
33
import JSON5 from 'json5'
4-
import { computed, reactive, ref, watch } from 'vue'
4+
import { computed, nextTick, reactive, ref, watch } from 'vue'
55
import { toast } from 'vue-sonner'
66
import type { Ref } from 'vue'
77

@@ -25,6 +25,7 @@ export function useSnippetEditor(selectedId: Ref<string | null>) {
2525
})
2626

2727
let jsonFormatBeforeType: SnippetType = SnippetType.JSON
28+
let isFetching = false
2829

2930
const isNew = computed(() => !selectedId.value)
3031
const isFunctionType = computed(
@@ -40,6 +41,8 @@ export function useSnippetEditor(selectedId: Ref<string | null>) {
4041
watch(
4142
() => editData.value.type,
4243
(type, beforeType) => {
44+
if (isFetching) return
45+
4346
if (type === 'function' || type === 'text') {
4447
editData.value.raw = typeToValueMap[type]
4548

@@ -92,9 +95,12 @@ export function useSnippetEditor(selectedId: Ref<string | null>) {
9295
data.raw = JSON.stringify(JSON5.parse(data.raw), null, 2)
9396
}
9497

98+
isFetching = true
9599
editData.value = data
96100
jsonFormatBeforeType = data.type
97101
typeToValueMap[data.type] = data.raw
102+
await nextTick()
103+
isFetching = false
98104
}
99105

100106
const reset = (reference?: string) => {

0 commit comments

Comments
 (0)