Skip to content

Commit

Permalink
SPT-1998 add some methods to node protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
mrandrewsmith committed Apr 1, 2024
1 parent 2a7e75e commit c3e306d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
14 changes: 14 additions & 0 deletions NodeKit/Core/Node/Async/AsyncNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,22 @@ public protocol AsyncNode<Input, Output>: CombineConvertibleNode {

public extension AsyncNode {

/// Метод process с созданием нового лог контекста.
func process(_ data: Input) async -> NodeResult<Output> {
return await process(data, logContext: LoggingContext())
}

/// Базовая реализация конвертации узла в ``CombineNode``.
func combineNode() -> any CombineNode<Input, Output> {
return CombineCompatibleNode(adapter: AsyncNodeAdapter(node: self))
}
}

/// Содержит иснтаксический сахар для работы с узлами, у которых входящий тип = `Void`
public extension AsyncNode where Input == Void {

/// Вызывает `process(_:)`
func process() async -> NodeResult<Output> {
return await process(Void())
}
}
14 changes: 14 additions & 0 deletions NodeKit/Core/Node/Async/AsyncStreamNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,22 @@ public protocol AsyncStreamNode<Input, Output>: CombineConvertibleNode {

public extension AsyncStreamNode {

/// Метод process с созданием нового лог контекста.
func process(_ data: Input) -> AsyncStream<NodeResult<Output>> {
return process(data, logContext: LoggingContext())
}

/// Базовая реализация конвертации узла в ``CombineNode``.
func combineNode() -> any CombineNode<Input, Output> {
return CombineCompatibleNode(adapter: AsyncStreamNodeAdapter(node: self))
}
}

/// Содержит иснтаксический сахар для работы с узлами, у которых входящий тип = `Void`
public extension AsyncStreamNode where Input == Void {

/// Вызывает `process(_:)`
func process() -> AsyncStream<NodeResult<Output>> {
return process(Void())
}
}
19 changes: 14 additions & 5 deletions NodeKit/Core/Node/Combine/CombineNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public protocol CombineNode<Input, Output>: AnyObject {
/// - logContext: Контекст логов.
/// - Returns: Self ноды.
@discardableResult
func process(data: Input, logContext: LoggingContextProtocol) -> Self
func process(_ data: Input, logContext: LoggingContextProtocol) -> Self

/// Метод получения Publisher, для подписки на результат обработки данных в главном потоке.
///
Expand All @@ -35,15 +35,24 @@ public protocol CombineNode<Input, Output>: AnyObject {
func eraseToAnyPublisher(queue: DispatchQueue) -> AnyPublisher<NodeResult<Output>, 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())
}
}

Expand All @@ -70,7 +79,7 @@ public class CombineCompatibleNode<Input, Output>: 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)
}
Expand Down
4 changes: 2 additions & 2 deletions NodeKitTests/UnitTests/Node/AsyncNodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions NodeKitTests/UnitTests/Node/AsyncStreamNodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class CombineCompatibleNodeTests: XCTestCase {
// when

Task {
sut.process(data: expectedInput)
sut.process(expectedInput)
}

await fulfillment(of: [expectation], timeout: 0.1)
Expand All @@ -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)
Expand Down

0 comments on commit c3e306d

Please sign in to comment.