Skip to content

Commit 12ff573

Browse files
committed
Remove some use of MainActor
1 parent 10f2a93 commit 12ff573

File tree

7 files changed

+47
-54
lines changed

7 files changed

+47
-54
lines changed

Core/Sources/SuggestionWidget/WidgetWindowsController.swift

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,13 @@ final class WidgetWindowsController: NSObject {
1919
var currentApplicationProcessIdentifier: pid_t?
2020

2121
var cancellable: Set<AnyCancellable> = []
22-
@MainActor
2322
var observeToAppTask: Task<Void, Error>?
24-
@MainActor
2523
var observeToFocusedEditorTask: Task<Void, Error>?
2624

27-
@MainActor
2825
var updateWindowOpacityTask: Task<Void, Error>?
29-
@MainActor
3026
var lastUpdateWindowOpacityTime = Date(timeIntervalSince1970: 0)
3127

32-
@MainActor
3328
var updateWindowLocationTask: Task<Void, Error>?
34-
@MainActor
3529
var lastUpdateWindowLocationTime = Date(timeIntervalSince1970: 0)
3630

3731
deinit {
@@ -92,13 +86,13 @@ final class WidgetWindowsController: NSObject {
9286
let isChatPanelDetached = state.chatPanelState.chatPanelInASeparateWindow
9387
let hasChat = !state.chatPanelState.chatTabGroup.tabInfo.isEmpty
9488
let shouldDebounce = await MainActor.run {
95-
defer {lastUpdateWindowOpacityTime = Date() }
96-
return (!immediately &&
97-
!(Date().timeIntervalSince(lastUpdateWindowOpacityTime) > 5))
89+
defer { lastUpdateWindowOpacityTime = Date() }
90+
return !immediately &&
91+
!(Date().timeIntervalSince(lastUpdateWindowOpacityTime) > 5)
9892
}
9993
let activeApp = xcodeInspector.activeApplication
10094

101-
await updateWindowOpacityTask?.cancel()
95+
updateWindowOpacityTask?.cancel()
10296

10397
let task = Task {
10498
if shouldDebounce {
@@ -156,10 +150,8 @@ final class WidgetWindowsController: NSObject {
156150
}
157151
}
158152
}
159-
160-
await MainActor.run {
161-
updateWindowOpacityTask = task
162-
}
153+
154+
updateWindowOpacityTask = task
163155
}
164156

165157
func updateWindowLocation(
@@ -217,16 +209,16 @@ final class WidgetWindowsController: NSObject {
217209
!(now.timeIntervalSince(lastUpdateWindowLocationTime) > 5)
218210
}
219211

220-
await updateWindowLocationTask?.cancel()
212+
updateWindowLocationTask?.cancel()
221213
let interval: TimeInterval = 0.1
222214

223215
if shouldThrottle {
224-
let delay = await max(
216+
let delay = max(
225217
0,
226218
interval - now.timeIntervalSince(lastUpdateWindowLocationTime)
227219
)
228220

229-
let task = Task { @MainActor in
221+
let task = Task {
230222
try await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
231223
try Task.checkCancellation()
232224
await update()
@@ -239,9 +231,7 @@ final class WidgetWindowsController: NSObject {
239231
await update()
240232
}
241233
}
242-
await MainActor.run {
243-
lastUpdateWindowLocationTime = Date()
244-
}
234+
lastUpdateWindowLocationTime = Date()
245235
}
246236
}
247237

@@ -296,7 +286,7 @@ private extension WidgetWindowsController {
296286
if let focusedEditor = xcodeInspector.focusedEditor {
297287
await observe(to: focusedEditor)
298288
}
299-
289+
300290
let task = Task {
301291
await windows.orderFront()
302292

@@ -340,11 +330,9 @@ private extension WidgetWindowsController {
340330
}
341331
}
342332
}
343-
344-
await MainActor.run {
345-
observeToAppTask?.cancel()
346-
observeToAppTask = task
347-
}
333+
334+
observeToAppTask?.cancel()
335+
observeToAppTask = task
348336
}
349337

350338
func observe(to editor: SourceEditor) async {
@@ -373,11 +361,9 @@ private extension WidgetWindowsController {
373361
}
374362
}
375363
}
376-
377-
await MainActor.run {
378-
observeToFocusedEditorTask?.cancel()
379-
observeToFocusedEditorTask = task
380-
}
364+
365+
observeToFocusedEditorTask?.cancel()
366+
observeToFocusedEditorTask = task
381367
}
382368
}
383369

