Skip to content

Conversation

@grdsdev
Copy link
Contributor

@grdsdev grdsdev commented Nov 17, 2025

Summary

This PR improves the Realtime implementation with better task lifecycle management, enhanced cancellation handling, and exposes necessary public APIs. It includes both the implementation changes and comprehensive tests.

Changes

Implementation Changes

RealtimeChannelV2.swift:

  • Make topic property public for external access
  • Make config property publicly readable (with private setter) for inspection

RealtimeClientV2.swift:

  • Add proper task cancellation in listenForMessages() before creating new message tasks
  • Add proper task cancellation in startHeartbeating() before creating new heartbeat tasks
  • Add additional cancellation check after processing text messages to be more responsive
  • Simplify heartbeat AsyncStream initialization (remove redundant type casting)
  • Format WebSocketTransport typealias for better readability

Tests Added

RealtimeChannelTests.swift (2 new tests):

  • testTopicIsPubliclyAccessible: Verifies the topic property is publicly accessible
  • testConfigIsPubliclyReadable: Verifies the config property is publicly readable

RealtimeTests.swift (4 new tests):

  • testListenForMessagesCancelsExistingTask: Verifies listenForMessages() properly cancels previous tasks on reconnection
  • testStartHeartbeatingCancelsExistingTask: Verifies startHeartbeating() properly cancels previous tasks on reconnection
  • testMessageProcessingRespectsCancellation: Verifies message processing respects task cancellation
  • testMultipleReconnectionsHandleTaskLifecycleCorrectly: Verifies multiple connect/disconnect cycles manage task lifecycle correctly

Benefits

  1. Prevents Task Leaks: Old tasks are now properly cancelled before creating new ones during reconnections
  2. Better Cancellation Handling: More responsive to cancellation requests, preventing unnecessary work
  3. Public API Access: Consumers can now access topic and inspect config on channel instances
  4. Improved Code Quality: Cleaner code with simplified AsyncStream initialization

Test Results

✅ All 6 new tests pass
✅ All 133 existing Realtime tests continue to pass
✅ No regressions introduced

Breaking Changes

None. The changes are backward compatible:

  • New public APIs are additive only
  • Task lifecycle improvements are internal implementation details
  • Existing functionality remains unchanged

🤖 Generated with Claude Code

Add comprehensive tests for the recent Realtime changes:

- Test public accessibility of `topic` property in RealtimeChannelV2
- Test public readability of `config` property in RealtimeChannelV2
- Test that listenForMessages() properly cancels existing tasks on reconnection
- Test that startHeartbeating() properly cancels existing tasks on reconnection
- Test that message processing respects task cancellation
- Test that multiple reconnections handle task lifecycle correctly

These tests cover the task lifecycle management improvements that prevent
task leaks and ensure proper cancellation handling during reconnections.

All 6 new tests pass, and all 133 existing Realtime tests continue to pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@grdsdev grdsdev changed the title test(realtime): add tests for public API and task lifecycle improvements feat(realtime): improve task lifecycle management and expose public APIs Nov 17, 2025
@grdsdev grdsdev requested a review from a team November 17, 2025 16:34
@grdsdev grdsdev merged commit bab4ec0 into main Nov 17, 2025
24 checks passed
@grdsdev grdsdev deleted the test/realtime-api-and-task-lifecycle branch November 17, 2025 17:20
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.

3 participants