Skip to content

Commit 2cdcfae

Browse files
Add reconnect strategy callback interface (#1545)
1 parent 457ea7f commit 2cdcfae

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let package = Package(
2525
dependencies: [
2626
// Dependencies declare other packages that this package depends on.
2727
.package(url: "https://github.com/apple/swift-async-algorithms", from: "1.0.0"),
28-
.package(url: "https://github.com/liveview-native/liveview-native-core", exact: "0.4.1-rc-1"),
28+
.package(url: "https://github.com/liveview-native/liveview-native-core", exact: "0.4.1-rc-2"),
2929

3030
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
3131

Sources/LiveViewNative/Coordinators/LiveSessionConfiguration.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import SwiftUI
9+
import LiveViewNativeCore
910

1011
/// An object that configures the behavior of a ``LiveSessionCoordinator``.
1112
public struct LiveSessionConfiguration {
@@ -99,3 +100,22 @@ public struct LiveSessionConfiguration {
99100
public static let never: Self = .init()
100101
}
101102
}
103+
104+
105+
final class ReconnectStrategyAdapter: SocketReconnectStrategy {
106+
private let behavior: LiveSessionConfiguration.ReconnectBehavior
107+
108+
init(_ behavior: LiveSessionConfiguration.ReconnectBehavior) {
109+
self.behavior = behavior
110+
}
111+
112+
func sleepDuration(_ attempt: UInt64) -> TimeInterval {
113+
guard let delay = behavior.delay else {
114+
return TimeInterval.greatestFiniteMagnitude
115+
}
116+
117+
let safeAttempt = min(Int(attempt), Int.max)
118+
return delay(safeAttempt)
119+
}
120+
}
121+

Sources/LiveViewNative/Coordinators/LiveSessionCoordinator.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
216216
try await socket.shutdown()
217217
}
218218

219+
let adapter = ReconnectStrategyAdapter(self.configuration.reconnectBehavior)
220+
219221
self.liveSocket = try await LiveSocket(
220222
originalURL.absoluteString,
221223
LiveSessionParameters.platform,
@@ -224,7 +226,8 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
224226
body: httpBody,
225227
method: httpMethod.flatMap(Method.init(_:)),
226228
timeoutMs: 10_000
227-
)
229+
),
230+
adapter
228231
)
229232

230233
// save cookies to storage
@@ -289,6 +292,17 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
289292
}
290293
}
291294

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+
292306
func bindLiveReloadListener() {
293307
let eventListener = self.liveReloadChannel!.channel().events()
294308
self.liveReloadListenerLoop = Task { @MainActor [weak self] in

Sources/LiveViewNative/Live/LiveErrorView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public struct LiveErrorView<Fallback: View>: View {
2929
case let .ConnectionError(trace) = error
3030
{
3131
SwiftUI.VStack {
32-
WebErrorView(html: trace)
32+
WebErrorView(html: trace.errorText)
3333
#if os(watchOS) || os(tvOS)
3434
SwiftUI.Button {
3535
Task {

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)