Skip to content

Commit

Permalink
feat/#95 :: WebSocketClientDelegate Deprecated
Browse files Browse the repository at this point in the history
WebSocketClientDelegate를 여러 곳에서 쓰게 되면서, 순환참조 문제나 배열을 순회하며 델리게이트 메소드를 호출해주는 구조보단 Pub/Sub 패턴을 활용해보고자 했습니다.
 Co-Authored-By: Kiyoung <[email protected]>
  • Loading branch information
youn9k committed Nov 25, 2024
1 parent b4db373 commit b7b4ef2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import Foundation
import Combine

public protocol WebSocketClient {
var delegates: [WebSocketClientDelegate] { get set }
var webSocketDidConnectPublisher: AnyPublisher<Void, Never> { get }
var webSocketDidDisconnectPublisher: AnyPublisher<Void, Never> { get }
var webSocketdidReceiveDataPublisher: AnyPublisher<Data, Never> { get }

func connect()
func send(data: Data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@available(*, deprecated, message: "Publisher로 교체될 예정입니다.")
public protocol WebSocketClientDelegate: AnyObject {
func webSocketDidConnect(_ webSocket: WebSocketClient)
func webSocketDidDisconnect(_ webSocket: WebSocketClient)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import Foundation
import PhotoGetherDomainInterface
import Combine

public final class WebSocketClientImpl: NSObject, WebSocketClient {
@available(*, deprecated, message: "Publisher로 교체될 예정입니다.")
public var delegates: [WebSocketClientDelegate] = []

private let _webSocketDidConnectPublisher = PassthroughSubject<Void, Never>()
private let _webSocketDidDisconnectPublisher = PassthroughSubject<Void, Never>()
private let _webSocketdidReceiveDataPublisher = PassthroughSubject<Data, Never>()

public var webSocketDidConnectPublisher: AnyPublisher<Void, Never> {
_webSocketDidConnectPublisher.eraseToAnyPublisher()
}
public var webSocketDidDisconnectPublisher: AnyPublisher<Void, Never> {
_webSocketDidDisconnectPublisher.eraseToAnyPublisher()
}
public var webSocketdidReceiveDataPublisher: AnyPublisher<Data, Never> {
_webSocketdidReceiveDataPublisher.eraseToAnyPublisher()
}

private let url: URL
private var socket: URLSessionWebSocketTask?

Expand All @@ -28,6 +45,7 @@ public final class WebSocketClientImpl: NSObject, WebSocketClient {

switch message {
case .success(.data(let data)):
self._webSocketdidReceiveDataPublisher.send(data)
self.delegates.forEach{
$0.webSocket(self, didReceiveData: data)
}
Expand All @@ -46,6 +64,7 @@ public final class WebSocketClientImpl: NSObject, WebSocketClient {
private func disconnect() {
self.socket?.cancel()
self.socket = nil
self._webSocketDidDisconnectPublisher.send(())
self.delegates.forEach {
$0.webSocketDidDisconnect(self)
}
Expand All @@ -58,6 +77,7 @@ extension WebSocketClientImpl: URLSessionWebSocketDelegate, URLSessionDelegate {
webSocketTask: URLSessionWebSocketTask,
didOpenWithProtocol protocol: String?
) {
self._webSocketDidConnectPublisher.send(())
self.delegates.forEach {
$0.webSocketDidConnect(self)
}
Expand Down

0 comments on commit b7b4ef2

Please sign in to comment.