summaryrefslogtreecommitdiffhomepage
path: root/ios/Operations/InputOperation.swift
blob: 9d88861f4db2cad7eb6e1315b6570239f9dee766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
//  InputOperation.swift
//  MullvadVPN
//
//  Created by pronebird on 09/06/2022.
//  Copyright © 2022 Mullvad VPN AB. All rights reserved.
//

import Foundation

public protocol InputOperation: Operation {
    associatedtype Input

    var input: Input? { get }

    func setInputBlock(_ block: @escaping () -> Input?)

    func inject<T>(from dependency: T)
        where T: OutputOperation, T.Output == Input

    func inject<T>(from dependency: T, via block: @escaping (T.Output) -> Input)
        where T: OutputOperation
}

public extension InputOperation {
    func inject<T>(from dependency: T) where T: OutputOperation, T.Output == Input {
        inject(from: dependency, via: { $0 })
    }

    func inject<T>(from dependency: T, via block: @escaping (T.Output) -> Input)
        where T: OutputOperation
    {
        setInputBlock {
            return dependency.output.map { value in
                return block(value)
            }
        }
        addDependency(dependency)
    }

    func injectMany<Context>(context: Context) -> InputInjectionBuilder<Self, Context> {
        return InputInjectionBuilder(
            operation: self,
            context: context
        )
    }
}