Skip to content

Commit 689d680

Browse files
authored
Reconnect when returning from background (#1122)
* Disconnect/reconnect when backgrounded/active * Change state to `connecting` when rejoining * Add socket reconnect when returning from background * Cleanup * Revert visibility
1 parent f5f30c9 commit 689d680

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Sources/LiveViewNative/Coordinators/LiveViewCoordinator.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,12 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
282282
for try await joinResult in join(channel: channel) {
283283
switch joinResult {
284284
case .rendered(let payload):
285-
self.handleJoinPayload(renderedPayload: payload)
285+
await MainActor.run {
286+
self.internalState = .connecting
287+
}
288+
await MainActor.run {
289+
self.handleJoinPayload(renderedPayload: payload)
290+
}
286291
case .redirect(let liveRedirect):
287292
self.url = liveRedirect.to
288293
try await self.connect(domValues: domValues, redirect: true)

Sources/LiveViewNative/LiveView.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public struct LiveView<R: RootRegistry>: View {
5151

5252
@ObservedObject private var rootCoordinator: LiveViewCoordinator<R>
5353

54+
@Environment(\.scenePhase) private var scenePhase
55+
5456
/// Creates a new LiveView attached to the given coordinator.
5557
///
5658
/// - Note: Changing coordinators after the `LiveView` is setup and connected is forbidden.
@@ -141,6 +143,14 @@ public struct LiveView<R: RootRegistry>: View {
141143
.task {
142144
await storage.session.connect()
143145
}
146+
.onChange(of: scenePhase) { newValue in
147+
guard case .active = newValue,
148+
storage.session.socket?.isConnected == false
149+
else { return }
150+
Task {
151+
await storage.session.connect()
152+
}
153+
}
144154
}
145155

146156
@ViewBuilder

0 commit comments

Comments
 (0)