Skip to content

Commit 3df41f0

Browse files
committed
Make sure debounced values are delivered on specified scheduler. (#714)
* Make sure debounced values are delivered on specified scheduler. * fix deferring too * wip
1 parent b3d2d78 commit 3df41f0

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

Examples/Search/Search/SearchView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ let searchReducer = Reducer<SearchState, SearchAction, SearchEnvironment> {
6969

7070
return environment.weatherClient
7171
.searchLocation(query)
72-
.observe(on: environment.mainQueue)
7372
.catchToEffect()
7473
.debounce(id: SearchLocationId(), for: 0.3, scheduler: environment.mainQueue)
7574
.map(SearchAction.locationsResponse)

Sources/ComposableArchitecture/Effects/Debouncing.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ extension Effect {
1010
/// protection against typos by defining a new type that conforms to `Hashable`, such as an empty
1111
/// struct:
1212
///
13-
/// ```swift
14-
/// case let .textChanged(text):
15-
/// struct SearchId: Hashable {}
13+
/// ```swift
14+
/// case let .textChanged(text):
15+
/// struct SearchId: Hashable {}
1616
///
17-
/// return environment.search(text)
18-
/// .map(Action.searchResponse)
19-
/// .debounce(id: SearchId(), for: 0.5, scheduler: environment.mainQueue)
20-
/// ```
17+
/// return environment.search(text)
18+
/// .debounce(id: SearchId(), for: 0.5, scheduler: environment.mainQueue)
19+
/// .map(Action.searchResponse)
20+
/// ```
2121
///
2222
/// - Parameters:
2323
/// - id: The effect's identifier.
24-
/// - for: The duration you want to debounce for.
24+
/// - dueTime: 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-
for interval: TimeInterval,
30+
for dueTime: TimeInterval,
3131
scheduler: DateScheduler
3232
) -> Effect<Value, Error> {
3333
Effect<Void, Never>.init(value: ())
3434
.promoteError(Error.self)
35-
.delay(interval, on: scheduler)
36-
.flatMap(.latest) { self }
35+
.delay(dueTime, on: scheduler)
36+
.flatMap(.latest) { self.observe(on: scheduler) }
3737
.cancellable(id: id, cancelInFlight: true)
3838
}
3939
}

Sources/ComposableArchitecture/Effects/Deferring.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ extension Effect {
77
/// ```swift
88
/// case let .textChanged(text):
99
/// return environment.search(text)
10-
/// .map(Action.searchResponse)
1110
/// .deferred(for: 0.5, scheduler: environment.mainQueue)
11+
/// .map(Action.searchResponse)
1212
/// ```
1313
///
1414
/// - Parameters:
@@ -23,6 +23,6 @@ extension Effect {
2323
) -> Effect<Value, Error> {
2424
SignalProducer<Void, Never>(value: ())
2525
.delay(dueTime, on: scheduler)
26-
.flatMap(.latest) { self }
26+
.flatMap(.latest) { self.observe(on: scheduler) }
2727
}
2828
}

0 commit comments

Comments
 (0)