Skip to content

Commit

Permalink
Add SendStompError, sub func
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Jul 8, 2024
1 parent bda2cd9 commit c916fc4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
Binary file not shown.
56 changes: 47 additions & 9 deletions Sources/ApeunStompKit/ApeunStomp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,21 @@ public class ApeunStomp: NSObject {
}

// MARK: - Subscribe
public func subBody<D: Decodable>(destination: String, res: D.Type) -> AnyPublisher<D, StompError> {
public func subBody<D: Decodable>(
destination: String,
res: D.Type
) -> AnyPublisher<D, StompError> {
connection = true
subscribeToDestination(destination: destination, ackMode: .AutoMode)
return subject
.tryMap { e in
.tryMap { (e: ApeunStompEvent) -> D in
guard case .stompClient(let jsonBody, _, _, let d) = e,
d == destination,
let json = jsonBody.data(using: .utf8) else {
let json = jsonBody.data(using: .utf8) else {
if case .serverDidSendError(let description, let message) = e {
print("\(description), \(message)")
}
throw StompError.unknownError
throw StompError.unknown
}
do {
let res = try self.jsonDecoder.decode(D.self, from: json)
Expand All @@ -224,13 +227,13 @@ public class ApeunStomp: NSObject {
throw StompError.decodingFailure
}
}
.compactMap { $0 }
.mapError {
if let error = $0 as? StompError {
return error
.mapError { (error: Error) -> StompError in
guard let error = error as? StompError else {
return StompError.unknown
}
return StompError.unknownError
return error
}
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}

Expand All @@ -254,6 +257,41 @@ public class ApeunStomp: NSObject {
.eraseToAnyPublisher()
}

public func subSendError() -> AnyPublisher<SendStompError, Never> {
subject
.compactMap { e in
guard case .serverDidSendError(let description, let message) = e else {
return nil
}
return SendStompError(description: description, message: message)
}
.eraseToAnyPublisher()
}

public func subDisconnect() -> AnyPublisher<Void, Never> {
subject
.compactMap { e in
guard case .stompClientDidDisconnect = e else {
return nil
}
return
}
.eraseToAnyPublisher()
}

public func subSendReceipt() -> AnyPublisher<String, Never> {
subject
.compactMap { e in
guard case .serverDidSendReceipt(let receiptId) = e else {
return nil
}
return receiptId
}
.eraseToAnyPublisher()
}



public func subscribeToDestination(destination: String, ackMode: StompAckMode) {
let ack = switch ackMode {
case StompAckMode.ClientMode:
Expand Down
4 changes: 4 additions & 0 deletions Sources/ApeunStompKit/SendStompError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public struct SendStompError {
public let description: String
public let message: String?
}
4 changes: 2 additions & 2 deletions Sources/ApeunStompKit/StompError.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public enum StompError: Error {
public enum StompError: Error, Equatable {
case decodingFailure
case connectFailure
case unknownError
case unknown
}

0 comments on commit c916fc4

Please sign in to comment.