File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -73,27 +73,15 @@ public extension WebSocket {
7373
7474 /// The WebSocket's received messages as an asynchronous stream.
7575 var messages : AsyncStream < WebSocketMessage > {
76- let cancellable = Locked < AnyCancellable ? > ( nil )
77-
78- return AsyncStream { cont in
79- func finish( ) {
80- cancellable. access { cancellable in
81- if cancellable != nil {
82- cont. finish ( )
83- cancellable = nil
84- }
85- }
86- }
87-
88- let _cancellable = self . messagesPublisher ( )
89- . handleEvents ( receiveCancel: { finish ( ) } )
90- . sink (
91- receiveCompletion: { _ in finish ( ) } ,
92- receiveValue: { cont. yield ( $0) }
93- )
94-
95- cancellable. access { $0 = _cancellable }
96- }
76+ let ( stream, continuation) = AsyncStream< WebSocketMessage> . makeStream( )
77+ let cancellable = messagesPublisher ( )
78+ . handleEvents ( receiveCancel: { continuation. finish ( ) } )
79+ . sink (
80+ receiveCompletion: { _ in continuation. finish ( ) } ,
81+ receiveValue: { continuation. yield ( $0) }
82+ )
83+ continuation. onTermination = { @Sendable _ in cancellable. cancel ( ) }
84+ return stream
9785 }
9886}
9987
You can’t perform that action at this time.
0 commit comments