Skip to content

Commit ce40906

Browse files
committed
Merge branch 'feature/ax-notification-out-of-main-thread' into develop
2 parents e6ca4d4 + f6b04ed commit ce40906

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

Core/Sources/HostApp/DebugView.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ final class DebugSettings: ObservableObject {
2525
var restartXcodeInspectorIfAccessibilityAPIIsMalfunctioningNoTimer
2626
@AppStorage(\.toastForTheReasonWhyXcodeInspectorNeedsToBeRestarted)
2727
var toastForTheReasonWhyXcodeInspectorNeedsToBeRestarted
28+
@AppStorage(\.observeToAXNotificationOnAnotherThread)
29+
var observeToAXNotificationOnAnotherThread
30+
@AppStorage(\.observeToAXNotificationWithDefaultMode)
31+
var observeToAXNotificationWithDefaultMode
2832
init() {}
2933
}
3034

@@ -116,6 +120,20 @@ struct DebugSettingsView: View {
116120
UserDefaults.shared.set(nil, forKey: "ChatModels")
117121
UserDefaults.shared.set(nil, forKey: "EmbeddingModels")
118122
}
123+
124+
Group {
125+
Toggle(
126+
isOn: $settings.observeToAXNotificationOnAnotherThread
127+
) {
128+
Text("Observe to AXNotification on background thread")
129+
}
130+
131+
Toggle(
132+
isOn: $settings.observeToAXNotificationWithDefaultMode
133+
) {
134+
Text("Observe to AXNotification with default mode")
135+
}
136+
}
119137
}
120138
}
121139
.padding()

Tool/Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ let package = Package(
160160
.target(
161161
name: "AXNotificationStream",
162162
dependencies: [
163+
"Preferences",
163164
"Logger",
164165
]
165166
),

Tool/Sources/AXNotificationStream/AXNotificationStream.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import AppKit
22
import ApplicationServices
33
import Foundation
44
import Logger
5+
import Preferences
56

67
public final class AXNotificationStream: AsyncSequence {
78
public typealias Stream = AsyncStream<Element>
@@ -53,6 +54,15 @@ public final class AXNotificationStream: AsyncSequence {
5354
self.file = file
5455
self.line = line
5556
self.function = function
57+
58+
let mode: CFRunLoopMode = UserDefaults.shared
59+
.value(for: \.observeToAXNotificationWithDefaultMode) ? .defaultMode : .commonModes
60+
61+
let runLoop: CFRunLoop = UserDefaults.shared
62+
.value(for: \.observeToAXNotificationOnAnotherThread)
63+
? DispatchQueue.global(qos: .userInteractive).sync { CFRunLoopGetCurrent() }
64+
: CFRunLoopGetMain()
65+
5666
var cont: Continuation!
5767
stream = Stream { continuation in
5868
cont = continuation
@@ -88,17 +98,17 @@ public final class AXNotificationStream: AsyncSequence {
8898
AXObserverRemoveNotification(observer, observingElement, name as CFString)
8999
}
90100
CFRunLoopRemoveSource(
91-
CFRunLoopGetMain(),
101+
runLoop,
92102
AXObserverGetRunLoopSource(observer),
93-
.commonModes
103+
mode
94104
)
95105
}
96106

97107
Task { @MainActor [weak self] in
98108
CFRunLoopAddSource(
99-
CFRunLoopGetMain(),
109+
runLoop,
100110
AXObserverGetRunLoopSource(observer),
101-
.commonModes
111+
mode
102112
)
103113
var pendingRegistrationNames = Set(notificationNames)
104114
var retry = 0

Tool/Sources/Preferences/Keys.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,5 +604,13 @@ public extension UserDefaultPreferenceKeys {
604604
key: "FeatureFlag-ToastForTheReasonWhyXcodeInspectorNeedsToBeRestarted"
605605
)
606606
}
607+
608+
var observeToAXNotificationOnAnotherThread: FeatureFlag {
609+
.init(defaultValue: false, key: "FeatureFlag-observeToAXNotificationOnAnotherThread")
610+
}
611+
612+
var observeToAXNotificationWithDefaultMode: FeatureFlag {
613+
.init(defaultValue: false, key: "FeatureFlag-observeToAXNotificationWithDefaultMode")
614+
}
607615
}
608616

0 commit comments

Comments
 (0)