Skip to content

Commit

Permalink
Allow construction of room status wrappers outside of subscription
Browse files Browse the repository at this point in the history
Will use in an upcoming test.
  • Loading branch information
lawrence-forooghian committed Nov 12, 2024
1 parent b8ea989 commit cee3d29
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions Tests/AblyChatTests/Helpers/Subscription+RoomStatusChange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,61 @@ extension Subscription where Element == RoomStatusChange {
var statusChange: RoomStatusChange
/// The error associated with `statusChange.current`.
var error: ARTErrorInfo

init(statusChange: RoomStatusChange, error: ARTErrorInfo) {
self.statusChange = statusChange
self.error = error
}

/// Returns a non-nil value and only if `maybeSuspendedStatusChange` has case `.suspended`.
init?(maybeSuspendedStatusChange: RoomStatusChange) {
if case let .suspended(error) = maybeSuspendedStatusChange.current {
self.init(statusChange: maybeSuspendedStatusChange, error: error)
} else {
return nil
}
}

/// Returns a non-nil value and only if `maybeFailedStatusChange` has case `.failed`.
init?(maybeFailedStatusChange: RoomStatusChange) {
if case let .suspended(error) = maybeFailedStatusChange.current {
self.init(statusChange: maybeFailedStatusChange, error: error)
} else {
return nil
}
}
}

struct StatusChangeWithOptionalError {
/// A status change whose `current` has an optional 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)
init(statusChange: RoomStatusChange, error: ARTErrorInfo? = nil) {
self.statusChange = statusChange
self.error = error
}

/// Returns a non-nil value and only if `maybeAttachingStatusChange` has case `.attaching`.
init?(maybeAttachingStatusChange: RoomStatusChange) {
if case let .attaching(error) = maybeAttachingStatusChange.current {
self.init(statusChange: maybeAttachingStatusChange, error: error)
} else {
nil
return nil
}
}
}

func suspendedElements() async -> AsyncCompactMapSequence<Subscription<RoomStatusChange>, Subscription<RoomStatusChange>.StatusChangeWithError> {
compactMap { .init(maybeSuspendedStatusChange: $0) }
}

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
}
}
compactMap { .init(maybeFailedStatusChange: $0) }
}

func attachingElements() async -> AsyncCompactMapSequence<Subscription<RoomStatusChange>, Subscription<RoomStatusChange>.StatusChangeWithOptionalError> {
compactMap { statusChange in
if case let .attaching(error) = statusChange.current {
StatusChangeWithOptionalError(statusChange: statusChange, error: error)
} else {
nil
}
}
compactMap { .init(maybeAttachingStatusChange: $0) }
}
}

0 comments on commit cee3d29

Please sign in to comment.