Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

Commit

Permalink
fix(notification): fix timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
daveenguyen committed Mar 18, 2019
1 parent 30bacee commit b0cf4a7
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions atomacos/notification.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import signal
import threading
import time

import objc
from ApplicationServices import (
Expand Down Expand Up @@ -60,7 +62,6 @@ def set_notification(
self.callbackKwargs = dict()

self.callback_result = None
self.timedout = True

@objc.callbackFor(AXObserverCreate)
def _observer_callback(observer, element, notification, refcon):
Expand All @@ -73,13 +74,7 @@ def _observer_callback(observer, element, notification, refcon):
if callback_result is None:
raise RuntimeError("Python callback failed.")
if callback_result in (-1, 1):
self.timedout = False
AppHelper.stopEventLoop()

self.callback_result = callback_result
else:
self.timedout = False
AppHelper.stopEventLoop()
self.callback_result = callback_result

observer = PAXObserverCreate(self.ref.pid, _observer_callback)

Expand All @@ -93,9 +88,19 @@ def _observer_callback(observer, element, notification, refcon):
NSDefaultRunLoopMode,
)

def event_stopper():
end_time = time.time() + timeout
while time.time() < end_time:
if self.callback_result is not None:
break
AppHelper.callAfter(AppHelper.stopEventLoop)

event_watcher = threading.Thread(target=event_stopper)
event_watcher.daemon = True
event_watcher.start()

# Set the signal handlers prior to running the run loop
oldSigIntHandler = MachSignals.signal(signal.SIGINT, _sigHandler)
AppHelper.callLater(timeout, AppHelper.stopEventLoop)
AppHelper.runConsoleEventLoop()
MachSignals.signal(signal.SIGINT, oldSigIntHandler)

Expand Down

0 comments on commit b0cf4a7

Please sign in to comment.