@@ -45,33 +45,37 @@ public final class AsyncThrowingReplaySubject<Element, Failure: Error>: AsyncSub
4545 /// Sends a value to all consumers
4646 /// - Parameter element: the value to send
4747 public func send( _ element: Element ) {
48- self . state. withCriticalRegion { state in
48+ let channels = self . state. withCriticalRegion { state -> [ AsyncThrowingBufferedChannel < Element , Error > ] in
4949 if state. buffer. count >= state. bufferSize && !state. buffer. isEmpty {
5050 state. buffer. removeFirst ( )
5151 }
5252 state. buffer. append ( element)
53- for channel in state. channels. values {
54- channel. send ( element)
55- }
53+ return Array ( state. channels. values)
54+ }
55+
56+ for channel in channels {
57+ channel. send ( element)
5658 }
5759 }
5860
5961 /// Finishes the subject with either a normal ending or an error.
6062 /// - Parameter termination: The termination to finish the subject
6163 public func send( _ termination: Termination < Failure > ) {
62- self . state. withCriticalRegion { state in
64+ let channels = self . state. withCriticalRegion { state -> [ AsyncThrowingBufferedChannel < Element , Error > ] in
6365 state. terminalState = termination
6466 let channels = Array ( state. channels. values)
6567 state. channels. removeAll ( )
6668 state. buffer. removeAll ( )
6769 state. bufferSize = 0
68- for channel in channels {
69- switch termination {
70- case . finished:
71- channel. finish ( )
72- case . failure( let error) :
73- channel. fail ( error)
74- }
70+ return channels
71+ }
72+
73+ for channel in channels {
74+ switch termination {
75+ case . finished:
76+ channel. finish ( )
77+ case . failure( let error) :
78+ channel. fail ( error)
7579 }
7680 }
7781 }
@@ -130,10 +134,10 @@ public final class AsyncThrowingReplaySubject<Element, Failure: Error>: AsyncSub
130134 }
131135
132136 public mutating func next( ) async throws -> Element ? {
133- try await withTaskCancellationHandler {
134- try await self . iterator. next ( )
135- } onCancel: { [ unregister] in
137+ try await withTaskCancellationHandler { [ unregister] in
136138 unregister ( )
139+ } operation: {
140+ try await self . iterator. next ( )
137141 }
138142 }
139143 }
0 commit comments