-
Notifications
You must be signed in to change notification settings - Fork 0
/
Callbag.swift
141 lines (125 loc) · 2.93 KB
/
Callbag.swift
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/// A Callbag dynamically receives input of type Input
/// and dynamically delivers output of type Output
public enum Callbag<Input, Output> {
indirect case start((Callbag<Output, Input>) -> Void)
case next(Input)
case completed(Completion)
}
public extension Callbag {
var isForcedToComplete: Bool {
switch self {
case .completed(.failed): return true
default: return false
}
}
}
public extension Callbag {
static func talkback<T>(_ tb: Optional<SourceTalkback<T>>) -> Callbag {
return Callbag.start({
switch $0 {
case .start: return
case let .next(element):
tb?(.next(element as? T))
case let .completed(completion):
tb?(.completed(completion))
}
})
}
static func whenReceiveCompletion(_ closure: @escaping () -> Void) -> Callbag {
return Callbag.start({
if case .completed = $0 {
closure()
}
})
}
}
extension Callbag: Equatable where Input: Equatable {
public static func == (lhs: Callbag, rhs: Callbag) -> Bool {
switch (lhs, rhs) {
case let (.next(a), .next(b)):
return a == b
case let (.completed(a), .completed(b)):
return a == b
default:
return false
}
}
public static func == (lhs: Callbag, rhs: Input) -> Bool {
switch (lhs, rhs) {
case let (.next(a), b):
return a == b
default:
return false
}
}
}
public func == <I, O>(lhs: Callbag<I, O>, rhs: Completion) -> Bool {
switch (lhs, rhs) {
case let (.completed(a), b):
return a == b
default:
return false
}
}
extension Callbag {
public var isStart: Bool {
if case .start = self {
return true
} else {
return false
}
}
public var isNext: Bool {
if case .next = self {
return true
} else {
return false
}
}
public var isCompleted: Bool {
if case .completed = self {
return true
} else {
return false
}
}
public var talkback: Optional<(Callbag<Output, Input>) -> Void> {
if case let .start(tb) = self {
return .some(tb)
} else {
return .none
}
}
public var element: Optional<Input> {
if case let .next(element) = self {
return .some(element)
} else {
return .none
}
}
public var completion: Optional<Completion> {
if case let .completed(completion) = self {
return .some(completion)
} else {
return .none
}
}
}
extension Callbag: CustomStringConvertible {
public var description: String {
switch self {
case .start:
if Output.self is Optional<Any>.Type {
return "start((Source<\(Input.self)>) -> Void)"
} else if Input.self is Optional<Any>.Type {
return "start((Sink<\(Output.self)>) -> Void)"
} else {
return "start((Callbag<\(Output.self), \(Input.self)>) -> Void)"
}
case .next(let item):
return "next(\(item))"
case .completed(let c):
return "completed(\(c))"
}
}
}