Optimize setting data operations.

Setting local data on Chrome is not throttled, only debounce sync.
Don't make DBs save data on every page load, don't save cleaning operations if nothing's changed.
Debounce Unread.saveLastReadPost() on Chrome to every 5 seconds.
This commit is contained in:
Mayhem 2014-02-13 14:41:24 +01:00
parent cf552959e0
commit cbc85407df
4 changed files with 19 additions and 17 deletions

View File

@ -787,6 +787,7 @@ a.hide-announcement {
min-width: 100%;
overflow: hidden;
white-space: nowrap;
position: relative;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;

View File

@ -288,8 +288,7 @@ do ->
delete timeout[area]
items[area] = {}
setAll = $.debounce $.SECOND, ->
setArea 'local'
setSync = $.debounce $.SECOND, ->
setArea 'sync'
$.set = (key, val) ->
@ -300,7 +299,8 @@ do ->
for key in $.localKeys when key of items.sync
items.local[key] = items.sync[key]
delete items.sync[key]
setAll()
setArea 'local'
setSync()
$.clear = (cb) ->
items.local = {}

View File

@ -58,15 +58,14 @@ class DataBoard
val or defaultValue
clean: ->
for boardID, val of @data.boards
@deleteIfEmpty {boardID}
now = Date.now()
if (@data.lastChecked or 0) < now - 2 * $.HOUR
@data.lastChecked = now
for boardID of @data.boards
@ajaxClean boardID
return if (@data.lastChecked or 0) > now - 2 * $.HOUR
for boardID of @data.boards
@deleteIfEmpty {boardID}
@ajaxClean boardID if boardID of @data.boards
@data.lastChecked = now
@save()
ajaxClean: (boardID) ->
$.cache "//a.4cdn.org/#{boardID}/threads.json", (e) =>
@ -76,12 +75,14 @@ class DataBoard
board = @data.boards[boardID]
threads = {}
for page in e.target.response
for thread in page.threads
if thread.no of board
threads[thread.no] = board[thread.no]
@data.boards[boardID] = threads
@deleteIfEmpty {boardID}
@save()
for thread in page.threads when thread.no of board
threads[thread.no] = board[thread.no]
count = Object.keys(threads).length
return if count is Object.keys(board).length # Nothing changed.
if count
@set {boardID, val: threads}
else
@delete {boardID}
onSync: (data) =>
@data = data or boards: {}

View File

@ -139,7 +139,7 @@ Unread =
Unread.readArray Unread.postsQuotingYou
Unread.update() if e
saveLastReadPost: ->
saveLastReadPost: <% if (type === 'crx') { %>$.debounce 5 * $.SECOND,<% } %> ->
return if Unread.thread.isDead
Unread.db.set
boardID: Unread.thread.board.ID