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%; min-width: 100%;
overflow: hidden; overflow: hidden;
white-space: nowrap; white-space: nowrap;
position: relative;
-webkit-user-select: none; -webkit-user-select: none;
-moz-user-select: none; -moz-user-select: none;
user-select: none; user-select: none;

View File

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

View File

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

View File

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