Skip to content

Commit 3be98e5

Browse files
committed
Simplify
1 parent e358db5 commit 3be98e5

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

Sources/AsyncQueue/ActorQueue.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,29 @@ public final class ActorQueue: Sendable {
4242

4343
streamTask = Task.detached(priority: priority) {
4444
actor ActorExecutor {
45-
func seriallyExecute(_ task: @escaping @Sendable () async -> Void) async {
45+
func suspendUntilStarted(_ task: @escaping @Sendable () async -> Void) async {
4646
let semaphore = Semaphore()
47-
Task {
48-
await self.execute(task, afterSignaling: semaphore)
49-
}
50-
// Wait for the task to start.
47+
executeWithoutWaiting(task, afterSignaling: semaphore)
48+
// Suspend the calling code until our enqueued task starts.
5149
await semaphore.wait()
5250
}
5351

54-
private func execute(
52+
private func executeWithoutWaiting(
5553
_ task: @escaping @Sendable () async -> Void,
56-
afterSignaling semaphore: Semaphore
57-
) async {
58-
// Signal that the task has started.
59-
await semaphore.signal()
60-
await task()
54+
afterSignaling semaphore: Semaphore)
55+
{
56+
// Utilize the serial (but not FIFO) Actor context to execute the task without requiring the calling method to wait for the task to complete.
57+
Task {
58+
// Now that we're back within the serial Actor context, signal that the task has started.
59+
await semaphore.signal()
60+
await task()
61+
}
6162
}
6263
}
6364

6465
let executor = ActorExecutor()
6566
for await task in taskStream {
66-
await executor.seriallyExecute(task)
67+
await executor.suspendUntilStarted(task)
6768
}
6869
}
6970
}

Sources/AsyncQueue/Semaphore.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ actor Semaphore {
2929
return
3030
}
3131

32-
await withCheckedContinuation { continuation in
32+
await withUnsafeContinuation { continuation in
3333
continuations.append(continuation)
3434
}
3535
}
@@ -52,6 +52,6 @@ actor Semaphore {
5252
count < 0
5353
}
5454

55-
private var continuations = [CheckedContinuation<Void, Never>]()
55+
private var continuations = [UnsafeContinuation<Void, Never>]()
5656
private var count = 0
5757
}

0 commit comments

Comments
 (0)