-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Alexander-Ignition/swift_concurrency
Swift Concurrency
- Loading branch information
Showing
26 changed files
with
1,051 additions
and
485 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Foundation | ||
|
||
extension Process { | ||
var async: AsyncProcess { | ||
AsyncProcess(process: self) | ||
} | ||
} | ||
|
||
final class AsyncProcess { | ||
private let _process: Process | ||
private let _lock = NSLock() | ||
|
||
fileprivate init(process: Process) { | ||
self._process = process | ||
} | ||
|
||
/// Runs the process with the current environment. | ||
func run() async throws { | ||
try await withTaskCancellationHandler { | ||
_terminate() | ||
} operation: { | ||
try Task.checkCancellation() // can be canceled before running | ||
try await _run() | ||
} | ||
} | ||
|
||
private func _run() async throws { | ||
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in | ||
var runningError: Error? | ||
_lock.lock() | ||
_process.terminationHandler = { _ in | ||
continuation.resume() | ||
} | ||
do { | ||
try _process.run() // waiting for the `terminationHandler` call | ||
} catch { | ||
// `terminationHandler` is not called if if an error occurred | ||
runningError = error | ||
} | ||
_lock.unlock() | ||
|
||
if let runningError = runningError { | ||
continuation.resume(throwing: runningError) | ||
} | ||
} | ||
} | ||
|
||
private func _terminate() { | ||
_lock.lock() | ||
if _process.isRunning { // can be canceled without starting | ||
_process.terminate() // crash if not running | ||
} | ||
_lock.unlock() | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Foundation | ||
|
||
final class FileHandleReader { | ||
fileprivate(set) var data = Data() | ||
|
||
fileprivate init() {} | ||
} | ||
|
||
extension FileHandle { | ||
func reader() -> FileHandleReader { | ||
assert(self.readabilityHandler == nil) | ||
|
||
let reader = FileHandleReader() | ||
|
||
self.readabilityHandler = { fileHandle in | ||
// invoke on serial queue | ||
let data = fileHandle.availableData | ||
|
||
if data.isEmpty { | ||
// stop | ||
fileHandle.readabilityHandler = nil | ||
} else { | ||
reader.data.append(data) | ||
} | ||
} | ||
return reader | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.