Skip to content

fix: Task State Inconsistency - #11

Open
enochpromo wants to merge 1 commit into
scottpmiller:mainfrom
enochpromo:fix/Task-State-Inconsistency
Open

fix: Task State Inconsistency#11
enochpromo wants to merge 1 commit into
scottpmiller:mainfrom
enochpromo:fix/Task-State-Inconsistency

Conversation

@enochpromo

Copy link
Copy Markdown

MR Summary: Fix task persistence race conditions after refresh

  • Improved state updates and persistence to prevent lost/duplicated tasks after page refresh.
  • Replaced stale-closure patterns with functional state updates in useTasks.
  • Centralized persistence with a hydration-guarded effect; added debounce for write coalescing.
  • Added immediate saves on critical operations (add/toggle/delete) to ensure durability.
  • Stopped default writes during initial load to avoid write-before-read races.
  • Added robust tests simulating remount/refresh; mocked delays and debounce to stabilize.
    Key changes:
  • src/hooks/useTasks.ts
    • Functional setTasks, hydratedRef to gate writes.
    • Debounced save effect using APP_CONFIG.AUTO_SAVE_DELAY.
    • Immediate taskService.saveTasks(updatedTasks) on add/toggle/delete when hydrated.
  • src/services/taskService.ts
    • Removed save during loadTasks when defaults are used.
  • src/hooks/__tests__/useTasks.test.tsx
    • Added tests for add without refresh, add with remount, toggle+add with remount.
    • Mocked delays and disabled debounce for deterministic execution.
      Outcome:
  • Eliminates out-of-order localStorage writes and stale overwrites.
  • Tasks reliably persist across rapid actions and page reloads.
  • All tests passing.

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.

2 participants