Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions Sources/AsyncQueue/CancellableQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,20 @@ extension Task {
/// to the synchronous operation.
///
/// - Parameters:
/// - name: Human readable name of the task.
/// - priority: The priority of the task.
/// Pass `nil` to use the priority from `Task.currentPriority`.
/// - actorQueue: The queue on which to enqueue the task.
/// - operation: The operation to perform.
@discardableResult
public init<ActorType: Actor>(
name: String? = nil,
priority: TaskPriority? = nil,
on actorQueue: CancellableQueue<ActorQueue<ActorType>>,
operation: @Sendable @escaping (isolated ActorType) async -> Success,
) where Failure == Never {
let identifier = UUID()
self.init(priority: priority, on: actorQueue.underlyingQueue, operation: {
self.init(name: name, priority: priority, on: actorQueue.underlyingQueue, operation: {
defer {
actorQueue.taskIdentifierToCancelMap.writeAsync {
$0[identifier] = nil
Expand Down Expand Up @@ -108,18 +110,20 @@ extension Task {
/// to the synchronous operation.
///
/// - Parameters:
/// - name: Human readable name of the task.
/// - priority: The priority of the task.
/// Pass `nil` to use the priority from `Task.currentPriority`.
/// - actorQueue: The queue on which to enqueue the task.
/// - operation: The operation to perform.
@discardableResult
public init<ActorType: Actor>(
name: String? = nil,
priority: TaskPriority? = nil,
on actorQueue: CancellableQueue<ActorQueue<ActorType>>,
operation: @escaping @Sendable (isolated ActorType) async throws -> Success,
) where Failure == any Error {
let identifier = UUID()
self.init(priority: priority, on: actorQueue.underlyingQueue, operation: {
self.init(name: name, priority: priority, on: actorQueue.underlyingQueue, operation: {
defer {
actorQueue.taskIdentifierToCancelMap.writeAsync {
$0[identifier] = nil
Expand Down Expand Up @@ -148,18 +152,20 @@ extension Task {
/// to the synchronous operation.
///
/// - Parameters:
/// - name: Human readable name of the task.
/// - priority: The priority of the task.
/// Pass `nil` to use the priority from `Task.currentPriority`.
/// - actorQueue: The queue on which to enqueue the task.
/// - operation: The operation to perform.
@discardableResult
public init(
name: String? = nil,
priority: TaskPriority? = nil,
on actorQueue: CancellableQueue<ActorQueue<MainActor>>,
operation: @MainActor @escaping () async -> Success,
) where Failure == Never {
let identifier = UUID()
self.init(priority: priority, on: actorQueue.underlyingQueue, operation: {
self.init(name: name, priority: priority, on: actorQueue.underlyingQueue, operation: {
defer {
actorQueue.taskIdentifierToCancelMap.writeAsync {
$0[identifier] = nil
Expand Down Expand Up @@ -188,18 +194,20 @@ extension Task {
/// to the synchronous operation.
///
/// - Parameters:
/// - name: Human readable name of the task.
/// - priority: The priority of the task.
/// Pass `nil` to use the priority from `Task.currentPriority`.
/// - actorQueue: The queue on which to enqueue the task.
/// - operation: The operation to perform.
@discardableResult
public init(
name: String? = nil,
priority: TaskPriority? = nil,
on actorQueue: CancellableQueue<ActorQueue<MainActor>>,
operation: @escaping @MainActor () async throws -> Success,
) where Failure == any Error {
let identifier = UUID()
self.init(priority: priority, on: actorQueue.underlyingQueue, operation: {
self.init(name: name, priority: priority, on: actorQueue.underlyingQueue, operation: {
defer {
actorQueue.taskIdentifierToCancelMap.writeAsync {
$0[identifier] = nil
Expand Down Expand Up @@ -228,15 +236,17 @@ extension Task {
/// to the synchronous operation.
///
/// - Parameters:
/// - name: Human readable name of the task.
/// - fifoQueue: The queue on which to enqueue the task.
/// - operation: The operation to perform.
@discardableResult
public init(
name: String? = nil,
on fifoQueue: CancellableQueue<FIFOQueue>,
@_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async -> Success,
) where Failure == Never {
let identifier = UUID()
self.init(on: fifoQueue.underlyingQueue, operation: {
self.init(name: name, on: fifoQueue.underlyingQueue, operation: {
defer {
fifoQueue.taskIdentifierToCancelMap.writeAsync {
$0[identifier] = nil
Expand Down Expand Up @@ -265,15 +275,17 @@ extension Task {
/// to the synchronous operation.
///
/// - Parameters:
/// - name: Human readable name of the task.
/// - fifoQueue: The queue on which to enqueue the task.
/// - operation: The operation to perform.
@discardableResult
public init(
name: String? = nil,
on fifoQueue: CancellableQueue<FIFOQueue>,
@_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async throws -> Success,
) where Failure == any Error {
let identifier = UUID()
self.init(on: fifoQueue.underlyingQueue, operation: {
self.init(name: name, on: fifoQueue.underlyingQueue, operation: {
defer {
fifoQueue.taskIdentifierToCancelMap.writeAsync {
$0[identifier] = nil
Expand Down Expand Up @@ -302,20 +314,22 @@ extension Task {
/// to the synchronous operation.
///
/// - Parameters:
/// - name: Human readable name of the task.
/// - priority: The priority of the task.
/// Pass `nil` to use the priority from `Task.currentPriority`.
/// - fifoQueue: The queue on which to enqueue the task.
/// - isolatedActor: The actor to which the operation is isolated.
/// - operation: The operation to perform.
@discardableResult
public init<ActorType: Actor>(
name: String? = nil,
priority: TaskPriority? = nil,
on fifoQueue: CancellableQueue<FIFOQueue>,
isolatedTo isolatedActor: ActorType,
operation: @Sendable @escaping (isolated ActorType) async -> Success,
) where Failure == Never {
let identifier = UUID()
self.init(priority: priority, on: fifoQueue.underlyingQueue, isolatedTo: isolatedActor, operation: {
self.init(name: name, priority: priority, on: fifoQueue.underlyingQueue, isolatedTo: isolatedActor, operation: {
defer {
fifoQueue.taskIdentifierToCancelMap.writeAsync {
$0[identifier] = nil
Expand Down Expand Up @@ -344,20 +358,22 @@ extension Task {
/// to the synchronous operation.
///
/// - Parameters:
/// - name: Human readable name of the task.
/// - priority: The priority of the task.
/// Pass `nil` to use the priority from `Task.currentPriority`.
/// - fifoQueue: The queue on which to enqueue the task.
/// - isolatedActor: The actor to which the operation is isolated.
/// - operation: The operation to perform.
@discardableResult
public init<ActorType: Actor>(
name: String? = nil,
priority: TaskPriority? = nil,
on fifoQueue: CancellableQueue<FIFOQueue>,
isolatedTo isolatedActor: ActorType,
operation: @Sendable @escaping (isolated ActorType) async throws -> Success,
) where Failure == any Error {
let identifier = UUID()
self.init(priority: priority, on: fifoQueue.underlyingQueue, isolatedTo: isolatedActor, operation: {
self.init(name: name, priority: priority, on: fifoQueue.underlyingQueue, isolatedTo: isolatedActor, operation: {
defer {
fifoQueue.taskIdentifierToCancelMap.writeAsync {
$0[identifier] = nil
Expand Down