Skip to content

Commit 183fd5a

Browse files
committed
Extensible FlattenStrategy.
1 parent bdd2d26 commit 183fd5a

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

Sources/Flatten.swift

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,32 @@
99
import enum Result.NoError
1010

1111
/// Describes how multiple producers should be joined together.
12-
public enum FlattenStrategy: Equatable {
12+
public struct FlattenStrategy {
13+
fileprivate enum Kind {
14+
case concurrent(limit: UInt)
15+
case latest
16+
case race
17+
}
18+
19+
fileprivate let kind: Kind
20+
21+
private init(kind: Kind) {
22+
self.kind = kind
23+
}
24+
1325
/// The producers should be merged, so that any value received on any of the
1426
/// input producers will be forwarded immediately to the output producer.
1527
///
1628
/// The resulting producer will complete only when all inputs have
1729
/// completed.
18-
public static let merge = FlattenStrategy.concurrent(limit: .max)
30+
public static let merge = FlattenStrategy(kind: .concurrent(limit: .max))
1931

2032
/// The producers should be concatenated, so that their values are sent in
2133
/// the order of the producers themselves.
2234
///
2335
/// The resulting producer will complete only when all inputs have
2436
/// completed.
25-
public static let concat = FlattenStrategy.concurrent(limit: 1)
37+
public static let concat = FlattenStrategy(kind: .concurrent(limit: 1))
2638

2739
/// The producers should be merged, but only up to the given limit at any
2840
/// point of time, so that any value received on any of the input producers
@@ -35,15 +47,17 @@ public enum FlattenStrategy: Equatable {
3547
/// completed.
3648
///
3749
/// - precondition: `limit > 0`.
38-
case concurrent(limit: UInt)
50+
public static func concurrent(limit: UInt) -> FlattenStrategy {
51+
return FlattenStrategy(kind: .concurrent(limit: limit))
52+
}
3953

4054
/// Only the events from the latest input producer should be considered for
4155
/// the output. Any producers received before that point will be disposed
4256
/// of.
4357
///
4458
/// The resulting producer will complete only when the producer-of-producers
4559
/// and the latest producer has completed.
46-
case latest
60+
public static let latest = FlattenStrategy(kind: .latest)
4761

4862
/// Only the events from the "first input producer to send an event" (winning producer)
4963
/// should be considered for the output.
@@ -53,20 +67,7 @@ public enum FlattenStrategy: Equatable {
5367
/// The resulting producer will complete when:
5468
/// 1. The producer-of-producers and the first "alive" producer has completed.
5569
/// 2. The producer-of-producers has completed without inner producer being "alive".
56-
case race
57-
58-
public static func ==(left: FlattenStrategy, right: FlattenStrategy) -> Bool {
59-
switch (left, right) {
60-
case (.latest, .latest):
61-
return true
62-
63-
case (.concurrent(let leftLimit), .concurrent(let rightLimit)):
64-
return leftLimit == rightLimit
65-
66-
default:
67-
return false
68-
}
69-
}
70+
public static let race = FlattenStrategy(kind: .race)
7071
}
7172

7273
extension Signal where Value: SignalProducerConvertible, Error == Value.Error {
@@ -82,7 +83,7 @@ extension Signal where Value: SignalProducerConvertible, Error == Value.Error {
8283
/// - parameters:
8384
/// - strategy: Strategy used when flattening signals.
8485
public func flatten(_ strategy: FlattenStrategy) -> Signal<Value.Value, Error> {
85-
switch strategy {
86+
switch strategy.kind {
8687
case .concurrent(let limit):
8788
return self.concurrent(limit: limit)
8889

@@ -124,7 +125,7 @@ extension Signal where Value: SignalProducerConvertible, Error == NoError, Value
124125
/// - parameters:
125126
/// - strategy: Strategy used when flattening signals.
126127
public func flatten(_ strategy: FlattenStrategy) -> Signal<Value.Value, Value.Error> {
127-
switch strategy {
128+
switch strategy.kind {
128129
case .concurrent(let limit):
129130
return self.concurrent(limit: limit)
130131

@@ -167,7 +168,7 @@ extension SignalProducer where Value: SignalProducerConvertible, Error == Value.
167168
/// - parameters:
168169
/// - strategy: Strategy used when flattening signals.
169170
public func flatten(_ strategy: FlattenStrategy) -> SignalProducer<Value.Value, Error> {
170-
switch strategy {
171+
switch strategy.kind {
171172
case .concurrent(let limit):
172173
return self.concurrent(limit: limit)
173174

@@ -209,7 +210,7 @@ extension SignalProducer where Value: SignalProducerConvertible, Error == NoErro
209210
/// - parameters:
210211
/// - strategy: Strategy used when flattening signals.
211212
public func flatten(_ strategy: FlattenStrategy) -> SignalProducer<Value.Value, Value.Error> {
212-
switch strategy {
213+
switch strategy.kind {
213214
case .concurrent(let limit):
214215
return self.concurrent(limit: limit)
215216

0 commit comments

Comments
 (0)