Skip to content
Draft
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
35 changes: 35 additions & 0 deletions Sources/SkipModel/BodyInvalidation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2026 Skip
// SPDX-License-Identifier: MPL-2.0
#if SKIP
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf

// Support for native body observation tracking (SkipFuseUI). While a body is evaluated, the
// native side calls `trackBodyInvalidation()`, which subscribes the current recompose scope to a
// fresh state and returns an id. When a tracked property changes, it calls `invalidateBody(id:)`.
//
// The native side holds only the id, never a JNI reference: Observation cannot cancel a pending
// tracking, so its closures may outlive the composition by a long time. States are held weakly;
// Compose keeps one alive only while a scope still reads it, so a stale id is a no-op.

private let bodyInvalidationStates = java.util.concurrent.ConcurrentHashMap<Int, java.lang.ref.WeakReference<MutableState<Int>>>()
private let bodyInvalidationNextID = java.util.concurrent.atomic.AtomicInteger(0)

public func trackBodyInvalidation() -> Int {
let state = mutableStateOf(0)
let _ = state.value
let id = bodyInvalidationNextID.incrementAndGet()
bodyInvalidationStates.put(id, java.lang.ref.WeakReference(state))
// Periodic O(n) sweep of collected states; use a ReferenceQueue if it shows up in profiles.
if id % 1024 == 0 {
bodyInvalidationStates.values.removeIf { $0.get() == nil }
}
return id
}

public func invalidateBody(id: Int) {
if let state = bodyInvalidationStates.remove(id)?.get() {
state.value += 1
}
}
#endif
13 changes: 13 additions & 0 deletions Tests/SkipModelTests/SkipModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ import XCTest
final class SkipModelTests: XCTestCase {
var published = -1

/// Body invalidation ids are unique, and invalidating an id that already fired or never
/// existed is a no-op: native observation callbacks can outlive the composition.
func testBodyInvalidationIDs() {
#if SKIP
let first = trackBodyInvalidation()
let second = trackBodyInvalidation()
XCTAssertNotEqual(first, second)
invalidateBody(id: first)
invalidateBody(id: first)
invalidateBody(id: Int.max)
#endif
}

func testObjectWillChange() {
let model = Model()
var observed = -1
Expand Down
Loading