Files

44 lines
1.5 KiB
Swift

import Foundation
/// The bridge that carries ``RecordingCommand``s from outside the feature's UI into the recording flow.
///
/// The commander exists because the senders of the commands — the App Intents behind a Live Activity's buttons — are instantiated
/// by the system, out of reach of the feature's dependency injection: they send through the process-wide ``shared`` instance, and the
/// feature's model listens to ``commands``, translating every received command into the button press it mirrors. Unit tests create
/// commanders of their own and inject them into the model, leaving the shared instance untouched.
@MainActor
public final class RecordingCommander {
// MARK: Constants
/// The process-wide commander the recording flow listens to by default.
public static let shared = RecordingCommander()
// MARK: Properties
/// The stream of commands sent through the commander.
public let commands: AsyncStream<RecordingCommand>
/// The continuation that feeds ``commands``.
private let continuation: AsyncStream<RecordingCommand>.Continuation
// MARK: Initializers
/// Creates a commander with an empty stream of commands.
public init() {
(commands, continuation) = AsyncStream.makeStream(of: RecordingCommand.self)
}
// MARK: Methods
/// Sends a command to the recording flow listening to the commander.
///
/// - Parameter command: The command to send.
public func send(
_ command: RecordingCommand
) {
continuation.yield(command)
}
}