Skip to content

Commit 521afe2

Browse files
Merge pull request #1559 from liveview-native/reconnect_on_error
reconnect livechannel on error
2 parents 0bcff68 + 7a3384f commit 521afe2

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

Sources/LiveViewNative/Coordinators/LiveSessionCoordinator.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,7 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
266266
}
267267
}
268268

269-
let liveChannel = try await self.liveSocket!.joinLiveviewChannel(
270-
.some([
271-
"_format": .str(string: LiveSessionParameters.platform),
272-
"_interface": .object(object: LiveSessionParameters.platformParams)
273-
]),
274-
nil
275-
)
276-
277-
self.navigationPath.last!.coordinator.join(liveChannel)
269+
try await self.joinLiveViewChannel()
278270

279271
self.state = .connected
280272

@@ -328,6 +320,18 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
328320

329321
}
330322
}
323+
324+
func joinLiveViewChannel() async throws {
325+
let liveChannel = try await self.liveSocket!.joinLiveviewChannel(
326+
.some([
327+
"_format": .str(string: LiveSessionParameters.platform),
328+
"_interface": .object(object: LiveSessionParameters.platformParams)
329+
]),
330+
nil
331+
)
332+
333+
self.navigationPath.last?.coordinator.join(liveChannel)
334+
}
331335

332336
private func disconnect(preserveNavigationPath: Bool = false) async {
333337
do {

Sources/LiveViewNative/Coordinators/LiveViewCoordinator.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
270270
guard !Task.isCancelled else { return }
271271
do {
272272
switch event.event {
273+
case .phoenix(phoenix: .error):
274+
logger.error("encountered error in reply - channel reconnecting");
275+
if let liveChannel {
276+
let channel = liveChannel.channel()
277+
try await channel.shutdown()
278+
}
279+
try await session.joinLiveViewChannel()
273280
case .user(user: "diff"):
274281
switch event.payload {
275282
case let .jsonPayload(json):
@@ -342,6 +349,7 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
342349
}
343350

344351
func join(_ liveChannel: LiveViewNativeCore.LiveChannel) {
352+
345353
self.liveChannel = liveChannel
346354
let channel = liveChannel.channel()
347355
self.channel = channel

0 commit comments

Comments
 (0)