Skip to content

Commit

Permalink
Add tests-only method for subscribing to manager’s internal status
Browse files Browse the repository at this point in the history
Whem implementing #50, I intend to add further metadata to the manager’s
status, to be used by tests.
  • Loading branch information
lawrence-forooghian committed Nov 18, 2024
1 parent 3b60871 commit f3d6d87
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Sources/AblyChat/RoomLifecycleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,30 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
return subscription
}

#if DEBUG
// TODO: clean up old subscriptions (https://github.com/ably-labs/ably-chat-swift/issues/36)
/// Supports the ``testsOnly_onRoomStatusChange()`` method.
private var statusChangeSubscriptions: [Subscription<StatusChange>] = []

internal struct StatusChange {
internal var current: Status
internal var previous: Status
}

/// Allows tests to subscribe to changes to the manager’s internal status (which exposes more cases and additional metadata, compared to the ``RoomStatus`` exposed by ``onRoomStatusChange(bufferingPolicy:)``).
internal func testsOnly_onStatusChange() -> Subscription<StatusChange> {
let subscription: Subscription<StatusChange> = .init(bufferingPolicy: .unbounded)
statusChangeSubscriptions.append(subscription)
return subscription
}

internal func emitStatusChange(_ change: StatusChange) {
for subscription in statusChangeSubscriptions {
subscription.emit(change)
}
}
#endif

/// Updates ``status`` and emits a status change event.
private func changeStatus(to new: Status) {
logger.log(message: "Transitioning from \(status) to \(new)", level: .info)
Expand All @@ -335,6 +359,11 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
let statusChange = RoomStatusChange(current: status.toRoomStatus, previous: previous.toRoomStatus)
emitRoomStatusChange(statusChange)
}

#if DEBUG
let statusChange = StatusChange(current: status, previous: previous)
emitStatusChange(statusChange)
#endif
}

private func emitRoomStatusChange(_ change: RoomStatusChange) {
Expand Down

0 comments on commit f3d6d87

Please sign in to comment.