File tree Expand file tree Collapse file tree
Core/Sources/ChatGPTChatTab Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -226,9 +226,13 @@ struct Chat: ReducerProtocol {
226226 cancellable. cancel ( )
227227 }
228228 }
229- for await _ in stream {
229+ let debouncedHistoryChange = TimedDebounceFunction ( duration : 0.2 ) {
230230 await send ( . historyChanged)
231231 }
232+
233+ for await _ in stream {
234+ await debouncedHistoryChange ( )
235+ }
232236 } . cancellable ( id: CancelID . observeHistoryChange ( id) , cancelInFlight: true )
233237
234238 case . observeIsReceivingMessageChange:
@@ -450,3 +454,33 @@ struct ChatMenu: ReducerProtocol {
450454 }
451455}
452456
457+ private actor TimedDebounceFunction {
458+ let duration : TimeInterval
459+ let block : ( ) async -> Void
460+
461+ var task : Task < Void , Error > ?
462+ var lastFireTime : Date = . init( timeIntervalSince1970: 0 )
463+
464+ init ( duration: TimeInterval , block: @escaping ( ) async -> Void ) {
465+ self . duration = duration
466+ self . block = block
467+ }
468+
469+ func callAsFunction( ) async {
470+ task? . cancel ( )
471+ if lastFireTime. timeIntervalSinceNow < - duration {
472+ await fire ( )
473+ task = nil
474+ } else {
475+ task = Task . detached { [ weak self, duration] in
476+ try await Task . sleep ( nanoseconds: UInt64 ( duration * 1_000_000_000 ) )
477+ await self ? . fire ( )
478+ }
479+ }
480+ }
481+
482+ func fire( ) async {
483+ lastFireTime = Date ( )
484+ await block ( )
485+ }
486+ }
Original file line number Diff line number Diff line change @@ -228,7 +228,10 @@ struct ChatPanelMessages: View {
228228 if isInitialLoad {
229229 isInitialLoad = false
230230 }
231- scrollToBottom ( )
231+ Task {
232+ await Task . yield ( )
233+ scrollToBottom ( )
234+ }
232235 }
233236 }
234237 }
You can’t perform that action at this time.
0 commit comments