Skip to content

Commit 6650693

Browse files
committed
feat(TaskOperation): allow customizing priority of task
1 parent f28ee66 commit 6650693

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

Sources/AsyncObjects/TaskOperation.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public final class TaskOperation<R: Sendable>: Operation, AsyncObject,
1818
/// synchronize data access and modifications.
1919
@usableFromInline
2020
let locker: Locker
21+
/// The priority of top-level task executed.
22+
///
23+
/// In case of `nil` priority from `Task.currentPriority`
24+
/// of task that starts the operation used.
25+
public let priority: TaskPriority?
2126
/// The asynchronous action to perform as part of the operation..
2227
private let underlyingAction: @Sendable () async throws -> R
2328
/// The top-level task that executes asynchronous action provided
@@ -92,14 +97,19 @@ public final class TaskOperation<R: Sendable>: Operation, AsyncObject,
9297
/// - Parameters:
9398
/// - locker: The locker to use to synchronize property read and mutations.
9499
/// New lock object is created in case none provided.
100+
/// - priority: The priority of the task that operation executes.
101+
/// Pass `nil` to use the priority from `Task.currentPriority`
102+
/// of task that starts the operation.
95103
/// - operation: The asynchronous operation to execute.
96104
///
97105
/// - Returns: The newly created asynchronous operation.
98106
public init(
99107
synchronizedWith locker: Locker = .init(),
108+
priority: TaskPriority? = nil,
100109
operation: @escaping @Sendable () async throws -> R
101110
) {
102111
self.locker = locker
112+
self.priority = priority
103113
self.underlyingAction = operation
104114
super.init()
105115
}
@@ -126,7 +136,7 @@ public final class TaskOperation<R: Sendable>: Operation, AsyncObject,
126136
/// as part of a new top-level task on behalf of the current actor.
127137
public override func main() {
128138
guard isExecuting, execTask == nil else { return }
129-
execTask = Task { [weak self] in
139+
execTask = Task(priority: priority) { [weak self] in
130140
guard
131141
let action = self?.underlyingAction
132142
else { throw CancellationError() }

0 commit comments

Comments
 (0)