Skip to content

Commit c835496

Browse files
committed
Naming fixes to Effect.throttle and Effect.debounce.
1 parent fe43d83 commit c835496

File tree

6 files changed

+23
-47
lines changed

6 files changed

+23
-47
lines changed

Examples/Search/Search/SearchView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ let searchReducer = Reducer<SearchState, SearchAction, SearchEnvironment> {
7171
.searchLocation(query)
7272
.observe(on: environment.mainQueue)
7373
.catchToEffect()
74-
.debounce(id: SearchLocationId(), interval: 0.3, scheduler: environment.mainQueue)
74+
.debounce(id: SearchLocationId(), for: 0.3, scheduler: environment.mainQueue)
7575
.map(SearchAction.locationsResponse)
7676

7777
case let .locationWeatherResponse(.failure(locationWeather)):

Examples/Todos/Todos/Todos.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ let appReducer = Reducer<AppState, AppAction, AppEnvironment>.combine(
7878
case .todo(id: _, action: .checkBoxToggled):
7979
struct TodoCompletionId: Hashable {}
8080
return Effect(value: .sortCompletedTodos)
81-
.debounce(id: TodoCompletionId(), interval: 1, scheduler: environment.mainQueue.animation())
81+
.debounce(id: TodoCompletionId(), for: 1, scheduler: environment.mainQueue.animation())
8282

8383
case .todo:
8484
return .none

Sources/ComposableArchitecture/Effects/Debouncing.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ extension Effect {
2121
///
2222
/// - Parameters:
2323
/// - id: The effect's identifier.
24-
/// - dueTime: The duration you want to debounce for.
24+
/// - for: The duration you want to debounce for.
2525
/// - scheduler: The scheduler you want to deliver the debounced output to.
2626
/// - options: Scheduler options that customize the effect's delivery of elements.
2727
/// - Returns: An effect that publishes events only after a specified time elapses.
2828
public func debounce(
2929
id: AnyHashable,
30-
interval: TimeInterval,
30+
for interval: TimeInterval,
3131
scheduler: DateScheduler
3232
) -> Effect<Value, Error> {
3333
Effect<Void, Never>.init(value: ())

Sources/ComposableArchitecture/Effects/Throttling.swift

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,17 @@ extension Effect {
77
///
88
/// - Parameters:
99
/// - id: The effect's identifier.
10-
/// - interval: The interval at which to find and emit the most recent element, expressed in
10+
/// - for: The interval at which to find and emit the most recent element, expressed in
1111
/// the time system of the scheduler.
1212
/// - scheduler: The scheduler you want to deliver the throttled output to.
1313
/// - latest: A boolean value that indicates whether to publish the most recent element. If
1414
/// `false`, the producer emits the first element received during the interval.
1515
/// - Returns: An effect that emits either the most-recent or first element received during the
1616
/// specified interval.
17-
<<<<<<< ours:Sources/ComposableArchitecture/Internal/Throttling.swift
18-
func throttle(
19-
=======
20-
public func throttle<S>(
21-
>>>>>>> theirs:Sources/ComposableArchitecture/Effects/Throttling.swift
17+
public func throttle(
2218
id: AnyHashable,
23-
interval: TimeInterval,
24-
on scheduler: DateScheduler,
19+
for interval: TimeInterval,
20+
scheduler: DateScheduler,
2521
latest: Bool
2622
) -> Effect<Value, Error> {
2723
self.flatMap(.latest) { value -> Effect<Value, Error> in
@@ -31,41 +27,23 @@ extension Effect {
3127
return Effect(value: value)
3228
}
3329

34-
<<<<<<< ours:Sources/ComposableArchitecture/Internal/Throttling.swift
30+
let value = latest ? value : (throttleValues[id] as! Value? ?? value)
31+
throttleValues[id] = value
32+
3533
guard
3634
scheduler.currentDate.timeIntervalSince1970 - throttleTime.timeIntervalSince1970 < interval
3735
else {
3836
throttleTimes[id] = scheduler.currentDate
39-
=======
40-
let value = latest ? value : (throttleValues[id] as! Output? ?? value)
41-
throttleValues[id] = value
42-
43-
guard throttleTime.distance(to: scheduler.now) < interval else {
44-
throttleTimes[id] = scheduler.now
45-
>>>>>>> theirs:Sources/ComposableArchitecture/Effects/Throttling.swift
4637
throttleValues[id] = nil
4738
return Effect(value: value)
4839
}
4940

50-
<<<<<<< ours:Sources/ComposableArchitecture/Internal/Throttling.swift
51-
let value = latest ? value : (throttleValues[id] as! Value? ?? value)
52-
throttleValues[id] = value
53-
5441
return Effect(value: value)
55-
=======
56-
return Just(value)
57-
>>>>>>> theirs:Sources/ComposableArchitecture/Effects/Throttling.swift
5842
.delay(
5943
throttleTime.addingTimeInterval(interval).timeIntervalSince1970
6044
- scheduler.currentDate.timeIntervalSince1970,
6145
on: scheduler
62-
)
63-
<<<<<<< ours:Sources/ComposableArchitecture/Internal/Throttling.swift
64-
=======
65-
.handleEvents(receiveOutput: { _ in throttleTimes[id] = scheduler.now })
66-
.setFailureType(to: Failure.self)
67-
.eraseToAnyPublisher()
68-
>>>>>>> theirs:Sources/ComposableArchitecture/Effects/Throttling.swift
46+
).on(value: { _ in throttleTimes[id] = scheduler.currentDate })
6947
}
7048
.cancellable(id: id, cancelInFlight: true)
7149
}

Tests/ComposableArchitectureTests/EffectDebounceTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class EffectDebounceTests: XCTestCase {
1111
struct CancelToken: Hashable {}
1212

1313
Effect(value: value)
14-
.debounce(id: CancelToken(), interval: 1, scheduler: scheduler)
14+
.debounce(id: CancelToken(), for: 1, scheduler: scheduler)
1515
.startWithValues { values.append($0) }
1616
}
1717

@@ -59,7 +59,7 @@ final class EffectDebounceTests: XCTestCase {
5959
effectRuns += 1
6060
return Effect(value: value)
6161
}
62-
.debounce(id: CancelToken(), interval: 1, scheduler: scheduler)
62+
.debounce(id: CancelToken(), for: 1, scheduler: scheduler)
6363
.startWithValues { values.append($0) }
6464
}
6565

Tests/ComposableArchitectureTests/EffectThrottleTests.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class EffectThrottleTests: XCTestCase {
1717
effectRuns += 1
1818
return .init(value: value)
1919
}
20-
.throttle(id: CancelToken(), interval: 1, on: scheduler, latest: true)
20+
.throttle(id: CancelToken(), for: 1, scheduler: scheduler, latest: true)
2121
.startWithValues { values.append($0) }
2222
}
2323

@@ -63,7 +63,7 @@ final class EffectThrottleTests: XCTestCase {
6363
effectRuns += 1
6464
return .init(value: value)
6565
}
66-
.throttle(id: CancelToken(), interval: 1, on: scheduler, latest: false)
66+
.throttle(id: CancelToken(), for: 1, scheduler: scheduler, latest: false)
6767
.startWithValues { values.append($0) }
6868
}
6969

@@ -109,7 +109,7 @@ final class EffectThrottleTests: XCTestCase {
109109
effectRuns += 1
110110
return .init(value: value)
111111
}
112-
.throttle(id: CancelToken(), interval: 1, on: scheduler, latest: true)
112+
.throttle(id: CancelToken(), for: 1, scheduler: scheduler, latest: true)
113113
.startWithValues { values.append($0) }
114114
}
115115

@@ -133,28 +133,26 @@ final class EffectThrottleTests: XCTestCase {
133133
func runThrottledEffect(value: Int) {
134134
struct CancelToken: Hashable {}
135135

136-
Deferred { () -> Just<Int> in
136+
Effect.deferred { () -> Effect<Int, Never> in
137137
effectRuns += 1
138-
return Just(value)
138+
return .init(value: value)
139139
}
140-
.eraseToEffect()
141140
.throttle(
142-
id: CancelToken(), for: 1, scheduler: scheduler.eraseToAnyScheduler(), latest: false
141+
id: CancelToken(), for: 1, scheduler: scheduler, latest: false
143142
)
144-
.sink { values.append($0) }
145-
.store(in: &self.cancellables)
143+
.startWithValues { values.append($0) }
146144
}
147145

148146
runThrottledEffect(value: 1)
149147

150148
// A value emits right away.
151149
XCTAssertEqual(values, [1])
152150

153-
scheduler.advance(by: 0.5)
151+
scheduler.advance(by: .milliseconds(500))
154152

155153
runThrottledEffect(value: 2)
156154

157-
scheduler.advance(by: 0.5)
155+
scheduler.advance(by: .milliseconds(500))
158156

159157
runThrottledEffect(value: 3)
160158

0 commit comments

Comments
 (0)