77//
88
99import Foundation
10-
11- struct SseClientConstants {
12- static let pushNotificationChannelsParam = " channels "
13- static let pushNotificationTokenParam = " accessToken "
14- static let pushNotificationVersionParam = " v "
15- static let pushNotificationVersionValue = " 1.1 "
10+ import Concurrency
11+ import Http
12+ import Logging
13+
14+ public struct SseClientConstants {
15+ public static let pushNotificationChannelsParam = " channels "
16+ public static let pushNotificationTokenParam = " accessToken "
17+ public static let pushNotificationVersionParam = " v "
18+ public static let pushNotificationVersionValue = " 1.1 "
1619}
1720
18- protocol SseClient : AnyObject {
21+ public protocol SseClient : AnyObject {
1922 typealias CompletionHandler = @Sendable ( Bool ) -> Void
2023 func connect( token: String , channels: [ String ] , completion: @escaping CompletionHandler )
2124 func disconnect( )
2225 var isConnectionOpened : Bool { get }
26+
2327}
2428
25- class DefaultSseClient : SseClient , @unchecked Sendable {
29+ public class DefaultSseClient : SseClient , @unchecked Sendable {
2630
2731 ///
2832 /// NOTE:
@@ -38,18 +42,18 @@ class DefaultSseClient: SseClient, @unchecked Sendable {
3842 private var isDisconnectCalled : Atomic < Bool > = Atomic ( false )
3943 private var isConnected : Atomic < Bool > = Atomic ( false )
4044 private var isFirstMessage : Atomic < Bool > = Atomic ( false )
41- var isConnectionOpened : Bool {
45+ public var isConnectionOpened : Bool {
4246 return isConnected. value
4347 }
4448
45- init ( endpoint: Endpoint , httpClient: HttpClient , sseHandler: SseHandler ) {
49+ public init ( endpoint: Endpoint , httpClient: HttpClient , sseHandler: SseHandler ) {
4650 self . endpoint = endpoint
4751 self . httpClient = httpClient
4852 self . sseHandler = sseHandler
4953 self . queue = DispatchQueue ( label: " split-sse-client " )
5054 }
5155
52- func connect( token: String , channels: [ String ] , completion: @escaping CompletionHandler ) {
56+ public func connect( token: String , channels: [ String ] , completion: @escaping CompletionHandler ) {
5357 queue. async ( flags: . barrier) { [ weak self] in
5458 guard let self = self else { return }
5559 let parameters : [ String : Any ] = [
@@ -105,7 +109,7 @@ class DefaultSseClient: SseClient, @unchecked Sendable {
105109 }
106110 }
107111
108- func disconnect( ) {
112+ public func disconnect( ) {
109113 Logger . d ( " Disconnecting SSE client " )
110114 isDisconnectCalled. set ( true )
111115 isConnected. set ( false )
@@ -132,7 +136,7 @@ class DefaultSseClient: SseClient, @unchecked Sendable {
132136
133137 guard let self = self else { return }
134138
135- let values = self . streamParser. parse ( streamChunk: data. stringRepresentation )
139+ let values = self . streamParser. parse ( streamChunk: String ( data: data , encoding : . utf8 ) ?? " " )
136140
137141 if self . isFirstMessage. value {
138142 if self . isConnectionConfirmed ( message: values) {
0 commit comments