Skip to content

Commit f7de946

Browse files
authored
Merge pull request #1361 from liveview-native/macos-disconnected-view
Add separate `setup` and `disconnected` states
2 parents 9106ef2 + 61e5df4 commit f7de946

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

Sources/LiveViewNative/Coordinators/LiveSessionCoordinator.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private let logger = Logger(subsystem: "LiveViewNative", category: "LiveSessionC
3333
@MainActor
3434
public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
3535
/// The current state of the live view connection.
36-
@Published public private(set) var state = LiveSessionState.notConnected
36+
@Published public private(set) var state = LiveSessionState.setup
3737

3838
/// The current URL this live view is connected to.
3939
public private(set) var url: URL
@@ -103,11 +103,11 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
103103

104104
$navigationPath.scan(([LiveNavigationEntry<R>](), [LiveNavigationEntry<R>]()), { ($0.1, $1) }).sink { [weak self] prev, next in
105105
guard let self else { return }
106-
let isDisconnected: Bool
107-
if case .notConnected = next.last!.coordinator.state {
108-
isDisconnected = true
109-
} else {
110-
isDisconnected = false
106+
let isDisconnected = switch next.last!.coordinator.state {
107+
case .setup, .disconnected:
108+
true
109+
default:
110+
false
111111
}
112112
if next.last!.coordinator.url != next.last!.url || isDisconnected {
113113
Task {
@@ -151,15 +151,15 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
151151
///
152152
/// You generally do not call this function yourself. It is called automatically when the ``LiveView`` appears.
153153
///
154-
/// This function is a no-op unless ``state`` is ``LiveSessionState/notConnected``.
154+
/// This function is a no-op unless ``state`` is ``LiveSessionState/setup`` or ``LiveSessionState/disconnected`` or ``LiveSessionState/connectionFailed(_:)``.
155155
///
156156
/// This is an async function which completes when the connection has been established or failed.
157157
///
158158
/// - Parameter httpMethod: The HTTP method to use for the dead render. Defaults to `GET`.
159159
/// - Parameter httpBody: The HTTP body to send when requesting the dead render.
160160
public func connect(httpMethod: String? = nil, httpBody: Data? = nil) async {
161161
switch state {
162-
case .notConnected, .connectionFailed:
162+
case .setup, .disconnected, .connectionFailed:
163163
break
164164
default:
165165
return
@@ -252,7 +252,7 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
252252
}
253253
self.socket?.disconnect()
254254
self.socket = nil
255-
self.state = .notConnected
255+
self.state = .disconnected
256256
}
257257

258258
/// Forces the session to disconnect then connect.

Sources/LiveViewNative/Coordinators/LiveSessionState.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@ import Foundation
1010
/// The live view connection state.
1111
public enum LiveSessionState {
1212
/// The coordinator has not yet connected to the live view.
13-
case notConnected
13+
case setup
1414
/// The coordinator is attempting to connect.
1515
case connecting
1616
/// The coordinator is attempting to reconnect.
1717
case reconnecting
1818
/// The coordinator has connected and the view tree can be rendered.
1919
case connected
20-
// todo: disconnected state?
20+
/// The coordinator is disconnected.
21+
case disconnected
2122
/// The coordinator failed to connect and produced the given error.
2223
case connectionFailed(Error)
2324

24-
/// Either `notConnected` or `connecting`
25+
/// Either `setup` or `connecting`
2526
var isPending: Bool {
2627
switch self {
27-
case .notConnected,
28+
case .setup,
29+
.disconnected,
2830
.connecting,
2931
.reconnecting:
3032
return true

Sources/LiveViewNative/Coordinators/LiveViewCoordinator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private let logger = Logger(subsystem: "LiveViewNative", category: "LiveViewCoor
2626
/// - ``handleEvent(_:handler:)``
2727
@MainActor
2828
public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
29-
@Published internal private(set) var internalState: LiveSessionState = .notConnected
29+
@Published internal private(set) var internalState: LiveSessionState = .setup
3030

3131
var state: LiveSessionState {
3232
internalState
@@ -283,7 +283,7 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
283283
channel.on("phx_close") { [weak self, weak channel] message in
284284
Task { @MainActor in
285285
guard channel === self?.channel else { return }
286-
self?.internalState = .notConnected
286+
self?.internalState = .disconnected
287287
}
288288
}
289289

@@ -320,7 +320,7 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
320320
}
321321
await MainActor.run { [weak self] in
322322
self?.channel = nil
323-
self?.internalState = .notConnected
323+
self?.internalState = .disconnected
324324
}
325325
}
326326

Sources/LiveViewNative/Live/LiveView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ public struct LiveView<
201201
return .connecting
202202
case let .connectionFailed(error):
203203
return .error(error)
204-
case .notConnected:
204+
case .setup:
205+
return .connecting
206+
case .disconnected:
205207
return .disconnected
206208
case .reconnecting:
207209
return .reconnecting(_ConnectedContent<R>(session: session))

Sources/LiveViewNative/NavStackEntryView.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ struct NavStackEntryView<R: RootRegistry>: View {
3030

3131
private var phase: LiveViewPhase<R> {
3232
switch coordinator.state {
33-
case .notConnected:
34-
return .disconnected
33+
case .setup:
34+
return .connecting
3535
case .connecting:
3636
return .connecting
3737
case .connectionFailed(let error):
3838
return .error(error)
39+
case .disconnected:
40+
return .disconnected
3941
case .reconnecting, .connected: // these phases should always be handled internally
4042
fatalError()
4143
}

0 commit comments

Comments
 (0)