Hard
Implementing Debounced Auto-Save in a Real-Time Collaborative Document Editor
Your team is building a real-time collaborative document editor. You need to implement a debounced auto-save function that prevents too many API calls while users are typing. What's missing in the code below to make it work correctly?
1class DocumentEditor {2 constructor() {3 this.content = '';4 this.lastSaved = null;5 this.saveTimeout = null;6 }78 handleUserInput(newContent) {9 this.content = newContent;10 // Missing debounce implementation11 _______________12 }1314 async saveToServer() {15 try {16 await fetch('/api/save', {17 method: 'POST',18 body: JSON.stringify({ content: this.content }),19 headers: { 'Content-Type': 'application/json' }20 });21 this.lastSaved = new Date();22 } catch (error) {23 console.error('Failed to save:', error);24 }25 }26}