Skip to content

Commit 047d80a

Browse files
add reconnect logic to session handler
1 parent acd9099 commit 047d80a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Sources/LiveViewNative/Coordinators/LiveSessionCoordinator.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,17 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
292292
}
293293
}
294294

295+
func overrideLiveReloadChannel(channel: LiveChannel) async throws {
296+
297+
if let liveReloadChannel {
298+
try await liveReloadChannel.shutdownParentSocket()
299+
self.liveReloadChannel = nil
300+
}
301+
302+
self.liveReloadChannel = channel
303+
self.bindLiveReloadListener()
304+
}
305+
295306
func bindLiveReloadListener() {
296307
let eventListener = self.liveReloadChannel!.channel().events()
297308
self.liveReloadListenerLoop = Task { @MainActor [weak self] in

Sources/LiveViewNative/Live/LiveView.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import Foundation
99
import SwiftUI
1010
import Combine
11+
import LiveViewNativeCore
1112

1213
/// Create a ``LiveView`` with a list of addons.
1314
///
@@ -206,6 +207,16 @@ public struct LiveView<
206207
case .connecting:
207208
return .connecting
208209
case let .connectionFailed(error):
210+
211+
if let error = error as? LiveViewNativeCore.LiveSocketError,
212+
case let .ConnectionError(connectionError) = error {
213+
if let channel = connectionError.livereloadChannel {
214+
Task { @MainActor [weak session] in
215+
try await session?.overrideLiveReloadChannel(channel: channel)
216+
}
217+
}
218+
}
219+
209220
return .error(error)
210221
case .setup:
211222
return .connecting

0 commit comments

Comments
 (0)