Move all cooldown data into one object.

This commit is contained in:
ccd0 2015-08-07 13:40:58 -07:00
parent dc6f633280
commit 959ce1d38e
2 changed files with 43 additions and 44 deletions

View File

@ -524,11 +524,8 @@ $.clear = (cb) ->
# XXX https://github.com/greasemonkey/greasemonkey/issues/2033 # XXX https://github.com/greasemonkey/greasemonkey/issues/2033
# Also support case where GM_listValues is not defined. # Also support case where GM_listValues is not defined.
$.delete Object.keys(Conf) $.delete Object.keys(Conf)
$.delete ['previousversion', 'AutoWatch', 'cooldown.global', 'QR Size', 'captchas', 'QR.persona', 'hiddenPSA'] $.delete ['previousversion', 'AutoWatch', 'cooldowns', 'QR Size', 'captchas', 'QR.persona', 'hiddenPSA']
$.delete ("#{id}.position" for id in ['embedding', 'updater', 'thread-stats', 'thread-watcher', 'qr']) $.delete ("#{id}.position" for id in ['embedding', 'updater', 'thread-stats', 'thread-watcher', 'qr'])
boards = (a.textContent for a in $$ '#boardNavDesktop > .boardList > a')
boards.push 'qa'
$.delete ("cooldown.#{board}" for board in boards)
try try
$.delete $.listValues().map (key) -> key.replace g.NAMESPACE, '' $.delete $.listValues().map (key) -> key.replace g.NAMESPACE, ''
cb?() cb?()

View File

