-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from surfstudio/SPT-1998-combine-support
SPT-1998 Поддержка combine
- Loading branch information
Showing
77 changed files
with
2,104 additions
and
275 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// AsyncNode.swift | ||
// NodeKit | ||
// | ||
// Created by Andrei Frolov on 29.03.24. | ||
// Copyright © 2024 Surf. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
|
||
/// Протокол, наследованный от Node, добавляющий подход преобразования входных данных в результат с помощью SwiftConcurrency. | ||
/// Применим для узлов, которые возвращают один результат. | ||
public protocol AsyncNode<Input, Output>: Node { | ||
|
||
/// Ассинхронный метод, который содержит логику для обработки данных | ||
/// | ||
/// - Parameter data: Входные данные | ||
/// - Returns: Результат обработки данных. | ||
@discardableResult | ||
func process(_ data: Input, logContext: LoggingContextProtocol) async -> NodeResult<Output> | ||
|
||
/// Метод возвращающий объект для обработки результатов с помощью Combine. | ||
/// | ||
/// - Returns: Узел, поддерживающий обработку результатов с помощью Combine. | ||
func combineNode() -> any CombineNode<Input, Output> | ||
} | ||
|
||
public extension AsyncNode { | ||
|
||
/// Метод process с созданием нового лог контекста. | ||
@discardableResult | ||
func process(_ data: Input) async -> NodeResult<Output> { | ||
return await process(data, logContext: LoggingContext()) | ||
} | ||
|
||
/// Стандартная реализация конвертации узла в ``CombineNode``. | ||
/// | ||
/// - Returns: Узел, поддерживающий обработку результатов с помощью Combine. | ||
func combineNode() -> any CombineNode<Input, Output> { | ||
return AsyncCombineNode(node: self) | ||
} | ||
} | ||
|
||
/// Содержит синтаксический сахар для работы с узлами, у которых входящий тип = `Void` | ||
public extension AsyncNode where Input == Void { | ||
|
||
/// Вызывает `process(_:)` | ||
@discardableResult | ||
func process() async -> NodeResult<Output> { | ||
return await process(Void()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// AsyncStreamNode.swift | ||
// NodeKit | ||
// | ||
// Created by Andrei Frolov on 29.03.24. | ||
// Copyright © 2024 Surf. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
|
||
/// Протокол, наследованный от Node, добавляющий подход преобразования входных данных в поток результатов с помощью SwiftConcurrency | ||
/// Применим для узлов, которые могут вернуть несколько результатов | ||
public protocol AsyncStreamNode<Input, Output>: Node { | ||
|
||
/// Ассинхронный метод, который содержит логику для обработки данных | ||
/// | ||
/// - Parameter data: Входные данные | ||
/// - Returns: Поток результатов обработки данных. | ||
@discardableResult | ||
func process(_ data: Input, logContext: LoggingContextProtocol) -> AsyncStream<NodeResult<Output>> | ||
|
||
/// Метод возвращающий объект для обработки результатов с помощью Combine. | ||
/// | ||
/// - Returns: Узел, поддерживающий обработку результатов с помощью Combine. | ||
func combineStreamNode() -> any CombineStreamNode<Input, Output> | ||
} | ||
|
||
public extension AsyncStreamNode { | ||
|
||
/// Метод process с созданием нового лог контекста. | ||
@discardableResult | ||
func process(_ data: Input) -> AsyncStream<NodeResult<Output>> { | ||
return process(data, logContext: LoggingContext()) | ||
} | ||
/// Стандартная реализация конвертации узла в ``CombineStreamNode``. | ||
/// | ||
/// - Returns: Узел, поддерживающий обработку результатов с помощью Combine. | ||
func combineStreamNode() -> any CombineStreamNode<Input, Output> { | ||
return AsyncStreamCombineNode(node: self) | ||
} | ||
} | ||
|
||
/// Содержит синтаксический сахар для работы с узлами, у которых входящий тип = `Void` | ||
public extension AsyncStreamNode where Input == Void { | ||
|
||
/// Вызывает `process(_:)` | ||
@discardableResult | ||
func process() -> AsyncStream<NodeResult<Output>> { | ||
return process(Void()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// AsyncCombineNode.swift | ||
// NodeKit | ||
// | ||
// Created by Andrei Frolov on 03.04.24. | ||
// Copyright © 2024 Surf. All rights reserved. | ||
// | ||
|
||
import Combine | ||
|
||
/// Реализация ``CombineNode`` для ``AsyncNode``. | ||
public struct AsyncCombineNode<Input, Output>: CombineNode { | ||
|
||
// MARK: - Private Properties | ||
|
||
private let node: any AsyncNode<Input, Output> | ||
|
||
// MARK: - Initialization | ||
|
||
init(node: some AsyncNode<Input, Output>) { | ||
self.node = node | ||
} | ||
|
||
// MARK: - CombineNode | ||
|
||
/// Метод запускающий процесс обработки данных | ||
/// и возвращающий publisher для подписки на результат. | ||
/// | ||
/// - Parameters: | ||
/// - data: Входные данные ноды. | ||
/// - queue: Очередь на которой будут выдаваться результаты. | ||
/// - logContext: Контекст логов. | ||
/// - Returns: Publisher для подписки на результат. | ||
public func nodeResultPublisher(for data: Input, on scheduler: some Scheduler, logContext: LoggingContextProtocol) -> AnyPublisher<NodeResult<Output>, Never> { | ||
return Future<NodeResult<Output>, Never> { promise in | ||
Task { | ||
let result = await node.process(data, logContext: logContext) | ||
promise(.success(result)) | ||
} | ||
} | ||
.receive(on: scheduler) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
public func processLegacy(_ data: Input) -> Observer<Output> { | ||
return node.processLegacy(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// AsyncStreamCombineNode.swift | ||
// NodeKit | ||
// | ||
// Created by Andrei Frolov on 03.04.24. | ||
// Copyright © 2024 Surf. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
/// Реализация ``CombineStreamNode`` для ``AsyncStreamNode``. | ||
public struct AsyncStreamCombineNode<Input, Output>: CombineStreamNode { | ||
|
||
// MARK: - Private Properties | ||
|
||
private let subject = PassthroughSubject<NodeResult<Output>, Never>() | ||
private let node: any AsyncStreamNode<Input, Output> | ||
|
||
// MARK: - Initialization | ||
|
||
init(node: some AsyncStreamNode<Input, Output>) { | ||
self.node = node | ||
} | ||
|
||
// MARK: - CombineNode | ||
|
||
/// Publisher результата обработки данных. | ||
/// - Parameter scheduler: Scheduler для выдачи результов. | ||
public func nodeResultPublisher(on scheduler: some Scheduler) -> AnyPublisher<NodeResult<Output>, Never> { | ||
return subject | ||
.receive(on: scheduler) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
/// Метод обработки данных протокола ``Node``. Будет удален в ближайшее время. | ||
public func processLegacy(_ data: Input) -> Observer<Output> { | ||
return node.processLegacy(data) | ||
} | ||
|
||
/// Метод запускающий процесс обработки данных. | ||
/// | ||
/// - Parameters: | ||
/// - data: Входные данные ноды. | ||
/// - logContext: Контекст логов. | ||
public func process(_ data: Input, logContext: LoggingContextProtocol) { | ||
Task { | ||
for await result in node.process(data, logContext: logContext) { | ||
subject.send(result) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.