Skip to content

Commit

Permalink
- Save the cache contents only when necessary
Browse files Browse the repository at this point in the history
- Handle the unload event to avoid AttributeError (#68, thanks to @Timandes)
  • Loading branch information
seanliang committed Jul 10, 2016
1 parent c364489 commit a3b4418
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
18 changes: 11 additions & 7 deletions ConvertToUTF8.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ def __init__(self):
self.max_size = -1
self.dirty = False
self.load()
self.save_on_dirty()

def save_on_dirty(self):
if self.dirty:
self.save()
sublime.set_timeout(self.save_on_dirty, 10000)
return
self.dirty = True
sublime.set_timeout(self.save, 10000)

def shrink(self):
if self.max_size < 0:
return
if len(self.cache) > self.max_size:
self.dirty = True
self.save_on_dirty()
del self.cache[self.max_size:]

def set_max_size(self, max_size):
Expand All @@ -80,7 +80,7 @@ def load(self):
item['file']: item['encoding']
})
self.cache = new_cache
self.dirty = True
self.save_on_dirty()

def save(self):
self.shrink()
Expand All @@ -99,7 +99,7 @@ def pop(self, file_name):
for item in self.cache:
if file_name in item:
self.cache.remove(item)
self.dirty = True
self.save_on_dirty()
return item.get(file_name)
return None

Expand All @@ -110,7 +110,7 @@ def set(self, file_name, encoding):
self.cache.insert(0, {
file_name: encoding
})
self.dirty = True
self.save_on_dirty()

encoding_cache = None

Expand Down Expand Up @@ -201,6 +201,10 @@ def plugin_loaded():
init_settings()
setup_views()

def plugin_unloaded():
encoding_cache = None
sublime.load_settings('ConvertToUTF8.sublime-settings').clear_on_change('get_settings')

def wait_for_ready():
if sublime.windows():
setup_views()
Expand Down
1 change: 1 addition & 0 deletions messages.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"1.2.11": "messages/1.2.11.txt",
"1.2.10": "messages/1.2.10.txt",
"1.2.9": "messages/1.2.9.txt",
"1.2.8": "messages/1.2.8.txt",
Expand Down
7 changes: 7 additions & 0 deletions messages/1.2.11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ConvertToUTF8 1.2.11 Changelog

Enhancements
- Save the cache contents only when necessary

Fixed
- Handle the unload event to avoid AttributeError (#68, thanks to @Timandes)

0 comments on commit a3b4418

Please sign in to comment.