Skip to content

Commit ed80df1

Browse files
committed
Fix documentation
1 parent c3760f1 commit ed80df1

2 files changed

Lines changed: 57 additions & 10 deletions

File tree

Sources/Processed/Process/Process.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ import SwiftUI
8686
///
8787
/// ```swift
8888
/// @Process var saving
89-
///
9089
/// /* ... */
91-
///
9290
/// $saving.run {
9391
/// try await save()
9492
/// }
@@ -103,12 +101,14 @@ import SwiftUI
103101
/// }
104102
///
105103
/// @Process<ProcessKind> var action
106-
///
107104
/// /* ... */
108-
///
109-
/// $saving.run(.saving) {
105+
/// $action.run(.saving) {
110106
/// try await save()
111107
/// }
108+
/// $action.run(.deleting) {
109+
/// try await delete()
110+
/// }
111+
/// ```
112112
@MainActor public var projectedValue: Binding {
113113
.init(state: $state, task: $task)
114114
}
@@ -159,9 +159,7 @@ extension Process {
159159
///
160160
/// ```swift
161161
/// @Process var saving
162-
///
163162
/// /* ... */
164-
///
165163
/// $saving.run {
166164
/// try await save()
167165
/// }
@@ -176,12 +174,14 @@ extension Process {
176174
/// }
177175
///
178176
/// @Process<ProcessKind> var action
179-
///
180177
/// /* ... */
181-
///
182-
/// $saving.run(.saving) {
178+
/// $action.run(.saving) {
183179
/// try await save()
184180
/// }
181+
/// $action.run(.deleting) {
182+
/// try await delete()
183+
/// }
184+
/// ```
185185
public var projectedValue: Binding {
186186
self
187187
}

Sources/Processed/Process/ProcessSupport.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,53 @@
2222

2323
import SwiftUI
2424

25+
/// A protocol that adds support for automatic state and `Task` management for ``Processed/ProcessState`` to the class.
26+
///
27+
/// The provided method takes care of creating a `Task` to run the process in, cancel any previous `Task` instances and setting
28+
/// the appropriate loading states on the ``Processed/ProcessState`` that you specify.
29+
///
30+
/// To start a process, call one of the `run` methods on self with a key path to a ``Processed/ProcessState``
31+
/// property.
32+
///
33+
/// ```swift
34+
/// @MainActor final class ViewModel: ObservableObject, ProcessSupport {
35+
/// @Published var numbers: ProcessState = .idle
36+
///
37+
/// func save() {
38+
/// run {
39+
/// return try await save()
40+
/// }
41+
/// }
42+
/// }
43+
/// ```
44+
///
45+
/// You can run different processes on the same state by providing a process identifier:
46+
///
47+
/// ```swift
48+
/// @MainActor final class ViewModel: ObservableObject, ProcessSupport {
49+
/// enum ProcessKind: Equatable {
50+
/// case saving
51+
/// case deleting
52+
/// }
53+
///
54+
/// @Published var action: ProcessState<ProcessKind> = .idle
55+
///
56+
/// func save() {
57+
/// run(\.action, as: .saving) {
58+
/// return try await save()
59+
/// }
60+
/// }
61+
///
62+
/// func delete() {
63+
/// run(\.action, as: .delete) {
64+
/// return try await delete()
65+
/// }
66+
/// }
67+
/// }
68+
/// ```
69+
///
70+
/// - Note: This is only meant to be used in classes.
71+
/// If you want to do this inside a SwiftUI view, please refer to the ``Processed/Process`` property wrapper.
2572
public protocol ProcessSupport: AnyObject {
2673

2774
/// Cancels the task of an ongoing process.

0 commit comments

Comments
 (0)