Pro

Submodule Pro updated from 39b2b44 to 6fcfa82

Tool/Sources/AXNotificationStream/AXNotificationStream.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public final class AXNotificationStream: AsyncSequence {
106106
guard let self else { return }
107107
retry += 1
108108
for name in notificationNames {
109+
await Task.yield()
109110
let e = withUnsafeMutablePointer(to: &self.continuation) { pointer in
110111
AXObserverAddNotification(
111112
observer,

Tool/Sources/XcodeInspector/Apps/XcodeAppInstanceInspector.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
130130
override init(runningApplication: NSRunningApplication) {
131131
super.init(runningApplication: runningApplication)
132132

133-
Task { @MainActor in
133+
Task { @XcodeInspectorActor in
134134
observeFocusedWindow()
135135
observeAXNotifications()
136136

@@ -142,7 +142,7 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
142142
}
143143
}
144144

145-
@MainActor
145+
@XcodeInspectorActor
146146
func refresh() {
147147
if let focusedWindow = focusedWindow as? WorkspaceXcodeWindowInspector {
148148
focusedWindow.refresh()
@@ -151,7 +151,7 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
151151
}
152152
}
153153

154-
@MainActor
154+
@XcodeInspectorActor
155155
private func observeFocusedWindow() {
156156
if let window = appElement.focusedWindow {
157157
if window.identifier == "Xcode.WorkspaceWindow" {
@@ -197,7 +197,7 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
197197
}
198198
}
199199

200-
@MainActor
200+
@XcodeInspectorActor
201201
func observeAXNotifications() {
202202
longRunningTasks.forEach { $0.cancel() }
203203
longRunningTasks = []
@@ -220,7 +220,7 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
220220
kAXUIElementDestroyedNotification
221221
)
222222

223-
let observeAXNotificationTask = Task { @MainActor [weak self] in
223+
let observeAXNotificationTask = Task { @XcodeInspectorActor [weak self] in
224224
var updateWorkspaceInfoTask: Task<Void, Error>?
225225

226226
for await notification in axNotificationStream {

Tool/Sources/XcodeInspector/SourceEditor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class SourceEditor {
6161

6262
private func observeAXNotifications() {
6363
observeAXNotificationsTask?.cancel()
64-
observeAXNotificationsTask = Task { @MainActor [weak self] in
64+
observeAXNotificationsTask = Task { @XcodeInspectorActor [weak self] in
6565
guard let self else { return }
6666
await withThrowingTaskGroup(of: Void.self) { [weak self] group in
6767
guard let self else { return }

Tool/Sources/XcodeInspector/XcodeInspector.swift

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public extension Notification.Name {
1212
static let accessibilityAPIMalfunctioning = Notification.Name("accessibilityAPIMalfunctioning")
1313
}
1414

15+
@globalActor
16+
public enum XcodeInspectorActor: GlobalActor {
17+
public actor Actor {}
18+
public static let shared = Actor()
19+
}
20+
1521
public final class XcodeInspector: ObservableObject {
1622
public static let shared = XcodeInspector()
1723

@@ -145,19 +151,19 @@ public final class XcodeInspector: ObservableObject {
145151
if let existed = xcodes.first(where: {
146152
$0.processIdentifier == app.processIdentifier && !$0.isTerminated
147153
}) {
148-
await MainActor.run {
149-
self.setActiveXcode(existed)
154+
Task { @XcodeInspectorActor in
155+
await self.setActiveXcode(existed)
150156
}
151157
} else {
152158
let new = XcodeAppInstanceInspector(runningApplication: app)
153-
await MainActor.run {
159+
Task { @XcodeInspectorActor in
154160
self.xcodes.append(new)
155-
self.setActiveXcode(new)
161+
await self.setActiveXcode(new)
156162
}
157163
}
158164
} else {
159165
let appInspector = AppInstanceInspector(runningApplication: app)
160-
await MainActor.run {
166+
Task { @XcodeInspectorActor in
161167
self.previousActiveApplication = self.activeApplication
162168
self.activeApplication = appInspector
163169
}
@@ -176,7 +182,7 @@ public final class XcodeInspector: ObservableObject {
176182
else { continue }
177183
if app.isXcode {
178184
let processIdentifier = app.processIdentifier
179-
await MainActor.run {
185+
Task { @XcodeInspectorActor in
180186
self.xcodes.removeAll {
181187
$0.processIdentifier == processIdentifier || $0.isTerminated
182188
}
@@ -207,7 +213,7 @@ public final class XcodeInspector: ObservableObject {
207213
}
208214

209215
try await Task.sleep(nanoseconds: 10_000_000_000)
210-
await MainActor.run {
216+
Task { @XcodeInspectorActor in
211217
self.checkForAccessibilityMalfunction("Timer")
212218
}
213219
}
@@ -231,15 +237,15 @@ public final class XcodeInspector: ObservableObject {
231237
}
232238

233239
public func reactivateObservationsToXcode() {
234-
Task { @MainActor in
240+
Task { @XcodeInspectorActor in
235241
if let activeXcode {
236242
setActiveXcode(activeXcode)
237243
activeXcode.observeAXNotifications()
238244
}
239245
}
240246
}
241247

242-
@MainActor
248+
@XcodeInspectorActor
243249
private func setActiveXcode(_ xcode: XcodeAppInstanceInspector) {
244250
previousActiveApplication = activeApplication
245251
activeApplication = xcode
@@ -258,7 +264,7 @@ public final class XcodeInspector: ObservableObject {
258264
activeWorkspaceURL = xcode.workspaceURL
259265
focusedWindow = xcode.focusedWindow
260266

261-
let setFocusedElement = { @MainActor [weak self] in
267+
let setFocusedElement = { @XcodeInspectorActor [weak self] in
262268
guard let self else { return }
263269
focusedElement = xcode.appElement.focusedElement
264270
if let editorElement = focusedElement, editorElement.isSourceEditor {
@@ -279,7 +285,7 @@ public final class XcodeInspector: ObservableObject {
279285
}
280286

281287
setFocusedElement()
282-
let focusedElementChanged = Task { @MainActor in
288+
let focusedElementChanged = Task { @XcodeInspectorActor in
283289
for await notification in xcode.axNotifications {
284290
if notification.kind == .focusedUIElementChanged {
285291
try Task.checkCancellation()
@@ -293,7 +299,7 @@ public final class XcodeInspector: ObservableObject {
293299
if UserDefaults.shared
294300
.value(for: \.restartXcodeInspectorIfAccessibilityAPIIsMalfunctioning)
295301
{
296-
let malfunctionCheck = Task { @MainActor [weak self] in
302+
let malfunctionCheck = Task { @XcodeInspectorActor [weak self] in
297303
if #available(macOS 13.0, *) {
298304
let notifications = xcode.axNotifications.filter {
299305
$0.kind == .uiElementDestroyed
@@ -334,7 +340,7 @@ public final class XcodeInspector: ObservableObject {
334340

335341
private var lastRecoveryFromAccessibilityMalfunctioningTimeStamp = Date()
336342

337-
@MainActor
343+
@XcodeInspectorActor
338344
private func checkForAccessibilityMalfunction(_ source: String) {
339345
guard Date().timeIntervalSince(lastRecoveryFromAccessibilityMalfunctioningTimeStamp) > 5
340346
else { return }
@@ -356,7 +362,7 @@ public final class XcodeInspector: ObservableObject {
356362
}
357363
}
358364

359-
@MainActor
365+
@XcodeInspectorActor
360366
private func recoverFromAccessibilityMalfunctioning(_ source: String?) {
361367
let message = """
362368
Accessibility API malfunction detected: \

Tool/Sources/XcodeInspector/XcodeWindowInspector.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class WorkspaceXcodeWindowInspector: XcodeWindowInspector {
2626
}
2727

2828
public func refresh() {
29-
Task { @MainActor in updateURLs() }
29+
Task { @XcodeInspectorActor in updateURLs() }
3030
}
3131

3232
public init(
@@ -62,7 +62,7 @@ public final class WorkspaceXcodeWindowInspector: XcodeWindowInspector {
6262
}
6363
}
6464

65-
@MainActor
65+
@XcodeInspectorActor
6666
func updateURLs() {
6767
let documentURL = Self.extractDocumentURL(windowElement: uiElement)
6868
if let documentURL {

0 commit comments

Comments
 (0)