Docs native S1a: attaching tells the model nothing during the view update (#170, 3)
Fable's finding 3. attach() runs inside makeNSView/makeUIView and draws at once, so the first frame is final; it deferred didRender to the next turn, but the image-source report it made while drawing assigned the model's observed imageSources inside the update, for any document with an image (the 0.1.7/0.1.8 rule). The report now goes with didRender: synchronously after an ordinary flush, on the next turn after an attach. SurfaceLifecycleTests.testAttachingTellsTheModelOnlyAfterTheUpdate failed before (imageSources reported inside the update) and passes now.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+33
−3
native/apple/Sources/DocsKit/DocumentSurface.swift+7 −3native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift+26 −0
diff --git a/native/apple/Sources/DocsKit/DocumentSurface.swift b/native/apple/Sources/DocsKit/DocumentSurface.swift| index 8fec62d..e6a339a 100644 |
| --- a/native/apple/Sources/DocsKit/DocumentSurface.swift |
| +++ b/native/apple/Sources/DocsKit/DocumentSurface.swift |
| @@ -223,7 +223,7 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| applying = true |
| if entries.isEmpty { drawWhole(newest.projection, into: text, storage: storage) } |
| else { drawChanged(newest.projection, storage: storage, textOnly: !advancing) } |
| - report(sources: sourceCounts.keys.sorted()) |
| + let sources = sourceCounts.keys.sorted() |
| let length = storage.length |
| let range = NSRange(location: min(selected.location, length), length: min(selected.length, max(0, length - selected.location))) |
| if selectedRange != range { Self.select(range, in: text) } |
| @@ -231,9 +231,13 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| appliedRevision = newest.revision |
| generation = newest.generation |
| if !Self.hasMarkedText(text) { applyTypingFont() } |
| + // The model hears of the draw (the generation drawn, the image |
| + // sources) now, or, inside SwiftUI's update (attach), on the next |
| + // turn: no observed state is assigned during a view update. |
| let rendered = newest.generation |
| - if notifyLater { Task { @MainActor [weak self] in self?.hooks.didRender(rendered) } } |
| - else { hooks.didRender(rendered) } |
| + if notifyLater { |
| + Task { @MainActor [weak self] in self?.report(sources: sources); self?.hooks.didRender(rendered) } |
| + } else { report(sources: sources); hooks.didRender(rendered) } |
| } |
| /// Open, or after `reset`: the one whole render, and on iOS the one |
diff --git a/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift b/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift| index 9a6db75..41b25af 100644 |
| --- a/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift |
| @@ -28,4 +28,30 @@ import XCTest |
| state.publish(changed, generation: 1, dirty: ["p"]) |
| SurfaceFixture.assertMatchesWholeRender(Keyboard.storage(of: second), changed, "a publication after the second attach") |
| } |
| + |
| + /// The text view is attached inside SwiftUI's update (makeNSView / |
| + /// makeUIView). It is drawn there, so the first frame is final, but the |
| + /// model hears nothing until the next turn: no observed state is |
| + /// assigned during a view update (the 0.1.7/0.1.8 rule), not the |
| + /// rendered generation and not the document's image sources. |
| + func testAttachingTellsTheModelOnlyAfterTheUpdate() async throws { |
| + let image = CRDTNode(id: "i", type: "paragraph", children: [CRDTNode(id: "img", type: "image", attrs: ["src": "https://example.invalid/a.png", "alt": "A"])]) |
| + let document = SurfaceFixture.projection([SurfaceFixture.paragraph("p", "Before."), image]) |
| + let surface = DocumentSurface() |
| + var inUpdate = false |
| + var duringUpdate: [String] = [], after: [String] = [] |
| + var hooks = DocumentSurface.Hooks() |
| + hooks.didRender = { _ in (inUpdate ? { duringUpdate.append("didRender") } : { after.append("didRender") })() } |
| + hooks.imageSourcesChanged = { _ in (inUpdate ? { duringUpdate.append("imageSources") } : { after.append("imageSources") })() } |
| + surface.hooks = hooks |
| + surface.apply(DocumentSurface.Publication(generation: 1, projection: document, revision: 1, dirty: nil)) |
| + let text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| + inUpdate = true |
| + surface.attach(text) |
| + inUpdate = false |
| + XCTAssertEqual(duringUpdate, [], "nothing reaches the model inside the update") |
| + XCTAssertTrue(Keyboard.storage(of: text).string.hasPrefix("Before."), "drawn at once") |
| + for _ in 0..<20 where after.count < 2 { try await Task.sleep(for: .milliseconds(5)) } |
| + XCTAssertEqual(Set(after), ["didRender", "imageSources"], "the model hears both on the next turn") |
| + } |
| } |