-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThrottleWhile.swift
151 lines (134 loc) · 4.2 KB
/
ThrottleWhile.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
import Combine
import Foundation
import Synchronized
public extension Publisher {
func throttle<Regulator: Publisher>(
while regulator: Regulator,
latest: Bool = true
) -> Publishers.ThrottleWhile<Self, Regulator>
where Regulator.Output == Bool
{
Publishers.ThrottleWhile(
upstream: self,
regulator: regulator,
latest: latest
)
}
}
public extension Publishers {
struct ThrottleWhile<
Upstream: Publisher,
Regulator: Publisher
>: Publisher
where Regulator.Output == Bool, Upstream.Failure == Regulator.Failure
{
public typealias Output = Upstream.Output
public typealias Failure = Upstream.Failure
private let upstream: Upstream
private let regulator: Regulator
private let latest: Bool
public init(upstream: Upstream, regulator: Regulator, latest: Bool) {
self.upstream = upstream
self.regulator = regulator
self.latest = latest
}
public func receive<S: Subscriber>(
subscriber: S
) where S.Input == Output, S.Failure == Failure {
var state = State<Output>.waiting
let regulatorSubject = CurrentValueSubject<
Bool,
Regulator.Failure
>(false)
var subscription: AnyCancellable? = regulator
.subscribe(regulatorSubject)
upstream
.enumerated()
.handleEvents(receiveCompletion: { completion in
state.receiveCompletion()
regulatorSubject.send(completion: completion)
subscription?.cancel()
subscription = nil
})
.combineLatest(regulatorSubject)
.compactMap { (upstream: (Int, Output), shouldThrottle: Bool) -> Output? in
let emissionIndex = upstream.0
let output = upstream.1
return state.receiveValue(
output,
emissionIndex: emissionIndex,
shouldThrottle: shouldThrottle,
latest: latest
)
}
.receive(subscriber: subscriber)
}
}
}
private enum State<Output> {
case publishing(Int)
case terminal
case throttling(Output)
case throttlingAndWaitingForOutput(Int)
case waiting
mutating func receiveCompletion() {
self = .terminal
}
mutating func receiveValue(
_ value: Output,
emissionIndex: Int,
shouldThrottle: Bool,
latest: Bool
) -> Output? {
switch self {
case let .publishing(i):
if shouldThrottle {
if emissionIndex == i {
self = .throttlingAndWaitingForOutput(emissionIndex)
return nil
} else {
self = .throttling(value)
return nil
}
} else {
self = .publishing(emissionIndex)
return value
}
case .terminal:
return nil
case let .throttling(oldValue):
if shouldThrottle {
if latest {
self = .throttling(value)
}
return nil
} else {
self = .publishing(emissionIndex)
if latest {
return value
} else {
return oldValue
}
}
case let .throttlingAndWaitingForOutput(i):
guard shouldThrottle else {
self = .publishing(emissionIndex)
return nil
}
if i == emissionIndex {
return nil
} else {
self = .throttling(value)
return nil
}
case .waiting:
if shouldThrottle {
self = .throttling(value)
return nil
} else {
self = .publishing(emissionIndex)
return value
}
}
}
}