| 1 |
//
|
|
| 2 |
// RouteComposer
|
|
| 3 |
// RoutingInterceptorBox.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 RoutingInterceptorBox<RI: RoutingInterceptor>: AnyRoutingInterceptor, PreparableEntity, CustomStringConvertible, MainThreadChecking {
|
|
| 16 |
|
|
| 17 |
var routingInterceptor: RI
|
|
| 18 |
|
|
| 19 |
var isPrepared = false
|
|
| 20 |
|
|
| 21 |
init(_ routingInterceptor: RI) {
|
70x |
| 22 |
self.routingInterceptor = routingInterceptor
|
70x |
| 23 |
}
|
70x |
| 24 |
|
|
| 25 |
mutating func prepare(with context: AnyContext) throws {
|
335x |
| 26 |
let typedDestination: RI.Context = try context.value()
|
335x |
| 27 |
try routingInterceptor.prepare(with: typedDestination)
|
335x |
| 28 |
isPrepared = true
|
335x |
| 29 |
}
|
335x |
| 30 |
|
|
| 31 |
func perform(with context: AnyContext, completion: @escaping (RoutingResult) -> Void) {
|
328x |
| 32 |
do {
|
328x |
| 33 |
let typedContext: RI.Context = try context.value()
|
328x |
| 34 |
assertIfNotPrepared()
|
328x |
| 35 |
assertIfNotMainThread()
|
328x |
| 36 |
routingInterceptor.perform(with: typedContext) { result in
|
328x |
| 37 |
self.assertIfNotMainThread()
|
327x |
| 38 |
completion(result)
|
327x |
| 39 |
}
|
327x |
| 40 |
} catch {
|
328x |
| 41 |
completion(.failure(error))
|
1x |
| 42 |
}
|
328x |
| 43 |
|
328x |
| 44 |
}
|
328x |
| 45 |
|
|
| 46 |
var description: String {
|
3x |
| 47 |
String(describing: routingInterceptor)
|
3x |
| 48 |
}
|
3x |
| 49 |
|
|
| 50 |
}
|
|