Slather logo

Coverage for "FactoryBox.swift" : 100.00%

(13 of 13 relevant lines covered)

RouteComposer/Classes/Router/Type Erasure/Boxes/FactoryBox.swift

1
//
2
// RouteComposer
3
// FactoryBox.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
import UIKit
15
16
struct FactoryBox<F: Factory>: PreparableAnyFactory, AnyFactoryBox, MainThreadChecking, CustomStringConvertible {
17
18
    typealias FactoryType = F
19
20
    var factory: F
21
22
    let action: AnyAction
23
24
    var isPrepared = false
25
26
    init?(_ factory: F, action: AnyAction) {
337x
27
        guard !(factory is NilEntity) else {
337x
28
            return nil
91x
29
        }
246x
30
        self.factory = factory
246x
31
        self.action = action
246x
32
    }
246x
33
34
    func build(with context: AnyContext) throws -> UIViewController {
127x
35
        let typedContext: FactoryType.Context = try context.value()
127x
36
        assertIfNotMainThread()
127x
37
        assertIfNotPrepared()
127x
38
        return try factory.build(with: typedContext)
127x
39
    }
127x
40
41
}