Last active
February 7, 2025 13:35
-
-
Save Joannis/516cf0489da6aed957b363030830249e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @available(macOS 14, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | |
| final class DispatchSerialExecutor: SerialExecutor { | |
| let queue: DispatchQueue | |
| init(queue: DispatchQueue) { | |
| self.queue = queue | |
| } | |
| init() { | |
| self.queue = DispatchQueue( | |
| label: "software.unbeatable.dispatch-serial-executor", | |
| attributes: .concurrent | |
| ) | |
| } | |
| static let global = DispatchSerialExecutor(queue: .global()) | |
| func enqueue(_ job: UnownedJob) { | |
| queue.async { [job] in | |
| job.runSynchronously(on: self.asUnownedSerialExecutor()) | |
| } | |
| } | |
| func asUnownedSerialExecutor() -> UnownedSerialExecutor { | |
| UnownedSerialExecutor(ordinary: self) | |
| } | |
| } | |
| @globalActor fileprivate actor GlobalQueueActor { | |
| static let shared = GlobalQueueActor() | |
| nonisolated private let executor = DispatchSerialExecutor.global | |
| nonisolated var unownedExecutor: UnownedSerialExecutor { | |
| executor.asUnownedSerialExecutor() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment