1+ /// A monitor that observes an asynchronous sequence and invokes the given block for each received element.
2+ ///
3+ /// The element must be `Sendable` so to use it to monitor notifications from `NotificationCenter` you'll need to map them to
4+ /// something sendable before calling `monitor` on the sequence. e.g.
5+ ///
6+ /// ```
7+ /// NotificationCenter.default
8+ /// .notifications(named: .NSCalendarDayChanged).map(\.name)
9+ /// .monitor { _ in whatever() }
10+ /// .store(in: &cancellables)
11+ /// ```
112public final class AsyncMonitor : Hashable , AsyncCancellable {
213 let task : Task < Void , Never >
314
15+ /// Creates an ``AsyncMonitor`` that observes the provided asynchronous sequence.
16+ ///
17+ /// - Parameters:
18+ /// - isolation: An optional actor isolation context to inherit.
19+ /// Defaults to `#isolation`, preserving the caller's actor isolation.
20+ /// - sequence: The asynchronous sequence of elements to observe.
21+ /// - block: A closure to execute for each element yielded by the sequence.
422 public init < Element: Sendable > (
523 isolation: isolated ( any Actor ) ? = #isolation,
624 sequence: any AsyncSequence < Element , Never > ,
@@ -21,17 +39,8 @@ public final class AsyncMonitor: Hashable, AsyncCancellable {
2139
2240 // MARK: AsyncCancellable conformance
2341
42+ /// Cancels the underlying task monitoring the asynchronous sequence.
2443 public func cancel( ) {
2544 task. cancel ( )
2645 }
27-
28- // MARK: Hashable conformance
29-
30- public static func == ( lhs: AsyncMonitor , rhs: AsyncMonitor ) -> Bool {
31- lhs. task == rhs. task
32- }
33-
34- public func hash( into hasher: inout Hasher ) {
35- hasher. combine ( task)
36- }
3746}
0 commit comments