Skip to content
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions Sources/TranscriptKit/Stream/TranscriptStream.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import Observation
#if canImport(UIKit) && !os(watchOS)
import QuartzCore
import UIKit
Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion Sources/TranscriptKit/iOS/TranscriptText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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? {
Expand Down
24 changes: 24 additions & 0 deletions Tests/TranscriptKitTests/StreamTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import Observation
import Testing
@testable import TranscriptKit

Expand Down Expand Up @@ -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)
}
Loading