diff --git a/Sources/XXMessengerClient/Messenger/Functions/MessengerRegisterReceiveFileCallback.swift b/Sources/XXMessengerClient/Messenger/Functions/MessengerRegisterReceiveFileCallback.swift new file mode 100644 index 0000000000000000000000000000000000000000..42d56389e74e523a87208ebe11185fe6107d946d --- /dev/null +++ b/Sources/XXMessengerClient/Messenger/Functions/MessengerRegisterReceiveFileCallback.swift @@ -0,0 +1,24 @@ +import XCTestDynamicOverlay +import XXClient + +public struct MessengerRegisterReceiveFileCallback { + public var run: (ReceiveFileCallback) -> Cancellable + + public func callAsFunction(_ callback: ReceiveFileCallback) -> Cancellable { + run(callback) + } +} + +extension MessengerRegisterReceiveFileCallback { + public static func live(_ env: MessengerEnvironment) -> MessengerRegisterReceiveFileCallback { + MessengerRegisterReceiveFileCallback { callback in + env.receiveFileCallbacksRegistry.register(callback) + } + } +} + +extension MessengerRegisterReceiveFileCallback { + public static let unimplemented = MessengerRegisterReceiveFileCallback( + run: XCTUnimplemented("\(Self.self)", placeholder: Cancellable {}) + ) +} diff --git a/Sources/XXMessengerClient/Messenger/Messenger.swift b/Sources/XXMessengerClient/Messenger/Messenger.swift index 793daab0b708c48ee0fa1d2f9315a533c245475a..d565ebb2b6515f00bacd776be88f83f7bcd7401c 100644 --- a/Sources/XXMessengerClient/Messenger/Messenger.swift +++ b/Sources/XXMessengerClient/Messenger/Messenger.swift @@ -40,6 +40,7 @@ public struct Messenger { public var stopBackup: MessengerStopBackup public var setLogLevel: MessengerSetLogLevel public var startLogging: MessengerStartLogging + public var registerReceiveFileCallback: MessengerRegisterReceiveFileCallback public var startFileTransfer: MessengerStartFileTransfer public var sendFile: MessengerSendFile public var receiveFile: MessengerReceiveFile @@ -87,6 +88,7 @@ extension Messenger { stopBackup: .live(env), setLogLevel: .live(env), startLogging: .live(env), + registerReceiveFileCallback: .live(env), startFileTransfer: .live(env), sendFile: .live(env), receiveFile: .live(env) @@ -135,6 +137,7 @@ extension Messenger { stopBackup: .unimplemented, setLogLevel: .unimplemented, startLogging: .unimplemented, + registerReceiveFileCallback: .unimplemented, startFileTransfer: .unimplemented, sendFile: .unimplemented, receiveFile: .unimplemented diff --git a/Tests/XXMessengerClientTests/Messenger/Functions/MessengerRegisterReceiveFileCallbackTests.swift b/Tests/XXMessengerClientTests/Messenger/Functions/MessengerRegisterReceiveFileCallbackTests.swift new file mode 100644 index 0000000000000000000000000000000000000000..c30bb10afee600165089682c948c2539395992e4 --- /dev/null +++ b/Tests/XXMessengerClientTests/Messenger/Functions/MessengerRegisterReceiveFileCallbackTests.swift @@ -0,0 +1,34 @@ +import CustomDump +import XCTest +import XXClient +@testable import XXMessengerClient + +final class MessengerRegisterReceiveFileCallbackTests: XCTestCase { + func testRegisterCallback() { + var registeredCallbacks: [ReceiveFileCallback] = [] + var didHandleResult: [ReceiveFileCallback.Result] = [] + var didCancelRegisteredCallback = 0 + + var env: MessengerEnvironment = .unimplemented + env.receiveFileCallbacksRegistry.register = { callback in + registeredCallbacks.append(callback) + return Cancellable { didCancelRegisteredCallback += 1 } + } + let registerCallback: MessengerRegisterReceiveFileCallback = .live(env) + let cancellable = registerCallback(ReceiveFileCallback { result in + didHandleResult.append(result) + }) + + XCTAssertEqual(registeredCallbacks.count, 1) + + registeredCallbacks.forEach { callback in + callback.handle(.success(.stub(1))) + } + + XCTAssertNoDifference(didHandleResult, [.success(.stub(1))]) + + cancellable.cancel() + + XCTAssertEqual(didCancelRegisteredCallback, 1) + } +}