Reduce disk reads preformed by QR Cooldown.

This commit is contained in:
ccd0 2017-07-28 22:40:29 -07:00
parent e647586a1f
commit d8dd672710

View File

@ -7,6 +7,7 @@ QR.cooldown =
init: -> init: ->
return unless Conf['Quick Reply'] return unless Conf['Quick Reply']
@data = Conf['cooldowns'] @data = Conf['cooldowns']
@changes = {}
$.sync 'cooldowns', @sync $.sync 'cooldowns', @sync
# Called from QR # Called from QR
@ -43,6 +44,7 @@ QR.cooldown =
boardID = g.BOARD.ID boardID = g.BOARD.ID
QR.cooldown.set boardID, 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.save()
QR.cooldown.start() QR.cooldown.start()
addDelay: (post, delay) -> addDelay: (post, delay) ->
@ -50,21 +52,22 @@ QR.cooldown =
cooldown = QR.cooldown.categorize post cooldown = QR.cooldown.categorize post
cooldown.delay = delay cooldown.delay = delay
QR.cooldown.set g.BOARD.ID, Date.now(), cooldown QR.cooldown.set g.BOARD.ID, Date.now(), cooldown
QR.cooldown.save()
QR.cooldown.start() QR.cooldown.start()
addMute: (delay) -> addMute: (delay) ->
return unless Conf['Cooldown'] return unless Conf['Cooldown']
QR.cooldown.set g.BOARD.ID, Date.now(), {type: 'mute', delay} QR.cooldown.set g.BOARD.ID, Date.now(), {type: 'mute', delay}
QR.cooldown.save()
QR.cooldown.start() QR.cooldown.start()
delete: (post) -> delete: (post) ->
return unless QR.cooldown.data return unless QR.cooldown.data
$.forceSync 'cooldowns'
cooldowns = (QR.cooldown.data[post.board.ID] or= {}) cooldowns = (QR.cooldown.data[post.board.ID] or= {})
for id, cooldown of cooldowns 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 cooldowns[id] QR.cooldown.set post.board.ID, id, null
QR.cooldown.save [post.board.ID] QR.cooldown.save()
secondsDeletion: (post) -> secondsDeletion: (post) ->
return 0 unless QR.cooldown.data and Conf['Cooldown'] return 0 unless QR.cooldown.data and Conf['Cooldown']
@ -82,23 +85,32 @@ QR.cooldown =
type: if !!post.file then 'image' else 'reply' type: if !!post.file then 'image' else 'reply'
threadID: +post.thread threadID: +post.thread
set: (scope, id, value) -> mergeChange: (data, scope, id, value) ->
$.forceSync 'cooldowns' if value
cooldowns = (QR.cooldown.data[scope] or= {}) (data[scope] or= {})[id] = value
cooldowns[id] = value else if scope of data
$.set 'cooldowns', QR.cooldown.data delete data[scope][id]
delete data[scope] if Object.keys(data[scope]).length is 0
save: (scopes) -> set: (scope, id, value) ->
{data} = QR.cooldown QR.cooldown.mergeChange QR.cooldown.data, scope, id, value
for scope in scopes when scope of data and !Object.keys(data[scope]).length (QR.cooldown.changes[scope] or= {})[id] = value
delete data[scope]
$.set 'cooldowns', data save: ->
{changes} = QR.cooldown
return unless Object.keys(changes).length
$.get 'cooldowns', {}, ({cooldowns}) ->
for scope of QR.cooldown.changes
for id, value of QR.cooldown.changes[scope]
QR.cooldown.mergeChange cooldowns, scope, id, value
QR.cooldown.data = cooldowns
$.set 'cooldowns', cooldowns, ->
QR.cooldown.changes = {}
update: -> update: ->
return unless QR.cooldown.isCounting return unless QR.cooldown.isCounting
$.forceSync 'cooldowns' save = false
save = []
nCooldowns = 0 nCooldowns = 0
now = Date.now() now = Date.now()
{type, threadID} = QR.cooldown.categorize QR.posts[0] {type, threadID} = QR.cooldown.categorize QR.posts[0]
@ -111,15 +123,15 @@ QR.cooldown =
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 cooldowns[start] QR.cooldown.set scope, start, null
save.push scope save = true
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 cooldowns[start] QR.cooldown.set scope, start, null
save.push scope save = true
else if (cooldown.type is type and cooldown.threadID is threadID) or cooldown.type is 'mute' else if (cooldown.type is type and cooldown.threadID is threadID) or cooldown.type is 'mute'
# 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
@ -133,8 +145,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 cooldowns[start] QR.cooldown.set scope, start, null
save.push scope save = true
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
@ -153,7 +165,7 @@ QR.cooldown =
nCooldowns += Object.keys(cooldowns).length nCooldowns += Object.keys(cooldowns).length
QR.cooldown.save save if save.length QR.cooldown.save if save
if nCooldowns if nCooldowns
clearTimeout QR.cooldown.timeout clearTimeout QR.cooldown.timeout