Skip to content

Commit 1a740ce

Browse files
committed
Move code to helper
1 parent 7b96fd2 commit 1a740ce

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

EditorExtension/AcceptSuggestionCommand.swift

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,3 @@ class AcceptSuggestionCommand: NSObject, XCSourceEditorCommand, CommandType {
3131
}
3232
}
3333

34-
/// https://gist.github.com/swhitty/9be89dfe97dbb55c6ef0f916273bbb97
35-
extension Task where Failure == Error {
36-
// Start a new Task with a timeout. If the timeout expires before the operation is
37-
// completed then the task is cancelled and an error is thrown.
38-
init(
39-
priority: TaskPriority? = nil,
40-
timeout: TimeInterval,
41-
operation: @escaping @Sendable () async throws -> Success
42-
) {
43-
self = Task(priority: priority) {
44-
try await withThrowingTaskGroup(of: Success.self) { group -> Success in
45-
group.addTask(operation: operation)
46-
group.addTask {
47-
try await _Concurrency.Task.sleep(nanoseconds: UInt64(timeout * 1_000_000_000))
48-
throw TimeoutError()
49-
}
50-
guard let success = try await group.next() else {
51-
throw _Concurrency.CancellationError()
52-
}
53-
group.cancelAll()
54-
return success
55-
}
56-
}
57-
}
58-
}
59-
60-
private struct TimeoutError: LocalizedError {
61-
var errorDescription: String? = "Task timed out before completion"
62-
}

EditorExtension/Helpers.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,33 @@ extension EditorContent {
6666
)
6767
}
6868
}
69+
70+
/// https://gist.github.com/swhitty/9be89dfe97dbb55c6ef0f916273bbb97
71+
extension Task where Failure == Error {
72+
// Start a new Task with a timeout. If the timeout expires before the operation is
73+
// completed then the task is cancelled and an error is thrown.
74+
init(
75+
priority: TaskPriority? = nil,
76+
timeout: TimeInterval,
77+
operation: @escaping @Sendable () async throws -> Success
78+
) {
79+
self = Task(priority: priority) {
80+
try await withThrowingTaskGroup(of: Success.self) { group -> Success in
81+
group.addTask(operation: operation)
82+
group.addTask {
83+
try await _Concurrency.Task.sleep(nanoseconds: UInt64(timeout * 1_000_000_000))
84+
throw TimeoutError()
85+
}
86+
guard let success = try await group.next() else {
87+
throw _Concurrency.CancellationError()
88+
}
89+
group.cancelAll()
90+
return success
91+
}
92+
}
93+
}
94+
}
95+
96+
private struct TimeoutError: LocalizedError {
97+
var errorDescription: String? = "Task timed out before completion"
98+
}

0 commit comments

Comments
 (0)