Move yourPosts out of synced storage.

Also try to make it easier to move stuff in and out of synched storage if necessary.
This commit is contained in:
ccd0 2014-10-07 23:03:44 -07:00
parent 6f32c8f31b
commit 8b7275df22

View File

@ -288,14 +288,7 @@ $.item = (key, val) ->
$.syncing = {} $.syncing = {}
<% if (type === 'crx') { %> <% if (type === 'crx') { %>
$.sync = do -> # https://developer.chrome.com/extensions/storage.html
chrome.storage.onChanged.addListener (changes) ->
for key of changes
if cb = $.syncing[key]
cb changes[key].newValue, key
return
(key, cb) -> $.syncing[key] = cb
$.forceSync = (key) -> return
$.localKeys = [ $.localKeys = [
# filters # filters
'name', 'name',
@ -310,57 +303,64 @@ $.localKeys = [
'filesize', 'filesize',
'MD5', 'MD5',
# custom css # custom css
'usercss' 'usercss',
# your posts
'yourPosts'
] ]
# https://developer.chrome.com/extensions/storage.html
do ->
items =
local: {}
sync: {}
$.delete = (keys) -> chrome.storage.onChanged.addListener (changes, area) ->
if typeof keys is 'string' for key of changes
keys = [keys] cb = $.syncing[key]
local = [] if cb and (key in $.localKeys) is (area is 'local')
sync = [] cb changes[key].newValue, key
for key in keys return
if key in $.localKeys $.sync = (key, cb) ->
local.push key $.syncing[key] = cb
delete items.local[key] $.forceSync = (key) -> return
else
sync.push key
delete items.sync[key]
chrome.storage.local.remove local
chrome.storage.sync.remove sync
$.get = (key, val, cb) -> $.get = (key, val, cb) ->
if typeof cb is 'function' if typeof cb is 'function'
data = $.item key, val data = $.item key, val
else else
data = key data = key
cb = val cb = val
localItems = null results = {}
syncItems = null get = (area) ->
for key, val of data chrome.storage[area].get Object.keys(data), (result) ->
if key in $.localKeys
(localItems or= {})[key] = val
else
(syncItems or= {})[key] = val
count = 0
done = (result) ->
if chrome.runtime.lastError if chrome.runtime.lastError
c.error chrome.runtime.lastError.message c.error chrome.runtime.lastError.message
$.extend data, result results[area] = result
cb data unless --count if results.local and results.sync
$.extend data, results.sync
$.extend data, results.local
misplaced = null
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
get 'local'
get 'sync'
if localItems do ->
count++ items =
chrome.storage.local.get localItems, done local: {}
if syncItems sync: {}
count++
chrome.storage.sync.get syncItems, done exceedsQuota = (key, value) ->
# bytes in UTF-8
unescape(encodeURIComponent(JSON.stringify(key))).length + unescape(encodeURIComponent(JSON.stringify(value))).length > chrome.storage.sync.QUOTA_BYTES_PER_ITEM
$.delete = (keys) ->
if typeof keys is 'string'
keys = [keys]
for key in keys
delete items.local[key]
delete items.sync[key]
chrome.storage.local.remove keys
chrome.storage.sync.remove keys
timeout = {} timeout = {}
setArea = (area) -> setArea = (area) ->
@ -370,7 +370,7 @@ do ->
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] for key, val of data when key not of items[area]
if area is 'sync' and chrome.storage.sync.QUOTA_BYTES_PER_ITEM < JSON.stringify(val).length + key.length if area is 'sync' and exceedsQuota key, val
c.error chrome.runtime.lastError.message, key, val c.error chrome.runtime.lastError.message, key, val
continue continue
items[area][key] = val items[area][key] = val