File tree Expand file tree Collapse file tree
Sources/Processed/Process Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 2222
2323import 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.
2572public protocol ProcessSupport : AnyObject {
2673
2774 /// Cancels the task of an ongoing process.
You can’t perform that action at this time.
0 commit comments