Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions Sources/Observer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public final class Observer {
_ info: [String: AnyObject]?) -> Void

let pid: pid_t
let axObserver: AXObserver!
let axObserver: AXObserver?
let callback: Callback?
let callbackWithInfo: CallbackWithInfo?

Expand Down Expand Up @@ -71,10 +71,16 @@ public final class Observer {
///
/// If the observer has already been started, this method does nothing.
public func start() {
guard let axObserver else {
NSLog("\(#function) axObserver is nil")
return
}

CFRunLoopAddSource(
RunLoop.current.getCFRunLoop(),
AXObserverGetRunLoopSource(axObserver),
CFRunLoopMode.defaultMode)

}

/// Stops sending events to your callback until the next call to `start`.
Expand All @@ -84,6 +90,11 @@ public final class Observer {
/// - important: Events will still be queued in the target process until the Observer is started
/// again or destroyed. If you don't want them, create a new Observer.
public func stop() {
guard let axObserver else {
NSLog("\(#function) axObserver is nil")
return
}

CFRunLoopRemoveSource(
RunLoop.current.getCFRunLoop(),
AXObserverGetRunLoopSource(axObserver),
Expand All @@ -104,7 +115,7 @@ public final class Observer {
forElement element: UIElement) throws {
let selfPtr = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
let error = AXObserverAddNotification(
axObserver, element.element, notification.rawValue as CFString, selfPtr
axObserver!, element.element, notification.rawValue as CFString, selfPtr
)
guard error == .success || error == .notificationAlreadyRegistered else {
throw error
Expand All @@ -122,7 +133,7 @@ public final class Observer {
public func removeNotification(_ notification: AXNotification,
forElement element: UIElement) throws {
let error = AXObserverRemoveNotification(
axObserver, element.element, notification.rawValue as CFString
axObserver!, element.element, notification.rawValue as CFString
)
guard error == .success || error == .notificationNotRegistered else {
throw error
Expand Down