summaryrefslogtreecommitdiffhomepage
path: root/ios/Operations/InputOperation.swift
blob: 9c141671ec6cb776eb35b46987bdddade0fd0849 (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
//  Operations
//
//  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
}

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

    public 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)
    }

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