Skip to content

Conversation

harrytmthy
Copy link
Owner

Summary

This PR introduces SafeBoxStateManager, a dedicated component for coordinating SafeBox lifecycle states and concurrent write management.

It enables:

  • ✅ Accurate state transitions (STARTING, WRITING, IDLE, CLOSED)
  • ✅ Robust write tracking using atomic counters
  • ✅ Safe commit() and apply() sequencing
  • closeWhenIdle() support to defer closing until writes complete

Implementation Details

  • SafeBoxStateManager maintains:
    • Atomic write counter to support overlapping commit() / apply() calls
    • CompletableDeferred gating for initial load (launchWithStartingState)
    • Synchronized finalizeWriting() with accurate state emission
  • SafeBox now delegates all lifecycle and concurrency decisions to the manager
  • closeWhenIdle() is guaranteed to wait for both the initial read and all pending writes

This ensures lifecycle consistency and write safety, especially in async or ViewModel-scoped usage.

Closes #17

@harrytmthy harrytmthy self-assigned this Jun 4, 2025
@harrytmthy harrytmthy added enhancement New feature or request refactor labels Jun 4, 2025
@harrytmthy harrytmthy merged commit 7e317f6 into main Jun 4, 2025
3 checks passed
@harrytmthy harrytmthy deleted the feat/safebox-state-manager branch June 4, 2025 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce SafeBoxStateManager for better state orchestration
1 participant