-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from ably-labs/12-associated-value-RoomLifecycle
Move error inside `RoomLifecycle`
- Loading branch information
Showing
6 changed files
with
110 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Ably | ||
import AblyChat | ||
|
||
extension RoomLifecycle { | ||
var error: ARTErrorInfo? { | ||
switch self { | ||
case let .failed(error): | ||
error | ||
case let .suspended(error): | ||
error | ||
case .initialized, | ||
.attached, | ||
.attaching, | ||
.detached, | ||
.detaching, | ||
.releasing, | ||
.released: | ||
nil | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
Tests/AblyChatTests/Helpers/Subscription+RoomStatusChange.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Ably | ||
import AblyChat | ||
|
||
/// Extensions for filtering a subscription by a given case, and then providing access to the values associated with these cases. | ||
/// | ||
/// This provides better ergonomics than writing e.g. `failedStatusChange = await subscription.first { $0.isSuspended }`, because it means that you don’t have to write another `if case` (or equivalent) to get access to the associated value of `failedStatusChange.current`. | ||
extension Subscription where Element == RoomStatusChange { | ||
struct StatusChangeWithError { | ||
/// A status change whose `current` has an associated error; ``error`` provides access to this error. | ||
var statusChange: RoomStatusChange | ||
/// The error associated with `statusChange.current`. | ||
var error: ARTErrorInfo | ||
} | ||
|
||
func suspendedElements() async -> AsyncCompactMapSequence<Subscription<RoomStatusChange>, Subscription<RoomStatusChange>.StatusChangeWithError> { | ||
compactMap { statusChange in | ||
if case let .suspended(error) = statusChange.current { | ||
StatusChangeWithError(statusChange: statusChange, error: error) | ||
} else { | ||
nil | ||
} | ||
} | ||
} | ||
|
||
func failedElements() async -> AsyncCompactMapSequence<Subscription<RoomStatusChange>, Subscription<RoomStatusChange>.StatusChangeWithError> { | ||
compactMap { statusChange in | ||
if case let .failed(error) = statusChange.current { | ||
StatusChangeWithError(statusChange: statusChange, error: error) | ||
} else { | ||
nil | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters