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
2 changes: 1 addition & 1 deletion Sources/SwiftNavigation/NSObject+Observe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
public func observe(
_ apply: @escaping @MainActor @Sendable (_ transaction: UITransaction) -> Void
) -> ObserveToken {
let token = SwiftNavigation.observe { transaction in
let token = SwiftNavigation._observe { transaction in
MainActor._assumeIsolated {
apply(transaction)
}
Expand Down
33 changes: 8 additions & 25 deletions Sources/SwiftNavigation/Observe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ import ConcurrencyExtras
/// - Returns: A token that keeps the subscription alive. Observation is cancelled when the token
/// is deallocated.
public func observe(
isolation: (any Actor)? = #isolation,
@_inheritActorContext _ apply: @escaping @Sendable () -> Void
@_inheritActorContext _ apply: @escaping @isolated(any) @Sendable () -> Void
) -> ObserveToken {
observe(isolation: isolation) { _ in apply() }
observe { _ in Result(catching: apply).get() }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a trick similarly employed by the Observations type to convert an @isolated(any) to a synchronous function. I'm not sure if there is a more legit way to assume the isolation of a closure to perform it synchronously.

Copy link
Contributor

@maximkrouk maximkrouk Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fixing conflicts for #286 and looks like this implementation gives a bunch of warnings on Xcode 26 beta 4 🤔

Screenshot 2025-09-24 at 12 58 04 PM

Converting @isolated(any) function of type '@isolated(any) @Sendable (UITransaction) -> Void' to synchronous function type '@Sendable (UITransaction) -> Void' is not allowed; this will be an error in a future Swift language mode 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maximkrouk These warnings are fine. They even exist in Swift's Observation framework. Luckily they don't show up downstream.

}

/// Tracks access to properties of an observable model.
Expand All @@ -72,37 +71,21 @@ import ConcurrencyExtras
/// - Returns: A token that keeps the subscription alive. Observation is cancelled when the token
/// is deallocated.
public func observe(
isolation: (any Actor)? = #isolation,
@_inheritActorContext _ apply: @escaping @Sendable (_ transaction: UITransaction) -> Void
@_inheritActorContext
_ apply: @escaping @isolated(any) @Sendable (_ transaction: UITransaction) -> Void
) -> ObserveToken {
let actor = ActorProxy(base: isolation)
return observe(
_observe(
apply,
task: { transaction, operation in
Task {
await actor.perform {
operation()
}
await operation()
}
}
)
}
#endif

private actor ActorProxy {
let base: (any Actor)?
init(base: (any Actor)?) {
self.base = base
}
nonisolated var unownedExecutor: UnownedSerialExecutor {
(base ?? MainActor.shared).unownedExecutor
}
func perform(_ operation: @Sendable () -> Void) {
operation()
}
}

func observe(
func _observe(
_ apply: @escaping @Sendable (_ transaction: UITransaction) -> Void,
task: @escaping @Sendable (
_ transaction: UITransaction, _ operation: @escaping @Sendable () -> Void
Expand Down Expand Up @@ -141,7 +124,7 @@ func observe(
private func onChange(
_ apply: @escaping @Sendable (_ transaction: UITransaction) -> Void,
task: @escaping @Sendable (
_ transaction: UITransaction, _ operation: @escaping @Sendable () -> Void
_ transaction: UITransaction, _ operation: @escaping @isolated(any) @Sendable () -> Void
) -> Void
) {
withPerceptionTracking {
Expand Down