Skip to content

Commit d6c66a6

Browse files
mluisbrownelkraneo
andcommitted
Add throttle overloads that take types (#1101)
Co-authored-by: Cristian Díaz <[email protected]>
1 parent 082cb0e commit d6c66a6

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

Sources/ComposableArchitecture/Effects/Throttling.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ extension Effect {
5959
}
6060
.cancellable(id: id, cancelInFlight: true)
6161
}
62+
63+
/// Throttles an effect so that it only publishes one output per given interval.
64+
///
65+
/// A convenience for calling ``Effect/throttle(id:for:scheduler:latest:)-5jfpx`` with a static
66+
/// type as the effect's unique identifier.
67+
///
68+
/// - Parameters:
69+
/// - id: The effect's identifier.
70+
/// - interval: The interval at which to find and emit the most recent element, expressed in
71+
/// the time system of the scheduler.
72+
/// - scheduler: The scheduler you want to deliver the throttled output to.
73+
/// - latest: A boolean value that indicates whether to publish the most recent element. If
74+
/// `false`, the publisher emits the first element received during the interval.
75+
/// - Returns: An effect that emits either the most-recent or first element received during the
76+
/// specified interval.
77+
public func throttle(
78+
id: Any.Type,
79+
for interval: TimeInterval,
80+
scheduler: DateScheduler,
81+
latest: Bool
82+
) -> Effect {
83+
self.throttle(id: ObjectIdentifier(id), for: interval, scheduler: scheduler, latest: latest)
84+
}
6285
}
6386

6487
var throttleTimes: [AnyHashable: Any] = [:]

Tests/ComposableArchitectureTests/EffectThrottleTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ final class EffectThrottleTests: XCTestCase {
1111
var effectRuns = 0
1212

1313
func runThrottledEffect(value: Int) {
14-
struct CancelToken: Hashable {}
14+
enum CancelToken {}
1515

1616
Effect.deferred { () -> Effect<Int, Never> in
1717
effectRuns += 1
1818
return .init(value: value)
1919
}
20-
.throttle(id: CancelToken(), for: 1, scheduler: scheduler, latest: true)
20+
.throttle(id: CancelToken.self, for: 1, scheduler: scheduler, latest: true)
2121
.startWithValues { values.append($0) }
2222
}
2323

@@ -61,13 +61,13 @@ final class EffectThrottleTests: XCTestCase {
6161
var effectRuns = 0
6262

6363
func runThrottledEffect(value: Int) {
64-
struct CancelToken: Hashable {}
64+
enum CancelToken {}
6565

6666
Effect.deferred { () -> Effect<Int, Never> in
6767
effectRuns += 1
6868
return .init(value: value)
6969
}
70-
.throttle(id: CancelToken(), for: 1, scheduler: scheduler, latest: false)
70+
.throttle(id: CancelToken.self, for: 1, scheduler: scheduler, latest: false)
7171
.startWithValues { values.append($0) }
7272
}
7373

@@ -124,13 +124,13 @@ final class EffectThrottleTests: XCTestCase {
124124
var effectRuns = 0
125125

126126
func runThrottledEffect(value: Int) {
127-
struct CancelToken: Hashable {}
127+
enum CancelToken {}
128128

129129
Effect.deferred { () -> Effect<Int, Never> in
130130
effectRuns += 1
131131
return .init(value: value)
132132
}
133-
.throttle(id: CancelToken(), for: 1, scheduler: scheduler, latest: true)
133+
.throttle(id: CancelToken.self, for: 1, scheduler: scheduler, latest: true)
134134
.startWithValues { values.append($0) }
135135
}
136136

@@ -165,14 +165,14 @@ final class EffectThrottleTests: XCTestCase {
165165
var effectRuns = 0
166166

167167
func runThrottledEffect(value: Int) {
168-
struct CancelToken: Hashable {}
168+
enum CancelToken {}
169169

170170
Effect.deferred { () -> Effect<Int, Never> in
171171
effectRuns += 1
172172
return .init(value: value)
173173
}
174174
.throttle(
175-
id: CancelToken(), for: 1, scheduler: scheduler, latest: false
175+
id: CancelToken.self, for: 1, scheduler: scheduler, latest: false
176176
)
177177
.startWithValues { values.append($0) }
178178
}

0 commit comments

Comments
 (0)