1 |
//
|
|
2 |
// RouteComposer
|
|
3 |
// ConvertingStep.swift
|
|
4 |
// https://github.com/ekazaev/route-composer
|
|
5 |
//
|
|
6 |
// Created by Eugene Kazaev in 2018-2022.
|
|
7 |
// Distributed under the MIT license.
|
|
8 |
//
|
|
9 |
// Become a sponsor:
|
|
10 |
// https://github.com/sponsors/ekazaev
|
|
11 |
//
|
|
12 |
|
|
13 |
import Foundation
|
|
14 |
|
|
15 |
struct ConvertingStep<CT: ContextTransformer>: RoutingStep,
|
|
16 |
ChainableStep,
|
|
17 |
PerformableStep {
|
|
18 |
|
|
19 |
private let contextTransformer: CT
|
|
20 |
private var previousStep: RoutingStep?
|
|
21 |
|
|
22 |
init(contextTransformer: CT, previousStep: RoutingStep?) {
|
7x |
23 |
self.contextTransformer = contextTransformer
|
7x |
24 |
self.previousStep = previousStep
|
7x |
25 |
}
|
7x |
26 |
|
|
27 |
func getPreviousStep(with context: AnyContext) -> RoutingStep? {
|
14x |
28 |
return previousStep
|
14x |
29 |
}
|
14x |
30 |
|
|
31 |
func perform(with context: AnyContext) throws -> PerformableStepResult {
|
11x |
32 |
let typedContext: CT.SourceContext = try context.value()
|
11x |
33 |
let newContext = try contextTransformer.transform(typedContext)
|
11x |
34 |
return .updateContext(AnyContextBox(newContext))
|
11x |
35 |
}
|
11x |
36 |
}
|
|