From 6fa4e2170cafa5009b5776a3c9d12191ab427963 Mon Sep 17 00:00:00 2001 From: Christian Hartmann Date: Fri, 14 Feb 2025 12:10:03 +0100 Subject: [PATCH] Fix: Implement debounced input handling for AnswerInput component Signed-off-by: Christian Hartmann --- src/components/Questions/AnswerInput.vue | 29 ++++++++++++------------ src/mixins/QuestionMixin.js | 2 +- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/components/Questions/AnswerInput.vue b/src/components/Questions/AnswerInput.vue index 9c32ff4c4..f544d9de4 100644 --- a/src/components/Questions/AnswerInput.vue +++ b/src/components/Questions/AnswerInput.vue @@ -15,7 +15,7 @@ minlength="1" type="text" dir="auto" - @input="onInput" + @input="debounceOnInput" @keydown.delete="deleteEntry" @keydown.enter.prevent="focusNextInput" /> @@ -88,12 +88,8 @@ export default { data() { return { - queue: new PQueue({ concurrency: 1 }), - - // As data instead of Method, to have a separate debounce per AnswerInput - debounceUpdateAnswer: pDebounce(function (answer) { - return this.queue.add(() => this.updateAnswer(answer)) - }, 500), + queue: null, + debounceOnInput: null, } }, @@ -103,6 +99,15 @@ export default { }, }, + created() { + this.queue = new PQueue({ concurrency: 1 }) + + // As data instead of method, to have a separate debounce per AnswerInput + this.debounceOnInput = pDebounce(() => { + return this.queue.add(() => this.onInput()) + }, 500) + }, + methods: { handleTabbing() { this.$emit('tabbed-out') @@ -125,16 +130,15 @@ export default { if (this.answer.local) { // Dispatched for creation. Marked as synced - // eslint-disable-next-line vue/no-mutating-props - this.answer.local = false - const newAnswer = await this.debounceCreateAnswer(answer) + this.$set(this.answer, 'local', false) + const newAnswer = await this.createAnswer(answer) // Forward changes, but use current answer.text to avoid erasing // any in-between changes while creating the answer newAnswer.text = this.$refs.input.value this.$emit('update:answer', answer.id, newAnswer) } else { - this.debounceUpdateAnswer(answer) + await this.updateAnswer(answer) this.$emit('update:answer', answer.id, answer) } }, @@ -195,9 +199,6 @@ export default { return answer }, - debounceCreateAnswer: pDebounce(function (answer) { - return this.queue.add(() => this.createAnswer(answer)) - }, 100), /** * Save to the server, only do it after 500ms diff --git a/src/mixins/QuestionMixin.js b/src/mixins/QuestionMixin.js index 9c04279bd..0745707b8 100644 --- a/src/mixins/QuestionMixin.js +++ b/src/mixins/QuestionMixin.js @@ -468,7 +468,7 @@ export default { this.focusIndex(options.length - 1) // Trigger onInput on new AnswerInput for posting the new option to the API - this.$refs.input[options.length - 1].onInput() + this.$refs.input[options.length - 1].debounceOnInput() }) } },