Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/components/Questions/AnswerInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
minlength="1"
type="text"
dir="auto"
@input="onInput"
@input="debounceOnInput"
@keydown.delete="deleteEntry"
@keydown.enter.prevent="focusNextInput" />

Expand Down Expand Up @@ -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,
}
},

Expand All @@ -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')
Expand All @@ -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)
}
},
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/mixins/QuestionMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}
},
Expand Down
Loading