Skip to content

fix: resolve auto-save race conditions and add conflict detection#235

Merged
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
collinsezedike:fix/autosave-race-conditions-195
Apr 27, 2026
Merged

fix: resolve auto-save race conditions and add conflict detection#235
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
collinsezedike:fix/autosave-race-conditions-195

Conversation

@collinsezedike
Copy link
Copy Markdown
Contributor

Summary

AutoSaveManager.tsx

  • Replaced onSaveSuccess/onSaveError in the useEffect dep array with callback refs — inline functions from the parent were causing the manager to be torn down and rebuilt on every render, creating a race between the outgoing destroy and the incoming enable
  • Added isSavingRef guard to the debounced save effect so concurrent saves (interval + debounce firing together) can't overlap

auto-save-manager.ts

  • Named the online/offline handlers and stored them in instance fields so destroy() can properly removeEventListener — previously they leaked on every unmount
  • Added conflict detection in saveNow: before writing, compares the stored draft's updatedAt against lastKnownUpdatedAt (set on load and after each successful save); aborts with an error status if the draft was modified externally (e.g. another tab)
  • destroy() now also clears lastKnownUpdatedAt

Test plan

  • Pass inline onSaveSuccess/onSaveError callbacks from a parent — manager should not rebuild on every render
  • Trigger rapid form changes — only one save should be in flight at a time
  • Open the same form in two tabs, edit in one, then edit in the other — second save should surface a conflict error
  • Unmount the component — no online/offline listeners should remain attached

Closes #195

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Apr 27, 2026

@collinsezedike Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER RUKAYAT-CODER merged commit a50ac1c into rinafcode:main Apr 27, 2026
3 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Form Auto-save Issues

2 participants