fixed threading bug by not using set_timeout, which is blocking.

This commit is contained in:
Alan Hamlett 2013-08-14 01:49:08 -07:00
parent 601a9b9674
commit 09c9252b9b
1 changed files with 17 additions and 12 deletions

View File

@ -111,13 +111,21 @@ def enough_time_passed(now):
def handle_write_action(view): def handle_write_action(view):
thread = SendActionThread(view.file_name(), isWrite=True) global LAST_FILE, LAST_ACTION
targetFile = view.file_name()
thread = SendActionThread(targetFile, isWrite=True)
thread.start() thread.start()
LAST_FILE = targetFile
LAST_ACTION = time.time()
def handle_normal_action(view): def handle_normal_action(view):
thread = SendActionThread(view.file_name()) global LAST_FILE, LAST_ACTION
targetFile = view.file_name()
thread = SendActionThread(targetFile)
thread.start() thread.start()
LAST_FILE = targetFile
LAST_ACTION = time.time()
class SendActionThread(threading.Thread): class SendActionThread(threading.Thread):
@ -127,33 +135,30 @@ class SendActionThread(threading.Thread):
self.targetFile = targetFile self.targetFile = targetFile
self.isWrite = isWrite self.isWrite = isWrite
self.force = force self.force = force
self.debug = SETTINGS.get('debug')
self.api_key = get_api_key()
def run(self): def run(self):
sublime.set_timeout(self.check, 0)
def check(self):
global LAST_ACTION, LAST_FILE
if self.targetFile: if self.targetFile:
self.timestamp = time.time() self.timestamp = time.time()
if self.force or self.isWrite or self.targetFile != LAST_FILE or enough_time_passed(self.timestamp): if self.force or self.isWrite or self.targetFile != LAST_FILE or enough_time_passed(self.timestamp):
LAST_FILE = self.targetFile
LAST_ACTION = self.timestamp
self.send() self.send()
def send(self): def send(self):
api_key = get_api_key() time.sleep(5)
if not api_key: if not self.api_key:
print('missing api key')
return return
cmd = [ cmd = [
API_CLIENT, API_CLIENT,
'--file', self.targetFile, '--file', self.targetFile,
'--time', str('%f' % self.timestamp), '--time', str('%f' % self.timestamp),
'--plugin', 'sublime-wakatime/%s' % __version__, '--plugin', 'sublime-wakatime/%s' % __version__,
'--key', str(bytes.decode(api_key.encode('utf8'))), '--key', str(bytes.decode(self.api_key.encode('utf8'))),
] ]
if self.isWrite: if self.isWrite:
cmd.append('--write') cmd.append('--write')
if SETTINGS.get('debug'): if self.debug:
cmd.append('--verbose') cmd.append('--verbose')
print(cmd) print(cmd)
if HAS_SSL: if HAS_SSL: