Skip to content

Commit

Permalink
Shared: Simplify internal reference protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Oct 16, 2024
1 parent fc5cbee commit 160bebb
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 25 deletions.
8 changes: 6 additions & 2 deletions Sources/ComposableArchitecture/SharedState/Reference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ protocol Reference<Value>: AnyObject, CustomStringConvertible, Sendable {
associatedtype Value: Sendable
var value: Value { get set }

func access()
func withMutation<T>(_ mutation: () throws -> T) rethrows -> T
#if canImport(Combine)
var publisher: AnyPublisher<Value, Never> { get }
#endif
}

extension Reference {
func touch() {
value = value
}
}

extension Reference {
var valueType: Any.Type {
Value.self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,6 @@ final class ValueReference<Value, Persistence: PersistenceReaderKey<Value>>: Ref
}
}
}
func access() {
_$perceptionRegistrar.access(self, keyPath: \.value)
}
func withMutation<T>(_ mutation: () throws -> T) rethrows -> T {
self._$perceptionRegistrar.willSet(self, keyPath: \.value)
defer { self._$perceptionRegistrar.didSet(self, keyPath: \.value) }
return try mutation()
}
var description: String {
"Shared<\(Value.self)>@\(self.fileID):\(self.line)"
}
Expand Down
10 changes: 3 additions & 7 deletions Sources/ComposableArchitecture/SharedState/Shared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,10 @@ public struct Shared<Value: Sendable>: Sendable {
///
/// See <doc:SharingState#Deriving-shared-state> for more details.
public var projectedValue: Self {
get {
reference.access()
return self
}
get { self }
set {
reference.withMutation {
self = newValue
}
reference.touch()
self = newValue
}
}

Expand Down
10 changes: 3 additions & 7 deletions Sources/ComposableArchitecture/SharedState/SharedReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,10 @@ public struct SharedReader<Value: Sendable> {

/// A projection of the read-only shared value that returns a shared reference.
public var projectedValue: Self {
get {
reference.access()
return self
}
get { self }
set {
reference.withMutation {
self = newValue
}
reference.touch()
self = newValue
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/ComposableArchitectureTests/SharedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ final class SharedTests: XCTestCase {
_count = Shared(0)
let countDidChange = self.expectation(description: "countDidChange")
withPerceptionTracking {
_ = $count
_ = $count.wrappedValue
} onChange: {
countDidChange.fulfill()
}
Expand Down

0 comments on commit 160bebb

Please sign in to comment.