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
50 changes: 50 additions & 0 deletions Sources/Sharing/Internal/Reference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,56 @@ where Base: MutableReference, Path: WritableKeyPath<Base.Value, Value> {
}
}

final class _ReadClosureReference<Base: Reference, Value>:
Reference,
Observable
{
private let base: Base
private let body: @Sendable (Base.Value) -> Value

init(base: Base, body: @escaping @Sendable (Base.Value) -> Value) {
self.base = base
self.body = body
}

var id: ObjectIdentifier {
base.id
}

var isLoading: Bool {
base.isLoading
}

var loadError: (any Error)? {
base.loadError
}

var wrappedValue: Value {
body(base.wrappedValue)
}

func load() async throws {
try await base.load()
}

func touch() {
base.touch()
}

#if canImport(Combine)
var publisher: any Publisher<Value, Never> {
func open(_ publisher: some Publisher<Base.Value, Never>) -> any Publisher<Value, Never> {
publisher.map(body)
}
return open(base.publisher)
}
#endif

var description: String {
".map(\(base.description), as: \(Value.self).self)"
}
}

final class _OptionalReference<Base: Reference<Value?>, Value>:
Reference,
Observable,
Expand Down
9 changes: 9 additions & 0 deletions Sources/Sharing/Shared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ public struct Shared<Value> {
reference = newValue.reference
}
}

/// Returns a read-only shared reference to the resulting value of a given closure.
///
/// - Returns: A new read-only shared reference.
public func read<Member>(
_ body: @escaping @Sendable(Value) -> Member
) -> SharedReader<Member> {
SharedReader(self).read(body)
}

/// Returns a shared reference to the resulting value of a given key path.
///
Expand Down
14 changes: 14 additions & 0 deletions Sources/Sharing/SharedReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ public struct SharedReader<Value> {
reference = newValue.reference
}
}

/// Returns a read-only shared reference to the resulting value of a given closure.
///
/// - Returns: A new shared reader.
public func read<Member>(
_ body: @escaping @Sendable (Value) -> Member
) -> SharedReader<Member> {
func open(_ reference: some Reference<Value>) -> SharedReader<Member> {
SharedReader<Member>(
reference: _ReadClosureReference(base: reference, body: body)
)
}
return open(reference)
}

/// Returns a read-only shared reference to the resulting value of a given key path.
///
Expand Down
14 changes: 14 additions & 0 deletions Tests/SharingTests/EquatableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,18 @@ struct EquatableTests {
#expect(lhs == rhs)
#expect($lhs == $rhs)
}

@Test func mapReader() {
@Shared(value: 0) var base: Int
@SharedReader var lhs: Int
@SharedReader var rhs: Int
_lhs = $base.read { $0 * 2 }
_rhs = $base.read { $0 * 3 }
#expect(lhs == rhs)
#expect($lhs == $rhs)

$base.withLock { $0 += 1 }
#expect(lhs != rhs)
#expect($lhs != $rhs)
}
}
16 changes: 16 additions & 0 deletions Tests/SharingTests/SharedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ import Testing

#expect(id == 42)
}

@Test func mapReader() {
@Shared(value: 0) var count
@SharedReader var isZero: Bool
_isZero = $count.read { $0 == 0 }

#expect(isZero)

$count.withLock { $0 += 1 }

#expect(!isZero)

$count = Shared(value: 0)

#expect(!isZero)
}

@Test func optional() throws {
@Shared(value: nil) var wrappedCount: Int?
Expand Down