1- @preconcurrency import Combine
21import Dependencies
32import Foundation
43
4+ #if canImport(Combine)
5+ @preconcurrency import Combine
6+ #endif
7+
58public protocol BroadcastableAsyncSequence : AsyncSequence { }
69
7- extension BroadcastableAsyncSequence {
8- @_spi ( Internals) public func publisher( ) -> AnyPublisher < Element , Never >
9- where Self: Sendable , Self. Element: Sendable {
10- let subject = CurrentValueSubject < Element ? , Never > ( . none)
10+ #if canImport(Combine)
11+ extension BroadcastableAsyncSequence {
12+ @_spi ( Internals) public func publisher( ) -> AnyPublisher < Element , Never >
13+ where Self: Sendable , Self. Element: Sendable {
14+ let subject = CurrentValueSubject < Element ? , Never > ( . none)
1115
12- let task = Task { @MainActor in
13- do {
14- for try await element in self {
15- subject. send ( element)
16+ let task = Task { @MainActor in
17+ do {
18+ for try await element in self {
19+ subject. send ( element)
20+ }
21+ } catch {
22+ subject. send ( completion: . finished)
1623 }
17- } catch {
18- subject. send ( completion: . finished)
1924 }
20- }
2125
22- return subject. handleEvents (
23- receiveCancel: {
24- task. cancel ( )
25- } )
26- . compactMap { $0 }
27- . eraseToAnyPublisher ( )
26+ return subject. handleEvents (
27+ receiveCancel: {
28+ task. cancel ( )
29+ } )
30+ . compactMap { $0 }
31+ . eraseToAnyPublisher ( )
32+ }
2833 }
29- }
34+ #endif
3035
3136extension BroadcastableAsyncSequence where Self: Sendable {
3237 public func forEach(
@@ -42,68 +47,69 @@ extension BroadcastableAsyncSequence where Self: Sendable {
4247 } . eraseToAnyCancellableTask ( )
4348 }
4449}
50+ #if canImport(Combine)
51+ extension BroadcastableAsyncSequence where Self: Sendable , Self. Element: Sendable {
52+ /// Assigns each element from an async sequence to a property on an object.
53+ /// - Parameters:
54+ /// - keyPath: A key path that indicates the property to assign.
55+ /// - object: The object that contains the property. The subscriber assigns the object’s
56+ /// property every time it receives a new value.
57+ /// - Returns: An AnyCancellable instance. Call cancel() on this instance when you no longer want
58+ /// the publisher to automatically assign the property. Deinitializing this instance will also
59+ /// cancel automatic assignment.
60+ @_disfavoredOverload
61+ public func assign< Root> ( to keyPath: ReferenceWritableKeyPath < Root , Element > , on object: Root )
62+ -> AnyCancellable
63+ {
64+ self . publisher ( ) . assign ( to: keyPath, on: object)
65+ }
4566
46- extension BroadcastableAsyncSequence where Self: Sendable , Self. Element: Sendable {
47- /// Assigns each element from an async sequence to a property on an object.
48- /// - Parameters:
49- /// - keyPath: A key path that indicates the property to assign.
50- /// - object: The object that contains the property. The subscriber assigns the object’s
51- /// property every time it receives a new value.
52- /// - Returns: An AnyCancellable instance. Call cancel() on this instance when you no longer want
53- /// the publisher to automatically assign the property. Deinitializing this instance will also
54- /// cancel automatic assignment.
55- @_disfavoredOverload
56- public func assign< Root> ( to keyPath: ReferenceWritableKeyPath < Root , Element > , on object: Root )
57- -> AnyCancellable
58- {
59- self . publisher ( ) . assign ( to: keyPath, on: object)
60- }
61-
62- /// Assigns each element from an async sequence to a property on an object.
63- ///
64- /// The assign(to:) operator manages the life cycle of the subscription, canceling the
65- /// subscription automatically when the Published instance deinitializes. Because of this, the
66- /// ``NotificationStream/assign(to:)-5dfsw`` operator doesn’t return an `AnyCancellable` that
67- /// you’re responsible for like ``NotificationStream/assign(to:on:)-rllq`` does.
68- ///
69- /// - Parameter published: A property marked with the @Published attribute, which receives and
70- /// republishes all elements received from the upstream publisher.
71- @available ( iOS 14 . 0 , macOS 11 . 0 , tvOS 14 . 0 , watchOS 7 . 0 , * )
72- @_disfavoredOverload
73- public func assign( to published: inout Published < Element > . Publisher ) {
74- self . publisher ( ) . assign ( to: & published)
75- }
67+ /// Assigns each element from an async sequence to a property on an object.
68+ ///
69+ /// The assign(to:) operator manages the life cycle of the subscription, canceling the
70+ /// subscription automatically when the Published instance deinitializes. Because of this, the
71+ /// ``NotificationStream/assign(to:)-5dfsw`` operator doesn’t return an `AnyCancellable` that
72+ /// you’re responsible for like ``NotificationStream/assign(to:on:)-rllq`` does.
73+ ///
74+ /// - Parameter published: A property marked with the @Published attribute, which receives and
75+ /// republishes all elements received from the upstream publisher.
76+ @available ( iOS 14 . 0 , macOS 11 . 0 , tvOS 14 . 0 , watchOS 7 . 0 , * )
77+ @_disfavoredOverload
78+ public func assign( to published: inout Published < Element > . Publisher ) {
79+ self . publisher ( ) . assign ( to: & published)
80+ }
7681
77- /// Assigns each element from an async sequence to a property on an object.
78- /// - Parameters:
79- /// - keyPath: A key path that indicates the property to assign.
80- /// - object: The object that contains the property. The subscriber assigns the object’s
81- /// property every time it receives a new value.
82- /// - Returns: An AnyCancellable instance. Call cancel() on this instance when you no longer want
83- /// the publisher to automatically assign the property. Deinitializing this instance will also
84- /// cancel automatic assignment.
85- @_disfavoredOverload
86- public func assign< Root> ( to keyPath: ReferenceWritableKeyPath < Root , Element ? > , on object: Root )
87- -> AnyCancellable
88- {
89- self . publisher ( ) . map ( Optional . some) . assign ( to: keyPath, on: object)
90- }
82+ /// Assigns each element from an async sequence to a property on an object.
83+ /// - Parameters:
84+ /// - keyPath: A key path that indicates the property to assign.
85+ /// - object: The object that contains the property. The subscriber assigns the object’s
86+ /// property every time it receives a new value.
87+ /// - Returns: An AnyCancellable instance. Call cancel() on this instance when you no longer want
88+ /// the publisher to automatically assign the property. Deinitializing this instance will also
89+ /// cancel automatic assignment.
90+ @_disfavoredOverload
91+ public func assign< Root> ( to keyPath: ReferenceWritableKeyPath < Root , Element ? > , on object: Root )
92+ -> AnyCancellable
93+ {
94+ self . publisher ( ) . map ( Optional . some) . assign ( to: keyPath, on: object)
95+ }
9196
92- /// Assigns each element from an async sequence to a property on an object.
93- ///
94- /// The assign(to:) operator manages the life cycle of the subscription, canceling the
95- /// subscription automatically when the Published instance deinitializes. Because of this, the
96- /// ``NotificationStream/assign(to:)-3sgyd`` operator doesn’t return an `AnyCancellable` that
97- /// you’re responsible for like ``NotificationStream/assign(to:on:)-fj51`` does.
98- ///
99- /// - Parameter published: A property marked with the @Published attribute, which receives and
100- /// republishes all elements received from the upstream publisher.
101- @available ( iOS 14 . 0 , macOS 11 . 0 , tvOS 14 . 0 , watchOS 7 . 0 , * )
102- @_disfavoredOverload
103- public func assign( to published: inout Published < Element ? > . Publisher ) {
104- self . publisher ( ) . map ( Optional . some) . assign ( to: & published)
97+ /// Assigns each element from an async sequence to a property on an object.
98+ ///
99+ /// The assign(to:) operator manages the life cycle of the subscription, canceling the
100+ /// subscription automatically when the Published instance deinitializes. Because of this, the
101+ /// ``NotificationStream/assign(to:)-3sgyd`` operator doesn’t return an `AnyCancellable` that
102+ /// you’re responsible for like ``NotificationStream/assign(to:on:)-fj51`` does.
103+ ///
104+ /// - Parameter published: A property marked with the @Published attribute, which receives and
105+ /// republishes all elements received from the upstream publisher.
106+ @available ( iOS 14 . 0 , macOS 11 . 0 , tvOS 14 . 0 , watchOS 7 . 0 , * )
107+ @_disfavoredOverload
108+ public func assign( to published: inout Published < Element ? > . Publisher ) {
109+ self . publisher ( ) . map ( Optional . some) . assign ( to: & published)
110+ }
105111 }
106- }
112+ #endif
107113
108114extension AsyncMapSequence : BroadcastableAsyncSequence
109115where Base: BroadcastableAsyncSequence { }
0 commit comments