prevent sending timestamp when saving same file multiple times. increase send frequency from 5 minutes to 2 minutes.

This commit is contained in:
Alan Hamlett 2013-11-28 12:16:43 +01:00
parent 3127f264b4
commit a1c0d7e489
1 changed files with 19 additions and 12 deletions

View File

@ -21,14 +21,17 @@ from os.path import expanduser, dirname, realpath, isfile, join, exists
# globals # globals
ACTION_FREQUENCY = 5 ACTION_FREQUENCY = 2
ST_VERSION = int(sublime.version()) ST_VERSION = int(sublime.version())
PLUGIN_DIR = dirname(realpath(__file__)) PLUGIN_DIR = dirname(realpath(__file__))
API_CLIENT = '%s/packages/wakatime/wakatime-cli.py' % PLUGIN_DIR API_CLIENT = '%s/packages/wakatime/wakatime-cli.py' % PLUGIN_DIR
SETTINGS_FILE = 'WakaTime.sublime-settings' SETTINGS_FILE = 'WakaTime.sublime-settings'
SETTINGS = {} SETTINGS = {}
LAST_ACTION = 0 LAST_ACTION = {
LAST_FILE = None 'time': 0,
'file': None,
'is_write': False,
}
HAS_SSL = False HAS_SSL = False
LOCK = threading.RLock() LOCK = threading.RLock()
@ -76,20 +79,24 @@ def python_binary():
return 'python' return 'python'
def enough_time_passed(now): def enough_time_passed(now, last_time):
if now - LAST_ACTION > ACTION_FREQUENCY * 60: if now - last_time > ACTION_FREQUENCY * 60:
return True return True
return False return False
def handle_action(view, is_write=False): def handle_action(view, is_write=False):
global LOCK, LAST_FILE, LAST_ACTION global LOCK, LAST_ACTION
with LOCK: with LOCK:
target_file = view.file_name() target_file = view.file_name()
thread = SendActionThread(target_file, is_write=is_write) if target_file:
thread.start() thread = SendActionThread(target_file, is_write=is_write)
LAST_FILE = target_file thread.start()
LAST_ACTION = time.time() LAST_ACTION = {
'file': target_file,
'time': time.time(),
'is_write': is_write,
}
class SendActionThread(threading.Thread): class SendActionThread(threading.Thread):
@ -102,12 +109,12 @@ class SendActionThread(threading.Thread):
self.debug = SETTINGS.get('debug') self.debug = SETTINGS.get('debug')
self.api_key = SETTINGS.get('api_key', '') self.api_key = SETTINGS.get('api_key', '')
self.ignore = SETTINGS.get('ignore', []) self.ignore = SETTINGS.get('ignore', [])
self.last_file = LAST_FILE self.last_action = LAST_ACTION
def run(self): def run(self):
if self.target_file: if self.target_file:
self.timestamp = time.time() self.timestamp = time.time()
if self.force or self.is_write or self.target_file != self.last_file or enough_time_passed(self.timestamp): if self.force or (self.is_write and not self.last_action['is_write']) or self.target_file != self.last_action['file'] or enough_time_passed(self.timestamp, self.last_action['time']):
self.send() self.send()
def send(self): def send(self):