Skip to content

Commit c47d372

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

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

src/components/Questions/AnswerInput.vue

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,27 @@ export default {
278278
const answer = { ...this.answer }
279279
answer.text = value
280280
281-
// Dispatched for creation. Marked as synced
282-
this.$set(this.answer, 'local', false)
283-
const newAnswer = await this.createAnswer(answer)
284-
285-
// Forward changes, but use current answer.text to avoid erasing
286-
// any in-between changes while creating the answer
287-
newAnswer.text = this.$refs.input.value
288-
this.localText = ''
289-
290-
this.$emit('create-answer', this.index, newAnswer)
281+
// Prevent any queued debounced PATCHes from running while creating
282+
this.queue.pause()
283+
try {
284+
const newAnswer = await this.createAnswer(answer)
285+
if (!newAnswer || !newAnswer.id) {
286+
// Creation failed or response invalid — keep the local input as-is
287+
return
288+
}
289+
290+
// Forward changes, but use current input value to avoid erasing
291+
// any in-between changes while creating the answer
292+
const currentText = this.$refs.input?.value ?? ''
293+
newAnswer.text = currentText
294+
this.localText = ''
295+
296+
this.$emit('create-answer', this.index, newAnswer)
297+
} finally {
298+
// Clear pending update tasks (stale PATCHes) before resuming processing
299+
this.queue.clear()
300+
this.queue.start()
301+
}
291302
},
292303
293304
/**

0 commit comments

Comments
 (0)