-
Notifications
You must be signed in to change notification settings - Fork 27
refactor!: Introduce Synchronized primitive and adapt classes to its use #974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d51d885
aadb0b9
6f80ef1
80561f8
76b1783
66fd950
c2f720f
528ca50
a20206c
cc2be70
6c2b61e
afdb118
e4d4195
57fd683
6c80e2d
590a0ea
6c628b2
f2b1e33
68b9f95
850eb74
66672db
fd2b22b
ccef5d4
c345ada
999976c
7332a54
b03581b
869a746
845f249
c5612c1
97c2700
6c17fc3
f8e6f93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import Foundation | ||
|
|
||
| extension OperationQueue { | ||
| func addAsyncOperation(asyncBlock: @escaping () async -> Void) { | ||
| addOperation(AsyncOperation(asyncBlock: asyncBlock)) | ||
| } | ||
| } | ||
|
|
||
| public final class AsyncOperation: Operation, @unchecked Sendable { | ||
| private let activeTask: Synchronized<Task<Void, Never>?> = .init(initial: nil) | ||
|
|
||
| public let asyncBlock: () async -> Void | ||
|
|
||
| public init(asyncBlock: @escaping () async -> Void) { | ||
| self.asyncBlock = asyncBlock | ||
| } | ||
|
|
||
| // Only required when you want to manually start an operation | ||
| // Ignored when an operation is added to a queue. | ||
| override public var isAsynchronous: Bool { true } | ||
|
|
||
| // State is accessed and modified in a thread safe and KVO compliant way. | ||
| private let _isExecuting: Synchronized<Bool> = .init(initial: false) | ||
| override public private(set) var isExecuting: Bool { | ||
| get { | ||
| _isExecuting.wrappedValue | ||
| } | ||
| set { | ||
| willChangeValue(forKey: "isExecuting") | ||
| _isExecuting.wrappedValue = newValue | ||
| didChangeValue(forKey: "isExecuting") | ||
| } | ||
| } | ||
|
|
||
| private let _isFinished: Synchronized<Bool> = .init(initial: false) | ||
| override public private(set) var isFinished: Bool { | ||
| get { | ||
| _isFinished.wrappedValue | ||
| } | ||
| set { | ||
| willChangeValue(forKey: "isFinished") | ||
| _isFinished.wrappedValue = newValue | ||
| didChangeValue(forKey: "isFinished") | ||
| } | ||
| } | ||
|
|
||
| override public func start() { | ||
| guard !isCancelled else { | ||
| finish() | ||
| return | ||
| } | ||
| isFinished = false | ||
| isExecuting = true | ||
| main() | ||
| } | ||
|
|
||
| override public func main() { | ||
| activeTask.wrappedValue = Task { [weak self] in | ||
| guard let self, !isCancelled else { return } | ||
| await asyncBlock() | ||
| finish() | ||
| } | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AsyncOperation cancellation leaves operation in incomplete stateWhen |
||
|
|
||
| override public func cancel() { | ||
| activeTask.mutating { value in | ||
| value?.cancel() | ||
| value = nil | ||
| } | ||
| super.cancel() | ||
| } | ||
|
|
||
| func finish() { | ||
| isExecuting = false | ||
| isFinished = true | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.