-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combine.swift
175 lines (166 loc) · 4.72 KB
/
Combine.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
public func combine<A, B>(
_ a: @escaping Producer<A>,
_ b: @escaping Producer<B>
) -> Producer<(A, B)> {
return { sink in
let lock: RecursiveLock = RecursiveLock(label: DOMAIN("combine"))
var didReceiveCompletion: Bool = false
var didSendCompletion: Bool = false
let sources: Array<Any> = Array(arrayLiteral: a, b)
var talkbacks: Array<Optional<Any>> = .init(repeating: .none, count: 2)
var didStartSource: Array<Bool> = .init(repeating: false, count: 2)
var latestElements: Array<Optional<Any>> = .init(repeating: .none, count: 2)
var didSetInitials: Bool = false
let publish: (Int, Any) -> Void = { (index, element) in
latestElements[index] = element
if didSetInitials {
sink(.next((
latestElements[0] as! A,
latestElements[1] as! B
)))
}
if latestElements.allSatisfy(notEquals(.none)) {
didSetInitials = true
sink(.next((
latestElements[0] as! A,
latestElements[1] as! B
)))
}
}
func start<T>( _ index: Int, _ source: @escaping Producer<T>) {
source { payload in
lock.withLock {
switch payload {
case let .start(tb):
didStartSource[index] = true
talkbacks[index] = .some(tb)
case let .next(element):
if !didReceiveCompletion {
publish(index, element)
} else {
(talkbacks[index] as! SourceTalkback<T>)(.completed(.finished))
}
case let .completed(completion):
if !didSendCompletion {
talkbacks[index] = .none
if payload.isForcedToComplete {
didSendCompletion = true
sink(.completed(completion))
/// stop the other payload
if index == 0 {
(talkbacks[1] as? SourceTalkback<B>)?(.completed(.finished))
} else {
(talkbacks[0] as? SourceTalkback<A>)?(.completed(.finished))
}
} else if talkbacks.allSatisfy(equals(.none)) &&
didStartSource.allSatisfy(equals(true)) {
didSendCompletion = true
sink(.completed(completion))
}
}
}
}
}
}
sink(.start({ _ in lock.withLock { didReceiveCompletion = true } }))
start(0, sources[0] as! Producer<A>)
start(1, sources[1] as! Producer<B>)
}
}
public func combine<A, B, C>(
_ a: @escaping Producer<A>,
_ b: @escaping Producer<B>,
_ c: @escaping Producer<C>
) -> Producer<(A, B, C)> {
return map(unpackTuple)(
combine( combine(a, b), c )
)
}
public func combine<A, B, C, D>(
_ a: @escaping Producer<A>,
_ b: @escaping Producer<B>,
_ c: @escaping Producer<C>,
_ d: @escaping Producer<D>
) -> Producer<(A, B, C, D)> {
return map(unpackTuple)(
combine( combine( combine(a, b), c ), d )
)
}
public func combine<A, B, C, D, E>(
_ a: @escaping Producer<A>,
_ b: @escaping Producer<B>,
_ c: @escaping Producer<C>,
_ d: @escaping Producer<D>,
_ e: @escaping Producer<E>
) -> Producer<(A, B, C, D, E)> {
return map(unpackTuple)(
combine( combine( combine( combine(a, b), c ), d ), e )
)
}
public func combine<A, B, C, D, E, F>(
_ a: @escaping Producer<A>,
_ b: @escaping Producer<B>,
_ c: @escaping Producer<C>,
_ d: @escaping Producer<D>,
_ e: @escaping Producer<E>,
_ f: @escaping Producer<F>
) -> Producer<(A, B, C, D, E, F)> {
return map(unpackTuple)(
combine( combine( combine( combine( combine(a, b), c ), d ), e ), f )
)
}
public func combine<A, B>(
_ b: @escaping Producer<B>
) -> Operator<A, (A, B)> {
return { a in
return { sink in
combine(a, b)(sink)
}
}
}
public func combine<A, B, C>(
_ b: @escaping Producer<B>,
_ c: @escaping Producer<C>
) -> Operator<A, (A, B, C)> {
return { a in
return { sink in
combine(a, b, c)(sink)
}
}
}
public func combine<A, B, C, D>(
_ b: @escaping Producer<B>,
_ c: @escaping Producer<C>,
_ d: @escaping Producer<D>
) -> Operator<A, (A, B, C, D)> {
return { a in
return { sink in
combine(a, b, c, d)(sink)
}
}
}
public func combine<A, B, C, D, E>(
_ b: @escaping Producer<B>,
_ c: @escaping Producer<C>,
_ d: @escaping Producer<D>,
_ e: @escaping Producer<E>
) -> Operator<A, (A, B, C, D, E)> {
return { a in
return { sink in
combine(a, b, c, d, e)(sink)
}
}
}
public func combine<A, B, C, D, E, F>(
_ b: @escaping Producer<B>,
_ c: @escaping Producer<C>,
_ d: @escaping Producer<D>,
_ e: @escaping Producer<E>,
_ f: @escaping Producer<F>
) -> Operator<A, (A, B, C, D, E, F)> {
return { a in
return { sink in
combine(a, b, c, d, e, f)(sink)
}
}
}