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
27 changes: 27 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Format

on:
push:
branches:
- main

concurrency:
group: format-${{ github.ref }}
cancel-in-progress: true

jobs:
swift_format:
name: swift-format
runs-on: macos-15
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Select Xcode 16.2
run: sudo xcode-select -s /Applications/Xcode_16.2.app
- name: Format
run: make format
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Run swift-format
branch: 'main'
13 changes: 11 additions & 2 deletions Sources/Sharing/Shared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,25 @@ 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
_ body: @escaping @Sendable (Value) -> Member
) -> SharedReader<Member> {
SharedReader(self).read(body)
}

@available(
*,
deprecated,
message: "Use dynamic member lookup instead ('$shared.member', not '$shared.read(\\.member)')"
)
public func read<Member>(_ keyPath: KeyPath<Value, Member>) -> SharedReader<Member> {
self[dynamicMember: keyPath]
}

/// Returns a shared reference to the resulting value of a given key path.
///
/// You don't call this subscript directly. Instead, Swift calls it for you when you access a
Expand Down
26 changes: 13 additions & 13 deletions Sources/Sharing/SharedKeys/AppStorageKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -559,20 +559,20 @@
}
}

extension UserDefaults {
public static var inMemory: UserDefaults {
let suiteName: String
// NB: Due to a bug in iOS 16 and lower, UserDefaults does not observe changes when using
// file-based suites. Go back to using temporary directory always when we drop iOS 16
// support.
if #unavailable(iOS 17, macOS 14, tvOS 17, watchOS 10, visionOS 1) {
suiteName = "co.pointfree.Sharing.\(UUID().uuidString)"
} else {
suiteName = "\(NSTemporaryDirectory())co.pointfree.Sharing.\(UUID().uuidString)"
}
return UserDefaults(suiteName: suiteName)!
extension UserDefaults {
public static var inMemory: UserDefaults {
let suiteName: String
// NB: Due to a bug in iOS 16 and lower, UserDefaults does not observe changes when using
// file-based suites. Go back to using temporary directory always when we drop iOS 16
// support.
if #unavailable(iOS 17, macOS 14, tvOS 17, watchOS 10, visionOS 1) {
suiteName = "co.pointfree.Sharing.\(UUID().uuidString)"
} else {
suiteName = "\(NSTemporaryDirectory())co.pointfree.Sharing.\(UUID().uuidString)"
}
return UserDefaults(suiteName: suiteName)!
}
}
}

private enum AppStorageKeyFormatWarningEnabledKey: DependencyKey {
static let liveValue = true
Expand Down
11 changes: 10 additions & 1 deletion Sources/Sharing/SharedReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ 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.
Expand All @@ -170,6 +170,15 @@ public struct SharedReader<Value> {
return open(reference)
}

@available(
*,
deprecated,
message: "Use dynamic member lookup instead ('$shared.member', not '$shared.read(\\.member)')"
)
public func read<Member>(_ keyPath: KeyPath<Value, Member>) -> SharedReader<Member> {
self[dynamicMember: keyPath]
}

/// Returns a read-only shared reference to the resulting value of a given key path.
///
/// You don't call this subscript directly. Instead, Swift calls it for you when you access a
Expand Down
2 changes: 1 addition & 1 deletion Tests/SharingTests/ContinuationTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Testing
import Sharing
import Testing

@Suite struct ContinuationTests {
@Test func dontResumeLoadContinuation() async throws {
Expand Down
4 changes: 2 additions & 2 deletions Tests/SharingTests/EquatableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct EquatableTests {
#expect(lhs == rhs)
#expect($lhs == $rhs)
}

@Test func mapReader() {
@Shared(value: 0) var base: Int
@SharedReader var lhs: Int
Expand All @@ -41,7 +41,7 @@ struct EquatableTests {
_rhs = $base.read { $0 * 3 }
#expect(lhs == rhs)
#expect($lhs == $rhs)

$base.withLock { $0 += 1 }
#expect(lhs != rhs)
#expect($lhs != $rhs)
Expand Down
6 changes: 3 additions & 3 deletions Tests/SharingTests/ErrorThrowingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import Testing
@SharedReader(Key()) var count = 0
#expect($count.loadError != nil)
}

@Test func userInitiatedLoadError() async {
struct LoadError: Error {}
struct Key: Hashable, Sendable, SharedReaderKey {
Expand All @@ -62,9 +62,9 @@ import Testing
SharedSubscription {}
}
}

@SharedReader(Key()) var value = 0

await #expect(throws: LoadError.self) {
try await $value.load()
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/SharingTests/InMemoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ import Testing
@Shared(.inMemory("nestedOptionalCount")) var nestedOptionalCount: Int?? = .some(.none)
#expect(nestedOptionalCount == .some(.none))

@SharedReader(.inMemory("nestedOptionalCountReader")) var nestedOptionalCountReader: Int?? = .some(.none)
@SharedReader(.inMemory("nestedOptionalCountReader")) var nestedOptionalCountReader: Int?? =
.some(.none)
#expect(nestedOptionalCountReader == .some(.none))
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SharingTests/SharedChangeTrackerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ import Testing
$count.withLock { $0 = 1 }

#expect($count == $count)
#expect(counts.withLock(\.self) == [0 ,1])
#expect(counts.withLock(\.self) == [0, 1])
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SharingTests/SharedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import Testing

#expect(id == 42)
}

@Test func mapReader() {
@Shared(value: 0) var count
@SharedReader var isZero: Bool
Expand Down