|
1 | 1 | import AppKit |
2 | 2 | import ComposableArchitecture |
| 3 | +import AsyncAlgorithms |
3 | 4 | import Dependencies |
4 | 5 | import Foundation |
5 | 6 | import PromptToCodeService |
@@ -43,7 +44,7 @@ public struct PromptToCode: ReducerProtocol { |
43 | 44 | } |
44 | 45 | } |
45 | 46 | } |
46 | | - |
| 47 | + |
47 | 48 | public enum FocusField: Equatable { |
48 | 49 | case textField |
49 | 50 | } |
@@ -113,7 +114,7 @@ public struct PromptToCode: ReducerProtocol { |
113 | 114 | self.generateDescriptionRequirement = generateDescriptionRequirement |
114 | 115 | self.isAttachedToSelectionRange = isAttachedToSelectionRange |
115 | 116 | self.commandName = commandName |
116 | | - |
| 117 | + |
117 | 118 | if selectionRange?.isEmpty ?? true { |
118 | 119 | self.isAttachedToSelectionRange = false |
119 | 120 | } |
@@ -151,7 +152,7 @@ public struct PromptToCode: ReducerProtocol { |
151 | 152 | switch action { |
152 | 153 | case .binding: |
153 | 154 | return .none |
154 | | - |
| 155 | + |
155 | 156 | case .focusOnTextField: |
156 | 157 | state.focusedField = .textField |
157 | 158 | return .none |
@@ -187,13 +188,23 @@ public struct PromptToCode: ReducerProtocol { |
187 | 188 | generateDescriptionRequirement: copiedState |
188 | 189 | .generateDescriptionRequirement |
189 | 190 | ) |
190 | | - #warning("TODO: make the action call debounced.") |
191 | | - for try await fragment in stream { |
192 | | - try Task.checkCancellation() |
193 | | - await send(.modifyCodeChunkReceived( |
194 | | - code: fragment.code, |
195 | | - description: fragment.description |
196 | | - )) |
| 191 | + |
| 192 | + if #available(macOS 13.0, *) { |
| 193 | + for try await fragment in stream.debounce(for: .milliseconds(400)) { |
| 194 | + try Task.checkCancellation() |
| 195 | + await send(.modifyCodeChunkReceived( |
| 196 | + code: fragment.code, |
| 197 | + description: fragment.description |
| 198 | + )) |
| 199 | + } |
| 200 | + } else { |
| 201 | + for try await fragment in stream { |
| 202 | + try Task.checkCancellation() |
| 203 | + await send(.modifyCodeChunkReceived( |
| 204 | + code: fragment.code, |
| 205 | + description: fragment.description |
| 206 | + )) |
| 207 | + } |
197 | 208 | } |
198 | 209 | try Task.checkCancellation() |
199 | 210 | await send(.modifyCodeFinished) |
|
0 commit comments