Skip to content

Commit 58886cd

Browse files
reconnect strategies added
1 parent b19bb46 commit 58886cd

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Sources/LiveViewNative/Coordinators/LiveSessionConfiguration.swift

Lines changed: 24 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,26 @@ public struct LiveSessionConfiguration {
99100
public static let never: Self = .init()
100101
}
101102
}
103+
104+
105+
public final class ReconnectStrategyAdapter: SocketReconnectStrategy {
106+
107+
private let behavior: LiveSessionConfiguration.ReconnectBehavior
108+
109+
public init(_ behavior: LiveSessionConfiguration.ReconnectBehavior) {
110+
self.behavior = behavior
111+
}
112+
113+
public func sleepDuration(_ attempt: UInt64) -> TimeInterval {
114+
115+
guard let delay = behavior.delay else {
116+
return TimeInterval.greatestFiniteMagnitude
117+
}
118+
119+
let safeAttempt = min(Int(attempt), Int.max)
120+
return delay(safeAttempt)
121+
}
122+
123+
124+
}
125+

Sources/LiveViewNative/Coordinators/LiveSessionCoordinator.swift

Lines changed: 3 additions & 0 deletions
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,
@@ -225,6 +227,7 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
225227
method: httpMethod.flatMap(Method.init(_:)),
226228
timeoutMs: 10_000
227229
)
230+
adapter,
228231
)
229232

230233
// save cookies to storage

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 {

0 commit comments

Comments
 (0)