From c3e306df02ef051a51ef06b9757c5a854b5f711e Mon Sep 17 00:00:00 2001 From: Andrey Frolov Date: Mon, 1 Apr 2024 12:46:20 +0200 Subject: [PATCH] SPT-1998 add some methods to node protocols --- NodeKit/Core/Node/Async/AsyncNode.swift | 14 ++++++++++++++ NodeKit/Core/Node/Async/AsyncStreamNode.swift | 14 ++++++++++++++ NodeKit/Core/Node/Combine/CombineNode.swift | 19 ++++++++++++++----- .../UnitTests/Node/AsyncNodeTests.swift | 4 ++-- .../UnitTests/Node/AsyncStreamNodeTests.swift | 4 ++-- .../Combine/CombineCompatibleNodeTests.swift | 4 ++-- 6 files changed, 48 insertions(+), 11 deletions(-) diff --git a/NodeKit/Core/Node/Async/AsyncNode.swift b/NodeKit/Core/Node/Async/AsyncNode.swift index 6796d43b..50c6469b 100644 --- a/NodeKit/Core/Node/Async/AsyncNode.swift +++ b/NodeKit/Core/Node/Async/AsyncNode.swift @@ -22,8 +22,22 @@ public protocol AsyncNode: CombineConvertibleNode { public extension AsyncNode { + /// Метод process с созданием нового лог контекста. + func process(_ data: Input) async -> NodeResult { + return await process(data, logContext: LoggingContext()) + } + /// Базовая реализация конвертации узла в ``CombineNode``. func combineNode() -> any CombineNode { return CombineCompatibleNode(adapter: AsyncNodeAdapter(node: self)) } } + +/// Содержит иснтаксический сахар для работы с узлами, у которых входящий тип = `Void` +public extension AsyncNode where Input == Void { + + /// Вызывает `process(_:)` + func process() async -> NodeResult { + return await process(Void()) + } +} diff --git a/NodeKit/Core/Node/Async/AsyncStreamNode.swift b/NodeKit/Core/Node/Async/AsyncStreamNode.swift index 8aba0bab..b781d3f8 100644 --- a/NodeKit/Core/Node/Async/AsyncStreamNode.swift +++ b/NodeKit/Core/Node/Async/AsyncStreamNode.swift @@ -21,8 +21,22 @@ public protocol AsyncStreamNode: CombineConvertibleNode { public extension AsyncStreamNode { + /// Метод process с созданием нового лог контекста. + func process(_ data: Input) -> AsyncStream> { + return process(data, logContext: LoggingContext()) + } + /// Базовая реализация конвертации узла в ``CombineNode``. func combineNode() -> any CombineNode { return CombineCompatibleNode(adapter: AsyncStreamNodeAdapter(node: self)) } } + +/// Содержит иснтаксический сахар для работы с узлами, у которых входящий тип = `Void` +public extension AsyncStreamNode where Input == Void { + + /// Вызывает `process(_:)` + func process() -> AsyncStream> { + return process(Void()) + } +} diff --git a/NodeKit/Core/Node/Combine/CombineNode.swift b/NodeKit/Core/Node/Combine/CombineNode.swift index 8136d813..556e5714 100644 --- a/NodeKit/Core/Node/Combine/CombineNode.swift +++ b/NodeKit/Core/Node/Combine/CombineNode.swift @@ -21,7 +21,7 @@ public protocol CombineNode: AnyObject { /// - logContext: Контекст логов. /// - Returns: Self ноды. @discardableResult - func process(data: Input, logContext: LoggingContextProtocol) -> Self + func process(_ data: Input, logContext: LoggingContextProtocol) -> Self /// Метод получения Publisher, для подписки на результат обработки данных в главном потоке. /// @@ -35,15 +35,24 @@ public protocol CombineNode: AnyObject { func eraseToAnyPublisher(queue: DispatchQueue) -> AnyPublisher, Never> } -extension CombineNode { +public extension CombineNode { /// Метод запускающий процесс обработки данных и создающий новый контекст логов. /// /// - Parameter data: Входные данные ноды. /// - Returns: Self ноды. @discardableResult - func process(data: Input) -> Self { - return process(data: data, logContext: LoggingContext()) + func process(_ data: Input) -> Self { + return process(data, logContext: LoggingContext()) + } +} + +/// Содержит иснтаксический сахар для работы с узлами, у которых входящий тип = `Void` +public extension CombineNode where Input == Void { + + /// Вызывает `process(_:)` + func process() -> Self { + return process(Void()) } } @@ -70,7 +79,7 @@ public class CombineCompatibleNode: CombineNode, NodeAdapterOutpu /// - logContext: Контекст логов. /// - Returns: Self ноды. @discardableResult - public func process(data: Input, logContext: LoggingContextProtocol) -> Self { + public func process(_ data: Input, logContext: LoggingContextProtocol) -> Self { Task { await adapter.process(data: data, logContext: logContext, output: self) } diff --git a/NodeKitTests/UnitTests/Node/AsyncNodeTests.swift b/NodeKitTests/UnitTests/Node/AsyncNodeTests.swift index 26170eb1..ac9f3ef2 100644 --- a/NodeKitTests/UnitTests/Node/AsyncNodeTests.swift +++ b/NodeKitTests/UnitTests/Node/AsyncNodeTests.swift @@ -50,7 +50,7 @@ final class AsyncNodeTests: XCTestCase { // when let sut = nodeMock.combineNode() - sut.process(data: expectedInput, logContext: logContextMock) + sut.process(expectedInput, logContext: logContextMock) .eraseToAnyPublisher() .sink(receiveValue: { value in result = value @@ -100,7 +100,7 @@ final class AsyncNodeTests: XCTestCase { }) .store(in: &cancellable) - sut.process(data: expectedInput, logContext: logContextMock) + sut.process(expectedInput, logContext: logContextMock) await fulfillment(of: [expectation1, expectation2], timeout: 0.1) diff --git a/NodeKitTests/UnitTests/Node/AsyncStreamNodeTests.swift b/NodeKitTests/UnitTests/Node/AsyncStreamNodeTests.swift index 95a6949d..30da8bf3 100644 --- a/NodeKitTests/UnitTests/Node/AsyncStreamNodeTests.swift +++ b/NodeKitTests/UnitTests/Node/AsyncStreamNodeTests.swift @@ -62,7 +62,7 @@ final class AsyncStreamNodeTests: XCTestCase { // when let sut = nodeMock.combineNode() - sut.process(data: expectedInput, logContext: logContextMock) + sut.process(expectedInput, logContext: logContextMock) .eraseToAnyPublisher() .sink(receiveValue: { value in results.append(value) @@ -133,7 +133,7 @@ final class AsyncStreamNodeTests: XCTestCase { }) .store(in: &cancellable) - sut.process(data: expectedInput, logContext: logContextMock) + sut.process(expectedInput, logContext: logContextMock) await fulfillment(of: [expectation1, expectation2], timeout: 0.1) diff --git a/NodeKitTests/UnitTests/Node/Combine/CombineCompatibleNodeTests.swift b/NodeKitTests/UnitTests/Node/Combine/CombineCompatibleNodeTests.swift index f4746f7e..62ebd3d5 100644 --- a/NodeKitTests/UnitTests/Node/Combine/CombineCompatibleNodeTests.swift +++ b/NodeKitTests/UnitTests/Node/Combine/CombineCompatibleNodeTests.swift @@ -55,7 +55,7 @@ final class CombineCompatibleNodeTests: XCTestCase { // when Task { - sut.process(data: expectedInput) + sut.process(expectedInput) } await fulfillment(of: [expectation], timeout: 0.1) @@ -80,7 +80,7 @@ final class CombineCompatibleNodeTests: XCTestCase { // when Task { - sut.process(data: expectedInput, logContext: logContext) + sut.process(expectedInput, logContext: logContext) } await fulfillment(of: [expectation], timeout: 0.1)