Skip to content

Add contentSettled() to wait for a view's final content - #31

Merged
servercimen merged 9 commits into
mainfrom
feature/content-settled
Nov 24, 2026
Merged

servercimen merged 9 commits into
mainfrom
feature/content-settled

Conversation

@servercimen

Copy link
Copy Markdown
Owner

Part 1 of a stack of 13 pull requests. The base is main.

Each part holds only its own change. The merge commits in a part join the branch below it, with the conflicts between the changes resolved. To merge the stack, start at the top: merge part 13 into its base, then part 12, and go down to this part, which you merge into main last. Each merge then has no conflicts.

On this branch, swift test passes (195 tests), and the iOS build passes.

Summary

A snapshot test or an app must know when a TranscriptTextView has nothing pending. Layouts of later updates, images, formulas and code colors come back on later turns of the main run loop. Plain text can also show on a later turn. This change adds contentSettled(), which returns when the view shows its final content.

  • TranscriptTextView.contentSettled() is the new public API. Before it returns, the view lays out if it must and draws the tiles that wait for the next commit. While a stream goes on, it returns at the first moment when nothing is pending. It does not wait for fades and tag animations.
  • The bookkeeping is internal and has no UIKit code, so the macOS tests can drive it. PendingWork counts the layout drains, the fade settling, the results that the view has not taken, and the picture, formula and highlight work. TranscriptStream gets isIdle and whenIdle. ContentSettledWaiters lets the tasks go when the count is zero, the stream is idle and the view has drawn.
  • The waiters look at the stream before and after the layout, because a layout callback such as onSelectionChange can append to the stream. A cancelled task returns at once. A flush() call does not lay out or draw inside the call.
  • The README has a new section, "Waiting for the final content".

Tests

  • swift test on macOS: 195 tests pass. The new tests are in ContentSettledTests.swift and StreamTests.swift.
  • A flaky test is fixed: aFlushDoesNotSettleInsideTheCall no longer depends on the frame timer.
  • xcodebuild build -scheme TranscriptKit -destination 'generic/platform=iOS' CODE_SIGNING_ALLOWED=NO: passes.

Checked on an iPhone 12 mini

A check screen in a local test app ran on main first (5 runs) and showed the problem. Then it ran on this branch (6 Release runs and 1 Debug run). Stand-ins: the check calls the API from code. Its renderers have fixed delays: 200 ms for the formula, 100 ms for the code colors and 300 ms for the picture. The snapshots come from layer.render(in:).

  • Formula, code colors and picture:
    • Main: after one Task.yield, the snapshot shows the TeX source, the code in the text color and the alt text. It has 0 red code pixels and 0 picture pixels, and the formula area is 0% dark.
    • Branch: contentSettled() returns 319 to 341 ms after the set, 6 to 10 ms after the picture. The snapshot has 504 red code pixels and 7200 picture pixels, and the formula area is 92% dark. 0 pixels change 1 s later.
  • Streamed text:
    • Main: the view is behind the appended text at 39 to 40 of 40 checks.
    • Branch: the view length is equal to the stream length at 40 of 40 returns. The median wait is 3.9 to 7.2 ms, and the longest is 21 ms.
  • Branch only: a cancel, nil results, a stream with a fade-in, a late size and a stream that goes on all pass, with no timeout. The Debug run has no PendingWork assertion.

Not checked

  • The check of this branch ran before the last source commit, "Check the stream again after the layout in contentSettled()". A later run of the same check screen on a build that holds this commit and the other open changes passed. No check on the phone makes a layout callback append to the stream; a macOS test covers that case.
  • Real formula, highlight and image code.

🤖 Generated with Claude Code

servercimen and others added 8 commits November 23, 2026 17:06
The layout engine and the image store now count their work in a
PendingWork counter. A drain counts from when it is scheduled until it
ends. A result counts until the view takes it from the mailbox. A
picture load, a formula render and a code highlight count until they
end, also when they return nothing. The relayout on main after a
picture, a formula or code colors arrive counts too.

The count reaches zero only when no work is left. A later commit uses
it to tell when the view shows its final content.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TranscriptStream now has an internal isIdle and whenIdle. The stream is
idle when no appended text waits for a frame and no parse runs. By then
the attached views have every version. Text that the parser holds back
makes no new version, so the views get no update for it. whenIdle lets
a view know when such a parse ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A snapshot or a test needs to know when the view has nothing pending.
Layouts of later updates, images, formulas and code colors come back on
later turns of the main run loop, so a harness that does not wait for
them misses them.

TranscriptTextView.contentSettled() returns when no layout, stream
parse, image load, formula or code colors are pending, the last layout
is on screen and its tiles are drawn. The tiles take back the tail
first, so that they hold all of the text. While a stream that has not
finished goes on, it returns at the first moment when nothing is
pending. A cancelled task returns at once.

The bookkeeping is in ContentSettledWaiters, which has no UIKit code,
so the macOS tests drive it with a layout engine and a stream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TranscriptStream.flush() runs its idle actions inline. The waiters
therefore laid out and drew the view inside the caller's flush() call,
which can be in the middle of a SwiftUI update or a layout pass. The
stream's idle callback now hops to main before it checks again, as the
work counter's callback does.

PendingWork.end() without a begin() now stops only debug builds. A
release build keeps the count at zero, so a counting mistake does not
crash an app that never waits.

New tests cover a flush while a task waits, work that the layout on
main starts, the fade settling on the layout queue, and a stream that
changes while a task waits. Two tests no longer sleep before they check
a state that only the test can change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A load, a formula or a highlight that never ends holds back
contentSettled() until the task is cancelled. A view with no width lays
out nothing, so it settles at once with no content. The doc comment and
the README now say both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The test aFlushDoesNotSettleInsideTheCall failed in some full test
runs. It started a second task for the wait and yielded until the wait
registered. The wait registered at once. On a busy machine, the test
task did not run again on main before the frame, 1/60 s after the
append. The frame parsed the text that the parser holds back, so the
wait settled before the test called flush().

The test task now calls wait() itself. The test puts a block in the
main queue before the append. The block records the waiter count,
calls flush(), and records the draw count. The append starts the frame
timer after the block is in the queue, so the block always runs first.
The test checks the same three facts: one wait is registered before
the flush, the flush does not draw, and the wait resumes after the
flush.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A callback of the layout, such as onSelectionChange, can append to the
stream. An append starts no work, so the check after the layout saw no
work and let the waiting tasks go before the text was parsed. Now the
check looks at the stream before and after the layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A style or a renderer that the app sets before the document, on a view
that has a width, makes the view lay out the empty text first. The
document then shows on a later turn of the main run loop. The README
said that plain text is laid out at once, which is not true in that
case, and the example of the section is that case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Xcode 26.6 rejects the weak capture of self in the cancel handler of
contentSettled(): it says that sending self risks data races. The
waiters are main actor isolated and so Sendable, and the handler runs
one time, so a strong capture is safe and builds with each Xcode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@servercimen
servercimen added this pull request to stack #45 November 24, 2026 21:01
@servercimen
servercimen merged commit 2ea3b0a into main Nov 24, 2026
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant