Skip to content

Commit 3eec302

Browse files
committed
Add new product "ProcessedUtility"
- Added new property "loadedBinding" - Added .constant initializers
1 parent af5058c commit 3eec302

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

Package.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ let package = Package(
88
products: [
99
.library(
1010
name: "Processed",
11-
targets: ["Processed"]),
11+
targets: ["Processed"]
12+
),
13+
.library(
14+
name: "ProcessedUtility",
15+
targets: ["Processed", "ProcessedUtility"]
16+
),
1217
],
1318
targets: [
1419
.target(
15-
name: "Processed"),
20+
name: "Processed"
21+
),
22+
.target(name: "ProcessedUtility", dependencies: ["Processed"]),
1623
.testTarget(
1724
name: "ProcessedTests",
18-
dependencies: ["Processed"]),
25+
dependencies: ["Processed"]
26+
),
1927
]
2028
)

Sources/Processed/Process/Process.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ extension Process {
203203
self = binding
204204
}
205205

206+
public static func constant(_ state: ProcessState<ProcessKind>) -> Self {
207+
.init(state: .constant(state), task: .constant(nil))
208+
}
209+
206210
/// Cancels the task of an ongoing process.
207211
///
208212
/// - Note: You are responsible for cooperating with the task cancellation within the loading closures.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// By Dennis Müller
2+
3+
import OSLog
4+
import Processed
5+
import SwiftUI
6+
7+
public extension Loadable.Binding {
8+
/// Returns a binding to the associated value stored in the underlying `Loadable.loaded(value)` case. If that value
9+
/// does not exist (for example, if the loadable is not in the `.loaded` state), `nil` is returned instead.
10+
///
11+
/// You can pass this to a view that only wants to modify the _loaded data_ of the loadable without needing to change
12+
/// its state.
13+
///
14+
/// - Important: It is your responsibility to make sure the loadable stays in the `.loaded` state for the entirety
15+
/// of this binding's existence. Any other state will cause the `set` closure to be a no-op.
16+
var loadedBinding: Binding<Value>? {
17+
guard let data = state.data else { return nil }
18+
return Binding<Value> {
19+
data
20+
} set: { newValue in
21+
guard case .loaded = state else {
22+
return
23+
}
24+
state = .loaded(newValue)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)