From 4f65462bf72343222ae20ca569a92e42f246b697 Mon Sep 17 00:00:00 2001 From: todd-weidler <68913725+todd-weidler@users.noreply.github.com> Date: Sat, 7 Dec 2024 12:03:40 -0500 Subject: [PATCH] Added support for skipping withMutation in shared state when identity is unchanged --- Package.swift | 2 +- Package@swift-6.0.swift | 2 +- .../Observation/ObservableState.swift | 11 +++++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Package.swift b/Package.swift index 6f531d62f48f..2ec29cc9d114 100644 --- a/Package.swift +++ b/Package.swift @@ -28,7 +28,7 @@ let package = Package( .package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.2.0"), .package(url: "https://github.com/pointfreeco/swift-navigation", from: "2.2.2"), .package(url: "https://github.com/pointfreeco/swift-perception", from: "1.3.4"), - .package(url: "https://github.com/pointfreeco/swift-sharing", "0.1.2"..<"2.0.0"), + .package(url: "https://github.com/pointfreeco/swift-sharing", revision: "c1b1f5095a8b36aa22cd470815b373e40ec51d5b"), .package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.3.0"), .package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.0.0"), .package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"601.0.0"), diff --git a/Package@swift-6.0.swift b/Package@swift-6.0.swift index bbccd895f980..e30f8d33e29f 100644 --- a/Package@swift-6.0.swift +++ b/Package@swift-6.0.swift @@ -28,7 +28,7 @@ let package = Package( .package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.2.0"), .package(url: "https://github.com/pointfreeco/swift-navigation", from: "2.2.2"), .package(url: "https://github.com/pointfreeco/swift-perception", from: "1.3.4"), - .package(url: "https://github.com/pointfreeco/swift-sharing", "0.1.2"..<"2.0.0"), + .package(url: "https://github.com/pointfreeco/swift-sharing", revision: "c1b1f5095a8b36aa22cd470815b373e40ec51d5b"), .package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.3.0"), .package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.0.0"), .package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"601.0.0"), diff --git a/Sources/ComposableArchitecture/Observation/ObservableState.swift b/Sources/ComposableArchitecture/Observation/ObservableState.swift index 32a3675b89cb..c6ab2982c51b 100644 --- a/Sources/ComposableArchitecture/Observation/ObservableState.swift +++ b/Sources/ComposableArchitecture/Observation/ObservableState.swift @@ -1,4 +1,5 @@ import Foundation +import Sharing /// A type that emits notifications to observers when underlying data changes. /// @@ -7,12 +8,12 @@ import Foundation /// functionality to the type. Instead, always use the ``ObservableState()`` macro when adding /// observation support to a type. #if !os(visionOS) - public protocol ObservableState: Perceptible { + public protocol ObservableState: Perceptible, _MutationSkippableSharedValue { var _$id: ObservableStateID { get } mutating func _$willModify() } #else - public protocol ObservableState: Observable { + public protocol ObservableState: Observable, _MutationSkippableSharedValue { var _$id: ObservableStateID { get } mutating func _$willModify() } @@ -193,3 +194,9 @@ public func _$willModify(_: inout T) {} public func _$willModify(_ value: inout T) { value._$willModify() } + +public extension ObservableState { + func shouldCallWithMutation(newValue: Self) -> Bool { + !_$isIdentityEqual(self, newValue) + } +}