Skip to content

Commit d003aac

Browse files
committed
Restrict @Loadable and @process to @mainactor
They are only ever going to be used from SwifsUI Views, so this makes the code a lot more convenient. Also, it's easy to await a Task.detached if needed
1 parent 09e84b7 commit d003aac

2 files changed

Lines changed: 26 additions & 26 deletions

File tree

Sources/Processed/Loadable/Loadable.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import SwiftUI
7171
///
7272
/// It is okay to modify the state manually, instead of having it managed by a process like ``Processed/Loadable/Binding/load(silently:priority:block:)-4w49u``.
7373
/// However, doing so will cancel any ongoing task first, to prevent data races.
74-
public var wrappedValue: LoadableState<Value> {
74+
@MainActor public var wrappedValue: LoadableState<Value> {
7575
get { state }
7676
nonmutating set {
7777
cancel()
@@ -95,7 +95,7 @@ import SwiftUI
9595
/// try await fetchNumbers()
9696
/// }
9797
/// ```
98-
public var projectedValue: Binding {
98+
@MainActor public var projectedValue: Binding {
9999
.init(state: $state, task: $task)
100100
}
101101

@@ -210,10 +210,10 @@ extension Loadable {
210210
/// The block exposes a `yield` closure you can call to continuously update the resource loading state over time.
211211
///
212212
/// - Returns: The task that runs the asynchronous loading process. You don't have to store it, but you can.
213-
@discardableResult public func load(
213+
@MainActor @discardableResult public func load(
214214
silently runSilently: Bool = false,
215215
priority: TaskPriority? = nil,
216-
block: @escaping (_ yield: (_ state: LoadableState<Value>) -> Void) async throws -> Void
216+
block: @MainActor @escaping (_ yield: (_ state: LoadableState<Value>) -> Void) async throws -> Void
217217
) -> Task<Void, Never> {
218218
cancel()
219219
setLoadingStateIfNeeded(runSilently: runSilently)
@@ -253,9 +253,9 @@ extension Loadable {
253253
/// - runSilently: If `true`, the state will not be set to `.loading` initially.
254254
/// - block: The asynchronous block to run.
255255
/// The block exposes a `yield` closure you can call to continuously update the resource loading state over time.
256-
public func load(
256+
@MainActor public func load(
257257
silently runSilently: Bool = false,
258-
block: @escaping (_ yield: (_ state: LoadableState<Value>) -> Void) async throws -> Void
258+
block: @MainActor @escaping (_ yield: (_ state: LoadableState<Value>) -> Void) async throws -> Void
259259
) async {
260260
cancel()
261261
setLoadingStateIfNeeded(runSilently: runSilently)
@@ -289,10 +289,10 @@ extension Loadable {
289289
/// - block: The asynchronous block to run.
290290
///
291291
/// - Returns: The task that runs the asynchronous loading process. You don't have to store it, but you can.
292-
@discardableResult public func load(
292+
@MainActor @discardableResult public func load(
293293
silently runSilently: Bool = false,
294294
priority: TaskPriority? = nil,
295-
block: @escaping () async throws -> Value
295+
block: @MainActor @escaping () async throws -> Value
296296
) -> Task<Void, Never> {
297297
cancel()
298298
setLoadingStateIfNeeded(runSilently: runSilently)
@@ -311,8 +311,8 @@ extension Loadable {
311311
self.task = task
312312
return task
313313
}
314-
315-
/// Starts a resource loading process in the current asynchronous context,
314+
315+
/// Starts a resource loading process in the current asynchronous context,
316316
/// waiting for a return value or thrown error from the `block` closure,
317317
/// while setting the ``Processed/LoadableState`` accordingly.
318318
///
@@ -327,9 +327,9 @@ extension Loadable {
327327
/// - Parameters:
328328
/// - runSilently: If `true`, the state will not be set to `.loading` initially.
329329
/// - block: The asynchronous block to run.
330-
public func load(
330+
@MainActor public func load(
331331
silently runSilently: Bool = false,
332-
block: @escaping () async throws -> Value
332+
block: @MainActor @escaping () async throws -> Value
333333
) async {
334334
cancel()
335335
setLoadingStateIfNeeded(runSilently: runSilently)

Sources/Processed/Process/Process.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import SwiftUI
2929
@SwiftUI.State private var task: Task<Void, Never>?
3030

3131
/// The current state of the process.
32-
public var wrappedValue: ProcessState<ProcessID> {
32+
@MainActor public var wrappedValue: ProcessState<ProcessID> {
3333
get { state }
3434
nonmutating set {
3535
cancel()
@@ -38,7 +38,7 @@ import SwiftUI
3838
}
3939

4040
/// Provides a binding for controlling the process.
41-
public var projectedValue: Binding {
41+
@MainActor public var projectedValue: Binding {
4242
.init(state: $state, task: $task)
4343
}
4444

@@ -66,7 +66,7 @@ extension Process {
6666
@SwiftUI.Binding var state: ProcessState<ProcessID>
6767
@SwiftUI.Binding var task: Task<Void, Never>?
6868

69-
public var wrappedValue: ProcessState<ProcessID> {
69+
@MainActor public var wrappedValue: ProcessState<ProcessID> {
7070
get { state }
7171
nonmutating set {
7272
cancel()
@@ -75,7 +75,7 @@ extension Process {
7575
}
7676

7777
/// Provides a binding for controlling the process.
78-
public var projectedValue: Binding {
78+
@MainActor public var projectedValue: Binding {
7979
self
8080
}
8181

@@ -126,11 +126,11 @@ extension Process {
126126
/// - priority: The priority of the task.
127127
/// - block: The asynchronous block of code to execute.
128128
/// - Returns: The task representing the process execution.
129-
@discardableResult public func run(
129+
@MainActor @discardableResult public func run(
130130
_ process: ProcessID,
131131
silently runSilently: Bool = false,
132132
priority: TaskPriority? = nil,
133-
block: @escaping () async throws -> Void
133+
block: @MainActor @escaping () async throws -> Void
134134
) -> Task<Void, Never> {
135135
cancel()
136136
setLoadingStateIfNeeded(runSilently: runSilently, process: process)
@@ -141,36 +141,36 @@ extension Process {
141141
return task
142142
}
143143

144-
public func run(
144+
@MainActor public func run(
145145
_ process: ProcessID,
146146
silently runSilently: Bool = false,
147-
block: @escaping () async throws -> Void
147+
block: @MainActor @escaping () async throws -> Void
148148
) async {
149149
cancel()
150150
setLoadingStateIfNeeded(runSilently: runSilently, process: process)
151151
await runTaskBody(process: process, runSilently: runSilently, block: block)
152152
}
153153

154-
public func run(
154+
@MainActor public func run(
155155
silently runSilently: Bool = false,
156-
block: @escaping () async throws -> Void
156+
block: @MainActor @escaping () async throws -> Void
157157
) where ProcessID == SingleProcess {
158158
run(.init(), silently: runSilently, block: block)
159159
}
160160

161-
public func run(
161+
@MainActor public func run(
162162
silently runSilently: Bool = false,
163-
block: @escaping () async throws -> Void
163+
block: @MainActor @escaping () async throws -> Void
164164
) async where ProcessID == SingleProcess {
165165
await run(.init(), silently: runSilently, block: block)
166166
}
167167

168168
// MARK: - Internal
169169

170-
private func runTaskBody(
170+
@MainActor private func runTaskBody(
171171
process: ProcessID,
172172
runSilently: Bool,
173-
block: @escaping () async throws -> Void
173+
block: @MainActor @escaping () async throws -> Void
174174
) async {
175175
do {
176176
try await block()

0 commit comments

Comments
 (0)