Improve reading/writing of settings on Chromium.

Don't let failures to write synced storage break setting saving.
This commit is contained in:
ccd0 2015-02-14 12:09:38 -08:00
parent 7b2e5e310e
commit 321813f537

View File

@ -308,29 +308,12 @@ $.syncing = {}
<% if (type === 'crx') { %> <% if (type === 'crx') { %>
# https://developer.chrome.com/extensions/storage.html # https://developer.chrome.com/extensions/storage.html
$.localKeys = [ $.localKeys = {}
# filters
'name',
'uniqueID',
'tripcode',
'capcode',
'subject',
'comment',
'flag',
'filename',
'dimensions',
'filesize',
'MD5',
# custom css
'usercss',
# your posts
'yourPosts'
]
chrome.storage.onChanged.addListener (changes, area) -> chrome.storage.onChanged.addListener (changes, area) ->
for key of changes for key of changes
cb = $.syncing[key] cb = $.syncing[key]
if cb and (key in $.localKeys) is (area is 'local') if cb and (key of $.localKeys) is (area is 'local')
cb changes[key].newValue, key cb changes[key].newValue, key
return return
$.sync = (key, cb) -> $.sync = (key, cb) ->
@ -353,12 +336,7 @@ $.get = (key, val, cb) ->
if results.local and results.sync if results.local and results.sync
$.extend data, results.sync $.extend data, results.sync
$.extend data, results.local $.extend data, results.local
misplaced = null $.localKeys[key] = true for key of results.local
for key, val of results.local when !(key in $.localKeys)
(misplaced or= {})[key] = val
if misplaced
chrome.storage.sync.set misplaced, ->
chrome.storage.local.remove Object.keys(misplaced)
cb data cb data
get 'local' get 'local'
get 'sync' get 'sync'
@ -378,44 +356,49 @@ do ->
for key in keys for key in keys
delete items.local[key] delete items.local[key]
delete items.sync[key] delete items.sync[key]
delete $.localKeys[key]
chrome.storage.local.remove keys chrome.storage.local.remove keys
chrome.storage.sync.remove keys chrome.storage.sync.remove keys
timeout = {} timeout = {}
setArea = (area) -> setArea = (area) ->
data = items[area] data = {}
$.extend data, items[area]
return if !Object.keys(data).length or timeout[area] > Date.now() return if !Object.keys(data).length or timeout[area] > Date.now()
chrome.storage[area].set data, -> chrome.storage[area].set data, ->
if chrome.runtime.lastError if chrome.runtime.lastError
c.error chrome.runtime.lastError.message c.error chrome.runtime.lastError.message
for key, val of data when key not of items[area]
if area is 'sync' and exceedsQuota key, val
c.error chrome.runtime.lastError.message, key, val
continue
items[area][key] = val
setTimeout setArea, $.MINUTE, area setTimeout setArea, $.MINUTE, area
timeout[area] = Date.now() + $.MINUTE timeout[area] = Date.now() + $.MINUTE
return return
delete timeout[area] delete timeout[area]
items[area] = {} delete items[area][key] for key of data when items[area][key] is data[key]
if area is 'local'
items.sync[key] = val for key, val of data when not exceedsQuota(key, val)
setSync()
else
oldLocal = for key of data when key not of items.local
delete $.localKeys[key]
key
chrome.storage.local.remove oldLocal
setSync = $.debounce $.SECOND, -> setSync = $.debounce $.SECOND, ->
setArea 'sync' setArea 'sync'
$.set = (key, val) -> $.set = (key, val) ->
if typeof key is 'string' data = if typeof key is 'string'
items.sync[key] = val $.item key, val
else else
$.extend items.sync, key key
for key in $.localKeys when key of items.sync $.extend items.local, data
items.local[key] = items.sync[key] $.localKeys[key] = true for key of data
delete items.sync[key]
setArea 'local' setArea 'local'
setSync()
$.clear = (cb) -> $.clear = (cb) ->
items.local = {} items.local = {}
items.sync = {} items.sync = {}
$.localKeys = {}
count = 2 count = 2
done = -> done = ->
if chrome.runtime.lastError if chrome.runtime.lastError