Skip to content

Commit

Permalink
Avoid excessive view computation with @Bindable (#123)
Browse files Browse the repository at this point in the history
Because our `@Bindable` holds onto a reference, this reference is probed
by SwiftUI to determine if a view body should be recomputed, and since
the creation of the view is going to create a whole new reference, it is
invalidating the view and causing it to re-render.

Instead, lets treat these references as equal if the objects they wrap
are equal. This doesn't make things completely 1:1 with SwiftUI, as
there is an initial invalidation in the parent, but a single extra
invalidation seems like a good improvement.

Fixes #122.
  • Loading branch information
stephencelis authored Jan 30, 2025
1 parent 9e9ec51 commit d833bce
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/PerceptionCore/Bindable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@
self.object = object
}
}

extension Observer: Equatable where Object: AnyObject {
static func == (lhs: Observer, rhs: Observer) -> Bool {
lhs.object === rhs.object
}
}
#endif

0 comments on commit d833bce

Please sign in to comment.