From d8dd67271024072b1ec93e2f2bc3de226662c551 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Fri, 28 Jul 2017 22:40:29 -0700 Subject: [PATCH] Reduce disk reads preformed by QR Cooldown. --- src/Posting/QR.cooldown.coffee | 56 +++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/src/Posting/QR.cooldown.coffee b/src/Posting/QR.cooldown.coffee index de87327bc..17e9c7eb9 100644 --- a/src/Posting/QR.cooldown.coffee +++ b/src/Posting/QR.cooldown.coffee @@ -7,6 +7,7 @@ QR.cooldown = init: -> return unless Conf['Quick Reply'] @data = Conf['cooldowns'] + @changes = {} $.sync 'cooldowns', @sync # Called from QR @@ -43,6 +44,7 @@ QR.cooldown = boardID = g.BOARD.ID QR.cooldown.set boardID, start, {threadID, postID} QR.cooldown.set 'global', start, {boardID, threadID, postID} if threadID is postID + QR.cooldown.save() QR.cooldown.start() addDelay: (post, delay) -> @@ -50,21 +52,22 @@ QR.cooldown = cooldown = QR.cooldown.categorize post cooldown.delay = delay QR.cooldown.set g.BOARD.ID, Date.now(), cooldown + QR.cooldown.save() QR.cooldown.start() addMute: (delay) -> return unless Conf['Cooldown'] QR.cooldown.set g.BOARD.ID, Date.now(), {type: 'mute', delay} + QR.cooldown.save() QR.cooldown.start() delete: (post) -> return unless QR.cooldown.data - $.forceSync 'cooldowns' 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 - delete cooldowns[id] - QR.cooldown.save [post.board.ID] + QR.cooldown.set post.board.ID, id, null + QR.cooldown.save() secondsDeletion: (post) -> return 0 unless QR.cooldown.data and Conf['Cooldown'] @@ -82,23 +85,32 @@ QR.cooldown = type: if !!post.file then 'image' else 'reply' threadID: +post.thread - set: (scope, id, value) -> - $.forceSync 'cooldowns' - cooldowns = (QR.cooldown.data[scope] or= {}) - cooldowns[id] = value - $.set 'cooldowns', QR.cooldown.data + mergeChange: (data, scope, id, value) -> + if value + (data[scope] or= {})[id] = value + else if scope of data + delete data[scope][id] + delete data[scope] if Object.keys(data[scope]).length is 0 - save: (scopes) -> - {data} = QR.cooldown - for scope in scopes when scope of data and !Object.keys(data[scope]).length - delete data[scope] - $.set 'cooldowns', data + set: (scope, id, value) -> + QR.cooldown.mergeChange QR.cooldown.data, scope, id, value + (QR.cooldown.changes[scope] or= {})[id] = value + + 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: -> return unless QR.cooldown.isCounting - $.forceSync 'cooldowns' - save = [] + save = false nCooldowns = 0 now = Date.now() {type, threadID} = QR.cooldown.categorize QR.posts[0] @@ -111,15 +123,15 @@ QR.cooldown = start = +start elapsed = (now - start) // $.SECOND if elapsed < 0 # clock changed since then? - delete cooldowns[start] - save.push scope + QR.cooldown.set scope, start, null + save = true continue # Explicit delays from error messages if cooldown.delay? if cooldown.delay <= elapsed - delete cooldowns[start] - save.push scope + QR.cooldown.set scope, start, null + save = true 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. seconds = Math.max seconds, cooldown.delay - elapsed @@ -133,8 +145,8 @@ QR.cooldown = if QR.cooldown.customCooldown maxDelay = Math.max maxDelay, parseInt(Conf['customCooldown'], 10) if maxDelay <= elapsed - delete cooldowns[start] - save.push scope + QR.cooldown.set scope, start, null + save = true continue 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 - QR.cooldown.save save if save.length + QR.cooldown.save if save if nCooldowns clearTimeout QR.cooldown.timeout