@ -19,29 +19,25 @@ QR.cooldown =
QR.cooldown.delays['thread_global'] = 300 QR.cooldown.delays['thread_global'] = 300
# Retrieve recent posts and delays. # Retrieve recent posts and delays.
keys = QR.cooldown.keys = $.get 'cooldowns', {}, ({cooldowns}) ->
local: "cooldown.#{g.BOARD}" QR.cooldown.data = cooldowns
global: 'cooldown.global'
items = {}
items[key] = {} for scope, key of keys
$.get items, (items) ->
QR.cooldown[scope] = items[key] for scope, key of keys
QR.cooldown.start() QR.cooldown.start()
$.sync key, QR.cooldown.sync scope for scope, key of keys $.sync 'cooldowns', QR.cooldown.sync
start: -> start: ->
return if QR.cooldown.isCounting or Object.keys(QR.cooldown.local).length + Object.keys(QR.cooldown.global).length is 0 {data} = QR.cooldown
QR.cooldown.isCounting = true unless QR.cooldown.isCounting or Object.keys(data[g.BOARD.ID] or {}).length + Object.keys(data.global or {}).length is 0
QR.cooldown.count() QR.cooldown.isCounting = true
QR.cooldown.count()
sync: (scope) -> (cooldowns) -> sync: (data) ->
QR.cooldown[scope] = cooldowns or {} QR.cooldown.data = data or {}
QR.cooldown.start() QR.cooldown.start()
add: (start, threadID, postID) -> add: (start, threadID, postID) ->
return unless Conf['Cooldown'] return unless Conf['Cooldown']
boardID = g.BOARD.ID boardID = g.BOARD.ID
QR.cooldown.set 'local', start, {threadID, postID} QR.cooldown.set boardID, start, {threadID, postID}
QR.cooldown.set 'global', start, {boardID, threadID, postID} if threadID is postID QR.cooldown.set 'global', start, {boardID, threadID, postID} if threadID is postID
QR.cooldown.start() QR.cooldown.start()
@ -49,16 +45,17 @@ QR.cooldown =
return unless Conf['Cooldown'] return unless Conf['Cooldown']
cooldown = QR.cooldown.categorize post cooldown = QR.cooldown.categorize post
cooldown.delay = delay cooldown.delay = delay
QR.cooldown.set 'local', Date.now(), cooldown QR.cooldown.set g.BOARD.ID, Date.now(), cooldown
QR.cooldown.start() QR.cooldown.start()
delete: (post) -> delete: (post) ->
return unless Conf['Cooldown'] and g.BOARD.ID is post.board.ID return unless Conf['Cooldown']
$.forceSync QR.cooldown.keys.local $.forceSync 'cooldowns'
for id, cooldown of QR.cooldown.local cooldowns = (QR.cooldown.data[post.board.ID] or= {})
for id, cooldown of cooldowns
if !cooldown.delay? and cooldown.threadID is post.thread.ID and cooldown.postID is post.ID if !cooldown.delay? and cooldown.threadID is post.thread.ID and cooldown.postID is post.ID
delete QR.cooldown.local[id] delete cooldowns[id]
QR.cooldown.save 'local' QR.cooldown.save [post.board.ID]
categorize: (post) -> categorize: (post) ->
if post.thread is 'new' if post.thread is 'new'
@ -68,38 +65,41 @@ QR.cooldown =
threadID: +post.thread threadID: +post.thread
set: (scope, id, value) -> set: (scope, id, value) ->
$.forceSync QR.cooldown.keys[scope] $.forceSync 'cooldowns'
QR.cooldown[scope][id] = value cooldowns = (QR.cooldown.data[scope] or= {})
$.set QR.cooldown.keys[scope], QR.cooldown[scope] cooldowns[id] = value
$.set 'cooldowns', QR.cooldown.data
save: (scope) -> save: (scopes) ->
if Object.keys(QR.cooldown[scope]).length {data} = QR.cooldown
$.set QR.cooldown.keys[scope], QR.cooldown[scope] for scope in scopes when scope of data and !Object.keys(data[scope]).length
else delete data[scope]
$.delete QR.cooldown.keys[scope] $.set 'cooldowns', data
count: -> count: ->
$.forceSync 'cooldowns'
save = []
nCooldowns = 0
now = Date.now() now = Date.now()
{type, threadID} = QR.cooldown.categorize QR.posts[0] {type, threadID} = QR.cooldown.categorize QR.posts[0]
seconds = 0 seconds = 0
for scope, key of QR.cooldown.keys for scope in [g.BOARD.ID, 'global']
$.forceSync key cooldowns = (QR.cooldown.data[scope] or= {})
save = false
for start, cooldown of QR.cooldown[scope] for start, cooldown of cooldowns
start = +start start = +start
elapsed = (now - start) // $.SECOND elapsed = (now - start) // $.SECOND
if elapsed < 0 # clock changed since then? if elapsed < 0 # clock changed since then?
delete QR.cooldown[scope][start] delete cooldowns[start]
save = true save.push scope
continue continue
# Explicit delays from error messages # Explicit delays from error messages
if cooldown.delay? if cooldown.delay?
if cooldown.delay <= elapsed if cooldown.delay <= elapsed
delete QR.cooldown[scope][start] delete cooldowns[start]
save = true save.push scope
else if cooldown.type is type and cooldown.threadID is threadID else if cooldown.type is type and cooldown.threadID is threadID
# Delays only apply to the given post type and thread. # Delays only apply to the given post type and thread.
seconds = Math.max seconds, cooldown.delay - elapsed seconds = Math.max seconds, cooldown.delay - elapsed
@ -113,8 +113,8 @@ QR.cooldown =
if QR.cooldown.customCooldown if QR.cooldown.customCooldown
maxDelay = Math.max maxDelay, parseInt(Conf['customCooldown'], 10) maxDelay = Math.max maxDelay, parseInt(Conf['customCooldown'], 10)
if maxDelay <= elapsed if maxDelay <= elapsed
delete QR.cooldown[scope][start] delete cooldowns[start]
save = true save.push scope
continue continue
if (type is 'thread') is (cooldown.threadID is cooldown.postID) and cooldown.boardID isnt g.BOARD.ID if (type is 'thread') is (cooldown.threadID is cooldown.postID) and cooldown.boardID isnt g.BOARD.ID
@ -133,9 +133,11 @@ QR.cooldown =
if QR.cooldown.customCooldown if QR.cooldown.customCooldown
seconds = Math.max seconds, parseInt(Conf['customCooldown'], 10) - elapsed seconds = Math.max seconds, parseInt(Conf['customCooldown'], 10) - elapsed
QR.cooldown.save scope if save nCooldowns += Object.keys(cooldowns).length
if Object.keys(QR.cooldown.local).length + Object.keys(QR.cooldown.global).length QR.cooldown.save save if save.length
if nCooldowns
clearTimeout QR.cooldown.timeout clearTimeout QR.cooldown.timeout
QR.cooldown.timeout = setTimeout QR.cooldown.count, $.SECOND QR.cooldown.timeout = setTimeout QR.cooldown.count, $.SECOND
else else