There is a small Vue 3.5 Composition API component for editing a message draft. It shows a local text preview and, when autosave mode is enabled, sends a deferred save 300 ms after the text changes. Also, when the component is open, a global Escape key is registered: it should clear the text only while autosave is enabled and the component is mounted.
After one of the PRs, the behavior became unstable:
A diff review is needed to find the causes of the problems.
Requirements and assumptions:
saveDraft(text) returns Promise<void> and must be called only for the latest current text version after a 300 ms pause;modelValue only when autosave is true and the component still exists.How to reproduce:
autosave=true.modelValue several times in a row faster than 300 ms.autosave to false before the timer expires.Expected behavior: only the last text is saved, and after disabling the mode or leaving the page there are no leftover actions.
Vue 3.5.13 Composition API, TypeScript 5.8.2, Vite 5, browser, ES2022. Additionally, the standard DOM API window.addEventListener/removeEventListener and setTimeout/clearTimeout are assumed. No external libraries besides Vue.
Use line comments to explain the problem, its impact, the fix and how to test it.