Skip to content

Commit b8424b6

Browse files
authored
Minor Refactoring (#43)
* Add more strict conformance for base protocol types * Use opaque parameter * Use shorthands for unwrapping optinals * Use closure type inference
1 parent cfafe05 commit b8424b6

16 files changed

+49
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ class MockAPIClient: APIClientProtocol {
11571157
var response: Book?
11581158

11591159
func fetchBook(isbn: String) async throws -> Book {
1160-
guard let response = response else {
1160+
guard let response else {
11611161
throw URLError(.unknown)
11621162
}
11631163
return response

Sources/Atoms/Context/AtomContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public protocol AtomContext {
7777
/// ```
7878
///
7979
/// - Parameter atom: An atom that associates the value.
80-
func reset<Node: Atom>(_ atom: Node)
80+
func reset(_ atom: some Atom)
8181
}
8282

8383
public extension AtomContext {

Sources/Atoms/Context/AtomTestContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public struct AtomTestContext: AtomWatchableContext {
167167
/// ```
168168
///
169169
/// - Parameter atom: An atom that associates the value.
170-
public func reset<Node: Atom>(_ atom: Node) {
170+
public func reset(_ atom: some Atom) {
171171
state.store.reset(atom)
172172
}
173173

Sources/Atoms/Context/AtomTransactionContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public struct AtomTransactionContext<Coordinator>: AtomWatchableContext {
104104
///
105105
/// - Parameter atom: An atom that associates the value.
106106
@inlinable
107-
public func reset<Node: Atom>(_ atom: Node) {
107+
public func reset(_ atom: some Atom) {
108108
_store.reset(atom)
109109
}
110110

Sources/Atoms/Context/AtomViewContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public struct AtomViewContext: AtomWatchableContext {
103103
///
104104
/// - Parameter atom: An atom that associates the value.
105105
@inlinable
106-
public func reset<Node: Atom>(_ atom: Node) {
106+
public func reset(_ atom: some Atom) {
107107
_store.reset(atom)
108108
}
109109

Sources/Atoms/Core/AtomCache.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
@MainActor
2-
internal protocol AtomCacheBase {
2+
internal protocol AtomCacheProtocol: CustomStringConvertible {
3+
associatedtype Node: Atom
4+
5+
var atom: Node { get set }
6+
var value: Node.Loader.Value? { get set }
37
var shouldKeepAlive: Bool { get }
48

59
func reset(with store: StoreContext)
610
}
711

8-
internal struct AtomCache<Node: Atom>: AtomCacheBase, CustomStringConvertible {
12+
internal extension AtomCacheProtocol {
13+
var description: String {
14+
value.map { "\($0)" } ?? "nil"
15+
}
16+
}
17+
18+
internal struct AtomCache<Node: Atom>: AtomCacheProtocol {
919
var atom: Node
1020
var value: Node.Loader.Value?
1121

1222
var shouldKeepAlive: Bool {
1323
Node.shouldKeepAlive
1424
}
1525

16-
var description: String {
17-
value.map { "\($0)" } ?? "nil"
18-
}
19-
2026
func reset(with store: StoreContext) {
2127
store.reset(atom)
2228
}

Sources/Atoms/Core/AtomState.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
@MainActor
2-
internal protocol AtomStateBase {
3-
var transaction: Transaction? { get nonmutating set }
2+
internal protocol AtomStateProtocol: AnyObject {
3+
associatedtype Coordinator
4+
5+
var coordinator: Coordinator { get }
6+
var transaction: Transaction? { get set }
47
}
58

6-
internal final class AtomState<Coordinator>: AtomStateBase {
9+
internal final class AtomState<Coordinator>: AtomStateProtocol {
710
let coordinator: Coordinator
811
var transaction: Transaction?
912

Sources/Atoms/Core/Loader/AsyncSequenceAtomLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public struct AsyncSequenceAtomLoader<Node: AsyncSequenceAtom>: RefreshableAtomL
4444
/// and returns a final value.
4545
public func refresh(context: Context) async -> Value {
4646
let sequence = context.transaction(atom.sequence)
47-
let task = Task { () -> Value in
47+
let task = Task {
4848
var phase = Value.suspending
4949

5050
do {

Sources/Atoms/Core/Loader/ObservableObjectAtomLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public struct ObservableObjectAtomLoader<Node: ObservableObjectAtom>: AtomLoader
2424
/// Handles updates or cancellation of the passed value.
2525
public func handle(context: Context, with object: Value) -> Value {
2626
let cancellable = object.objectWillChange.sink { [weak object] _ in
27-
guard let object = object else {
27+
guard let object else {
2828
return
2929
}
3030

Sources/Atoms/Core/Loader/PublisherAtomLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public struct PublisherAtomLoader<Node: PublisherAtom>: RefreshableAtomLoader {
3838
/// Refreshes and awaits until the asynchronous is finished and returns a final value.
3939
public func refresh(context: Context) async -> Value {
4040
let results = context.transaction(atom.publisher).results
41-
let task = Task { () -> Value in
41+
let task = Task {
4242
var phase = Value.suspending
4343

4444
for await result in results {

0 commit comments

Comments
 (0)