From 2a4692ff725ec8956f97568f3657fe95477e2ff1 Mon Sep 17 00:00:00 2001 From: Server Cimen <505984+servercimen@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:27:15 -0800 Subject: [PATCH] Show streamed text in a lazy stack row that SwiftUI has not placed A `TranscriptText(stream:)` added to a `LazyVStack` could stay empty. After 50 rows of 1,400 points, the answer row never showed. The lazy stack added the row without placing it. The view asked SwiftUI to measure it again through its own state, but in this layout SwiftUI ignored changes inside the row. Now the stream has an observable revision. `TranscriptText(stream:)` reads it and keeps it when it is made. While the row is not placed, each new height changes the revision. The view whose body made the `TranscriptText` then runs again and gets a different value, and SwiftUI updates, measures and places the row. This works when `TranscriptText(stream:)` is made in the `ForEach` closure or in the view that holds the stack. In the body of a row view of its own, the change stays inside the row, and the row can stay empty. In the same layout, SwiftUI `Text` that reads an observable model in a row view of its own also stayed empty. Co-Authored-By: Claude Opus 5.5 --- README.md | 6 +++++ .../Stream/TranscriptStream.swift | 10 ++++++++ .../TranscriptKit/iOS/TranscriptText.swift | 15 +++++++++++- Tests/TranscriptKitTests/StreamTests.swift | 24 +++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2e3a04e..15e4433 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,12 @@ struct AnswerView: View { the background and goes straight to the view, without a SwiftUI update. SwiftUI is involved only when the text's height changes. `finish()` shows any text that was held back in case it turned into markup. +In a `LazyVStack`, make `TranscriptText(stream:)` in the `ForEach` closure or in the view that holds the stack, not in +the `body` of a row view of its own. A lazy stack can add a row without placing it, and it can then ignore changes +inside that row. When the streamed text grows in a row that the stack has not placed, `TranscriptText` makes the view +that made it update, and SwiftUI then measures and places the row. In a row view of its own, that update stays inside +the row, and the row can stay empty. + For text that is already complete, parse it once and pass the document: ```swift diff --git a/Sources/TranscriptKit/Stream/TranscriptStream.swift b/Sources/TranscriptKit/Stream/TranscriptStream.swift index 54cb867..4124486 100644 --- a/Sources/TranscriptKit/Stream/TranscriptStream.swift +++ b/Sources/TranscriptKit/Stream/TranscriptStream.swift @@ -1,4 +1,5 @@ import Foundation +import Observation #if canImport(UIKit) && !os(watchOS) import QuartzCore import UIKit @@ -38,6 +39,10 @@ public final class TranscriptStream { /// need it now. public private(set) var document: TranscriptDocument + /// Changed when a view of the stream that SwiftUI has not placed yet gets + /// a new height (see `TranscriptText`). + let unplacedHeight = UnplacedHeightSignal() + private let worker: StreamWorker private var documentSequence = 0 private var pending = "" @@ -182,6 +187,11 @@ public final class TranscriptStream { #endif } +@Observable +final class UnplacedHeightSignal { + var revision = 0 +} + /// The parser and the attached sinks. `parser` is only touched on `queue`. final class StreamWorker: @unchecked Sendable { let queue = DispatchQueue(label: "TranscriptKit.TranscriptStream", qos: .userInitiated) diff --git a/Sources/TranscriptKit/iOS/TranscriptText.swift b/Sources/TranscriptKit/iOS/TranscriptText.swift index 7defa3b..1a8d959 100644 --- a/Sources/TranscriptKit/iOS/TranscriptText.swift +++ b/Sources/TranscriptKit/iOS/TranscriptText.swift @@ -33,6 +33,8 @@ public struct TranscriptText: UIViewRepresentable { /// so that SwiftUI measures the view again (see /// `TranscriptTextView.onHeightChangeWhileUnsized`). @State private var unsizedRevision = 0 + /// The stream's `unplacedHeight` revision when this was made. + private var unplacedHeightRevision = 0 public init(_ document: TranscriptDocument, style: TranscriptStyle = .init()) { self.source = .document(document) @@ -42,6 +44,12 @@ public struct TranscriptText: UIViewRepresentable { public init(stream: TranscriptStream, style: TranscriptStyle = .init()) { self.source = .stream(stream) self.style = style + // A lazy stack can leave a new row unplaced and then ignore the + // row's own state, so `unsizedRevision` does not reach it. Read + // here, the revision is a dependency of the view whose body makes + // this one: when it changes, that body runs again, this value + // differs, and SwiftUI updates and measures the row. + self.unplacedHeightRevision = stream.unplacedHeight.revision } /// Loads the pictures for images in the text; see @@ -196,7 +204,12 @@ public struct TranscriptText: UIViewRepresentable { // only if the update reads that state. _ = unsizedRevision let revision = $unsizedRevision - view.onHeightChangeWhileUnsized = { revision.wrappedValue &+= 1 } + let unplacedHeight: UnplacedHeightSignal? + if case .stream(let stream) = source { unplacedHeight = stream.unplacedHeight } else { unplacedHeight = nil } + view.onHeightChangeWhileUnsized = { + revision.wrappedValue &+= 1 + unplacedHeight?.revision &+= 1 + } } public func sizeThatFits(_ proposal: ProposedViewSize, uiView: TranscriptTextView, context: Context) -> CGSize? { diff --git a/Tests/TranscriptKitTests/StreamTests.swift b/Tests/TranscriptKitTests/StreamTests.swift index 65778bf..0d8ae37 100644 --- a/Tests/TranscriptKitTests/StreamTests.swift +++ b/Tests/TranscriptKitTests/StreamTests.swift @@ -1,4 +1,5 @@ import Foundation +import Observation import Testing @testable import TranscriptKit @@ -241,3 +242,26 @@ private func content(_ document: TranscriptDocument) -> [String] { #expect(engine.backgroundLayoutCount <= frames, "\(engine.backgroundLayoutCount) layouts in \(elapsed) s") #expect(engine.backgroundLayoutCount < submissions / 2) } + +private final class Flag: @unchecked Sendable { + private let lock = NSLock() + private var raised = false + var isRaised: Bool { lock.withLock { raised } } + func raise() { lock.withLock { raised = true } } +} + +/// `TranscriptText(stream:)` reads the revision where it is made, so the view +/// that makes it must hear when the revision changes. +@MainActor +@Test func theUnplacedHeightRevisionIsObservable() { + let stream = TranscriptStream() + let changed = Flag() + let before = withObservationTracking { + stream.unplacedHeight.revision + } onChange: { + changed.raise() + } + stream.unplacedHeight.revision &+= 1 + #expect(changed.isRaised) + #expect(stream.unplacedHeight.revision == before &+ 1) +}