Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the debounce performance #105

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 64 additions & 6 deletions Sources/OneWay/AnyEffect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ public struct AnyEffect<Element>: Effect where Element: Sendable {

/// Sends elements only after a specified time interval elapses between events.
///
/// First, create a Hashable ID that will be used to identify the debounce effect:
///
/// ```swift
/// enum DebounceID {
/// case searchText
/// }
/// ```
///
/// Then, apply the `debounce` modifier using the defined ID:
///
/// ```swift
/// func reduce(state: inout State, action: Action) -> AnyEffect<Action> {
/// switch action {
/// // ...
/// case let .search(text):
/// return .single {
/// let result = await api.request(text)
/// return .setResult(result)
/// }
/// .debounce(id: DebounceID.searchText, for: 0.5)
/// // ...
/// }
/// }
/// ```
///
/// - Parameters:
/// - id: The effect's identifier.
/// - seconds: The duration for which the effect should wait before sending an element.
Expand All @@ -54,16 +79,20 @@ public struct AnyEffect<Element>: Effect where Element: Sendable {
id: some EffectID,
for seconds: Double
) -> Self {
let base = base
var copy = self
copy.method = .register(id, cancelInFlight: true)
let values = copy.values
copy.base = Effects.Sequence(
operation: { send in
guard !Task.isCancelled else { return }
let NSEC_PER_SEC: Double = 1_000_000_000
let dueTime = NSEC_PER_SEC * seconds
try? await Task.sleep(nanoseconds: UInt64(dueTime))
for await value in values {
do {
try await Task.sleep(nanoseconds: UInt64(dueTime))
} catch {
return
}
for await value in base.values {
guard !Task.isCancelled else { return }
send(value)
}
Expand All @@ -74,6 +103,31 @@ public struct AnyEffect<Element>: Effect where Element: Sendable {

/// Sends elements only after a specified time interval elapses between events.
///
/// First, create a Hashable ID that will be used to identify the debounce effect:
///
/// ```swift
/// enum DebounceID {
/// case searchText
/// }
/// ```
///
/// Then, apply the `debounce` modifier using the defined ID:
///
/// ```swift
/// func reduce(state: inout State, action: Action) -> AnyEffect<Action> {
/// switch action {
/// // ...
/// case let .search(text):
/// return .single {
/// let result = await api.request(text)
/// return .setResult(result)
/// }
/// .debounce(id: DebounceID.searchText, for: .milliseconds(500))
/// // ...
/// }
/// }
/// ```
///
/// - Parameters:
/// - id: The effect's identifier.
/// - dueTime: The duration for which the effect should wait before sending an element.
Expand All @@ -85,14 +139,18 @@ public struct AnyEffect<Element>: Effect where Element: Sendable {
for dueTime: C.Instant.Duration,
clock: C = ContinuousClock()
) -> Self {
let base = base
var copy = self
copy.method = .register(id, cancelInFlight: true)
let values = copy.values
copy.base = Effects.Sequence(
operation: { send in
guard !Task.isCancelled else { return }
try? await clock.sleep(for: dueTime)
for await value in values {
do {
try await clock.sleep(for: dueTime)
} catch {
return
}
for await value in base.values {
guard !Task.isCancelled else { return }
send(value)
}
Expand Down
23 changes: 19 additions & 4 deletions Sources/OneWay/Effect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ public enum Effects {

public var values: AsyncStream<Element> {
AsyncStream { continuation in
Task(priority: priority) {
let task = Task(priority: priority) {
let result = await operation()
continuation.yield(result)
continuation.finish()
}
continuation.onTermination = { _ in
task.cancel()
}
}
}
}
Expand All @@ -111,10 +114,13 @@ public enum Effects {

public var values: AsyncStream<Element> {
AsyncStream { continuation in
Task(priority: priority) {
let task = Task(priority: priority) {
await operation { continuation.yield($0) }
continuation.finish()
}
continuation.onTermination = { _ in
task.cancel()
}
}
}
}
Expand All @@ -141,14 +147,18 @@ public enum Effects {

public var values: AsyncStream<Element> {
AsyncStream { continuation in
Task(priority: priority) {
let task = Task(priority: priority) {
for effect in effects {
for await value in effect.values {
guard !Task.isCancelled else { break }
continuation.yield(value)
}
}
continuation.finish()
}
continuation.onTermination = { _ in
task.cancel()
}
}
}
}
Expand All @@ -175,12 +185,13 @@ public enum Effects {

public var values: AsyncStream<Element> {
AsyncStream { continuation in
Task(priority: priority) {
let task = Task(priority: priority) {
if #available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) {
await withDiscardingTaskGroup { group in
for effect in effects {
group.addTask {
for await value in effect.values {
guard !Task.isCancelled else { break }
continuation.yield(value)
}
}
Expand All @@ -192,6 +203,7 @@ public enum Effects {
for effect in effects {
group.addTask {
for await value in effect.values {
guard !Task.isCancelled else { break }
continuation.yield(value)
}
}
Expand All @@ -200,6 +212,9 @@ public enum Effects {
continuation.finish()
}
}
continuation.onTermination = { _ in
task.cancel()
}
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions Sources/OneWay/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@ where R.Action: Sendable, R.State: Sendable & Equatable {
guard !isProcessing else { return }
isProcessing = true
await Task.yield()
let count = actionQueue.count
for index in Int.zero ..< count {
let action = actionQueue[index]
for action in actionQueue {
let taskID = TaskID()
let effect = reducer.reduce(state: &state, action: action)
let task = Task { [weak self, taskID] in
guard !Task.isCancelled else { return }
for await value in effect.values {
guard let self else { break }
guard !Task.isCancelled else { break }
Expand All @@ -102,14 +101,18 @@ where R.Action: Sendable, R.State: Sendable & Equatable {

switch effect.method {
case let .register(id, cancelInFlight):
let effectID = EffectIDWrapper(id)
if cancelInFlight {
let taskIDs = cancellables[EffectIDWrapper(id), default: []]
let taskIDs = cancellables[effectID, default: []]
taskIDs.forEach { removeTask($0) }
cancellables.removeValue(forKey: effectID)
}
cancellables[EffectIDWrapper(id), default: []].insert(taskID)
cancellables[effectID, default: []].insert(taskID)
case let .cancel(id):
let taskIDs = cancellables[EffectIDWrapper(id), default: []]
let effectID = EffectIDWrapper(id)
let taskIDs = cancellables[effectID, default: []]
taskIDs.forEach { removeTask($0) }
cancellables.removeValue(forKey: effectID)
case .none:
break
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/OneWayTests/StoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ private struct TestReducer: Reducer {

case .longTimeTask:
return .single {
try! await clock.sleep(for: .seconds(200))
try? await clock.sleep(for: .seconds(200))
return Action.response("Success")
}
.cancellable(EffectID.longTimeTask)
Expand Down