From 358df51efd5461e7871823d1b5f2604f70d2c6d4 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Mon, 25 Jul 2022 16:18:13 +0100 Subject: [PATCH] Add ServiceProcessor --- .../ElixxirDAppsSDK/ServiceProcessor.swift | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Sources/ElixxirDAppsSDK/ServiceProcessor.swift diff --git a/Sources/ElixxirDAppsSDK/ServiceProcessor.swift b/Sources/ElixxirDAppsSDK/ServiceProcessor.swift new file mode 100644 index 00000000..f6798fa2 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/ServiceProcessor.swift @@ -0,0 +1,54 @@ +import Bindings +import XCTestDynamicOverlay + +public struct ServiceProcessor { + public typealias Process = ( + _ message: Data, + _ receptionId: Data, + _ ephemeralId: Int64, + _ roundId: Int64 + ) -> Void + + public init(serviceTag: String, process: @escaping Process) { + self.serviceTag = serviceTag + self.process = process + } + + public var serviceTag: String + public var process: Process +} + +extension ServiceProcessor { + public static let unimplemented = ServiceProcessor( + serviceTag: "unimplemented", + process: XCTUnimplemented("\(Self.self).process") + ) +} + +extension ServiceProcessor { + func makeBindingsProcessor() -> BindingsProcessorProtocol { + class Processor: NSObject, BindingsProcessorProtocol { + init(_ serviceProcessor: ServiceProcessor) { + self.serviceProcessor = serviceProcessor + } + + let serviceProcessor: ServiceProcessor + + func process(_ message: Data?, receptionId: Data?, ephemeralId: Int64, roundId: Int64) { + guard let message = message else { + fatalError("BindingsProcessor.process received `nil` message") + } + guard let receptionId = receptionId else { + fatalError("BindingsProcessor.process received `nil` receptionId") + } + serviceProcessor.process(message, receptionId, ephemeralId, roundId) + } + + func string() -> String { + serviceProcessor.serviceTag + } + } + + return Processor(self) + } +} -- GitLab