Skip to content

Commit bdf562c

Browse files
committed
Make the task more structured
1 parent 9a34b89 commit bdf562c

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

Tool/Sources/XcodeInspector/XcodeWindowInspector.swift

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

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

3232
public init(app: NSRunningApplication, uiElement: AXUIElement) {
3333
self.app = app
3434
super.init(uiElement: uiElement)
3535

36-
focusedElementChangedTask = Task { @MainActor in
37-
updateURLs()
36+
let notifications = AXNotificationStream(
37+
app: app,
38+
notificationNames: kAXFocusedUIElementChangedNotification
39+
)
3840

39-
Task { @MainActor in
40-
// prevent that documentURL may not be available yet
41-
try await Task.sleep(nanoseconds: 500_000_000)
42-
if documentURL == .init(fileURLWithPath: "/") {
43-
updateURLs()
41+
#warning("Test Me")
42+
focusedElementChangedTask = Task { [weak self] in
43+
await self?.updateURLs()
44+
45+
await withThrowingTaskGroup(of: Void.self) { [weak self] group in
46+
group.addTask { [weak self] in
47+
// prevent that documentURL may not be available yet
48+
try await Task.sleep(nanoseconds: 500_000_000)
49+
if self?.documentURL == .init(fileURLWithPath: "/") {
50+
await self?.updateURLs()
51+
}
4452
}
45-
}
4653

47-
let notifications = AXNotificationStream(
48-
app: app,
49-
notificationNames: kAXFocusedUIElementChangedNotification
50-
)
51-
52-
for await _ in notifications {
53-
try Task.checkCancellation()
54-
updateURLs()
54+
group.addTask { [weak self] in
55+
for await _ in notifications {
56+
guard let self else { return }
57+
try Task.checkCancellation()
58+
await self.updateURLs()
59+
}
60+
}
5561
}
5662
}
5763
}
5864

65+
@MainActor
5966
func updateURLs() {
6067
let documentURL = Self.extractDocumentURL(windowElement: uiElement)
6168
if let documentURL {

0 commit comments

Comments
 (0)