Topic: An older autosave response changes the editor status back to saved

Clara Finch free asked 1 week ago


We added autosave around our editor. While typing quickly on a slow connection, the status sometimes changes to Saved even though the newest change is still being sent. The document content on screen stays correct, but the status belongs to an earlier request.

Every save promise sets the same saved flag when it resolves. Our API stores a document revision, so the latest request can be identified. How should the wrapper ignore an outdated response when updating this status?


Mateusz Lazaru staff answered 1 week ago


Associate each save attempt with a sequence number or the document revision that it contains. Update the current status only when the response belongs to the newest outstanding attempt. An older promise resolving later should not mark newer unsaved work as saved.

Keep server-side revision checks as well: ignoring an old response in the UI does not prevent an older request from overwriting newer content on the server. Debouncing reduces requests but does not by itself establish their completion order.

const latestAttempt = useRef(0);
async function saveDraft(content) {
  const attempt = ++latestAttempt.current;
  setStatus('Saving');
  await persistDraft(content);
  if (attempt === latestAttempt.current) setStatus('Saved');
}

https://mdbootstrap.com/docs/react/plugins/wysiwyg-editor/


Clara Finch free commented 6 days ago

The request counter fixed responses arriving out of order. I found one remaining case: a save can finish after another edit but before that edit's debounce timer starts its request. Our change handler only sets the draft and marks it unsaved, so the old save still passes the counter check.

function onEditorChange(html) {
  setDraft(html);
  setStatus('Unsaved');
  debouncedSave(html);
}
// latestAttempt is currently incremented only inside saveDraft.

Mateusz Lazaru staff commented 6 days ago

Track the draft revision when the edit occurs rather than only when its request starts. Pass that revision through the debounced save, and let a response mark the draft saved only if its revision is still current. This covers the interval before the next request begins as well.

Keep the existing server-side revision protection. This client check controls the displayed status; the server still needs to reject or resolve an outdated write independently.

const draftRevision = useRef(0);
function onEditorChange(html) {
  const revision = ++draftRevision.current;
  setDraft(html);
  setStatus('Unsaved');
  debouncedSave(html, revision);
}
async function saveDraft(html, revision) {
  if (revision !== draftRevision.current) return;
  setStatus('Saving');
  await persistDraft(html);
  if (revision === draftRevision.current) setStatus('Saved');
}


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: MDB React
  • MDB Version: MDB5 9.0.0
  • Device: Desktop
  • Browser: Chrome
  • OS: macOS
  • Provided sample code: No
  • Provided link: No