Skip to content

fix(supabase_flutter): simplify lifecycle reconnection with serial Future chain#1340

Open
brunovsiqueira wants to merge 1 commit intosupabase:mainfrom
brunovsiqueira:fix/realtime-lifecycle-queue
Open

fix(supabase_flutter): simplify lifecycle reconnection with serial Future chain#1340
brunovsiqueira wants to merge 1 commit intosupabase:mainfrom
brunovsiqueira:fix/realtime-lifecycle-queue

Conversation

@brunovsiqueira
Copy link
Copy Markdown
Contributor

@brunovsiqueira brunovsiqueira commented Mar 27, 2026

Summary

Follow-up to #1321. Replaces the complex lifecycle state tracking (_disconnectFuture, _realtimeReconnectOperation, cancel flag) with a serial Future chain + target-state pattern, as suggested by @icnahom in the review.

  • Serial queue: _pendingLifecycleOperation ensures disconnect/connect never overlap
  • Stale-skip: _targetLifecycleState cancels outdated operations (e.g. abort reconnect if app backgrounded again)
  • Idempotency guards on both connect and disconnect paths
  • 3 state variables → 2, ~80 lines → ~30 lines

Test plan

  • Existing lifecycle tests pass
  • Added: rapid paused → resumed → paused → resumed flapping ends connected
  • Added: paused → resumed → paused before connect completes ends disconnected
  • Full supabase_flutter test suite passes
  • dart format and dart analyze clean

…uture chain

Replace complex lifecycle reconnection logic (_disconnectFuture,
_realtimeReconnectOperation, cancel flag) with a serial Future chain
and target-state pattern.

Operations are serialized via _pendingLifecycleOperation so disconnect
and connect never overlap. _targetLifecycleState tracks the most recent
lifecycle request, allowing stale operations to be skipped and
reconnects to be cancelled if the app goes back to background.

Add idempotency guards on both connect (isConnected) and disconnect
(isConnected || isConnecting) paths.

Add tests for rapid lifecycle flapping and cancellation edge cases.
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.

1 participant