Skip to content

Commit 923d507

Browse files
authored
observe should take @isolated(any) functions (#306)
1 parent 6b7f44d commit 923d507

File tree

2 files changed

+9
-26
lines changed

2 files changed

+9
-26
lines changed

Sources/SwiftNavigation/NSObject+Observe.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
public func observe(
122122
_ apply: @escaping @MainActor @Sendable (_ transaction: UITransaction) -> Void
123123
) -> ObserveToken {
124-
let token = SwiftNavigation.observe { transaction in
124+
let token = SwiftNavigation._observe { transaction in
125125
MainActor._assumeIsolated {
126126
apply(transaction)
127127
}

Sources/SwiftNavigation/Observe.swift

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ import ConcurrencyExtras
5656
/// - Returns: A token that keeps the subscription alive. Observation is cancelled when the token
5757
/// is deallocated.
5858
public func observe(
59-
isolation: (any Actor)? = #isolation,
60-
@_inheritActorContext _ apply: @escaping @Sendable () -> Void
59+
@_inheritActorContext _ apply: @escaping @isolated(any) @Sendable () -> Void
6160
) -> ObserveToken {
62-
observe(isolation: isolation) { _ in apply() }
61+
observe { _ in Result(catching: apply).get() }
6362
}
6463

6564
/// Tracks access to properties of an observable model.
@@ -72,37 +71,21 @@ import ConcurrencyExtras
7271
/// - Returns: A token that keeps the subscription alive. Observation is cancelled when the token
7372
/// is deallocated.
7473
public func observe(
75-
isolation: (any Actor)? = #isolation,
76-
@_inheritActorContext _ apply: @escaping @Sendable (_ transaction: UITransaction) -> Void
74+
@_inheritActorContext
75+
_ apply: @escaping @isolated(any) @Sendable (_ transaction: UITransaction) -> Void
7776
) -> ObserveToken {
78-
let actor = ActorProxy(base: isolation)
79-
return observe(
77+
_observe(
8078
apply,
8179
task: { transaction, operation in
8280
Task {
83-
await actor.perform {
84-
operation()
85-
}
81+
await operation()
8682
}
8783
}
8884
)
8985
}
9086
#endif
9187

92-
private actor ActorProxy {
93-
let base: (any Actor)?
94-
init(base: (any Actor)?) {
95-
self.base = base
96-
}
97-
nonisolated var unownedExecutor: UnownedSerialExecutor {
98-
(base ?? MainActor.shared).unownedExecutor
99-
}
100-
func perform(_ operation: @Sendable () -> Void) {
101-
operation()
102-
}
103-
}
104-
105-
func observe(
88+
func _observe(
10689
_ apply: @escaping @Sendable (_ transaction: UITransaction) -> Void,
10790
task: @escaping @Sendable (
10891
_ transaction: UITransaction, _ operation: @escaping @Sendable () -> Void
@@ -141,7 +124,7 @@ func observe(
141124
private func onChange(
142125
_ apply: @escaping @Sendable (_ transaction: UITransaction) -> Void,
143126
task: @escaping @Sendable (
144-
_ transaction: UITransaction, _ operation: @escaping @Sendable () -> Void
127+
_ transaction: UITransaction, _ operation: @escaping @isolated(any) @Sendable () -> Void
145128
) -> Void
146129
) {
147130
withPerceptionTracking {

0 commit comments

Comments
 (0)