Skip to content

Commit f6b04ed

Browse files
committed
Change runloop and mode according to flags
1 parent 48ce998 commit f6b04ed

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

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

0 commit comments

Comments
 (0)