Skip to content

Slows down as message gets longer #2

@macmetrep

Description

@macmetrep

I was testing out your view and noticed it slows down as more and more text gets added to the view? You can run this test code to see it in action

slow-demo.mov
import SwiftUI
import StreamChatAI

class MockStreamingStringSource: ObservableObject {
    @Published var text = ""
    let source = "**load_eligibility_plist**: Failed to open\n* One\n* Two\n* Three\n"
    let amountLimit = 20
    var count = 0
    var timer: Timer?
    
    init() {
        timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { t in
            self.click()
        })
    }
    deinit {
        timer?.invalidate()
        timer = nil
    }
    
    func click() {
        text.append(" \(count): ")
        text.append(source)
        count += 1
        if count >= amountLimit {
            timer?.invalidate()
            timer = nil
            text.append("FINISHED!!")
        }
    }
}


struct StreamingMessagePreviewView: View {
    @StateObject var stream: MockStreamingStringSource = .init()
    var body: some View {
        StreamingMessageView(
            content: stream.text,
            isGenerating: true
        )
    }
}

#Preview {
    ScrollView {
        VStack {
            StreamingMessagePreviewView()
        }
        
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions