Skip to content

Commit 1065256

Browse files
committed
fix: prevent race conditions while creating options
Signed-off-by: Christian Hartmann <chris-hartmann@gmx.de>
1 parent e4cab38 commit 1065256

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

src/components/Questions/AnswerInput.vue

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,27 @@ export default {
347347
const answer = { ...this.answer }
348348
answer.text = value
349349
350-
// Dispatched for creation. Marked as synced
351-
this.$set(this.answer, 'local', false)
352-
const newAnswer = await this.createAnswer(answer)
350+
// Prevent any queued debounced PATCHes from running while creating
351+
this.queue.pause()
352+
try {
353+
const newAnswer = await this.createAnswer(answer)
354+
if (!newAnswer || !newAnswer.id) {
355+
// Creation failed or response invalid — keep the local input as-is
356+
return
357+
}
353358
354-
// Forward changes, but use current answer.text to avoid erasing
355-
// any in-between changes while creating the answer
356-
newAnswer.text = this.$refs.input.value
357-
this.localText = ''
359+
// Forward changes, but use current input value to avoid erasing
360+
// any in-between changes while creating the answer
361+
const currentText = this.$refs.input?.value ?? ''
362+
newAnswer.text = currentText
363+
this.localText = ''
358364
359-
this.$emit('create-answer', this.index, newAnswer)
365+
this.$emit('create-answer', this.index, newAnswer)
366+
} finally {
367+
// Clear pending update tasks (stale PATCHes) before resuming processing
368+
this.queue.clear()
369+
this.queue.start()
370+
}
360371
},
361372
362373
/**

0 commit comments

Comments
 (0)