From a7d89fbd88077f29c785784be7afd354dd976315 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Fri, 22 Feb 2013 21:21:11 +0100 Subject: [PATCH] Add Misc.clearThreads, remove redundant code. --- 4chan_x.user.js | 119 ++++++++++++++++++-------------------------- src/features.coffee | 80 ++++++++++++----------------- 2 files changed, 80 insertions(+), 119 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 3fe9f23e0..a71420cf4 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -43,7 +43,7 @@ */ (function() { - var $, $$, Anonymize, ArchiveLink, AutoGIF, Board, Build, Clone, Conf, Config, DeleteLink, DownloadLink, ExpandComment, ExpandThread, Favicon, FileInfo, Filter, Fourchan, Get, Header, ImageExpand, ImageHover, Keybinds, Main, Menu, Nav, Notification, Polyfill, Post, QR, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Quotify, Recursive, Redirect, RelativeDates, ReplyHiding, ReportLink, RevealSpoilers, Sauce, Settings, Thread, ThreadExcerpt, ThreadHiding, ThreadStats, ThreadUpdater, ThreadWatcher, Time, UI, Unread, d, doc, g, + var $, $$, Anonymize, ArchiveLink, AutoGIF, Board, Build, Clone, Conf, Config, DeleteLink, DownloadLink, ExpandComment, ExpandThread, Favicon, FileInfo, Filter, Fourchan, Get, Header, ImageExpand, ImageHover, Keybinds, Main, Menu, Misc, Nav, Notification, Polyfill, Post, QR, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Quotify, Recursive, Redirect, RelativeDates, ReplyHiding, ReportLink, RevealSpoilers, Sauce, Settings, Thread, ThreadExcerpt, ThreadHiding, ThreadStats, ThreadUpdater, ThreadWatcher, Time, UI, Unread, d, doc, g, __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; @@ -1897,9 +1897,9 @@ if (g.VIEW !== 'index' || !Conf['Thread Hiding']) { return; } + Misc.clearThreads("hiddenThreads." + g.BOARD); this.getHiddenThreads(); this.syncFromCatalog(); - this.clean(); return Thread.prototype.callbacks.push({ name: 'Thread Hiding', cb: this.node @@ -1920,8 +1920,7 @@ hiddenThreads = $.get("hiddenThreads." + g.BOARD); if (!hiddenThreads) { hiddenThreads = { - threads: {}, - lastChecked: Date.now() + threads: {} }; $.set("hiddenThreads." + g.BOARD, hiddenThreads); } @@ -1945,38 +1944,6 @@ } return $.set("hiddenThreads." + g.BOARD, ThreadHiding.hiddenThreads); }, - clean: function() { - var hiddenThreads, lastChecked, now; - hiddenThreads = ThreadHiding.hiddenThreads; - lastChecked = hiddenThreads.lastChecked; - hiddenThreads.lastChecked = now = Date.now(); - if (lastChecked > now - $.DAY) { - return; - } - if (!Object.keys(hiddenThreads.threads).length) { - $.set("hiddenThreads." + g.BOARD, hiddenThreads); - return; - } - return $.ajax("//api.4chan.org/" + g.BOARD + "/catalog.json", { - onload: function() { - var obj, thread, threads, _i, _j, _len, _len1, _ref, _ref1; - threads = {}; - _ref = JSON.parse(this.response); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - obj = _ref[_i]; - _ref1 = obj.threads; - for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { - thread = _ref1[_j]; - if (thread.no in hiddenThreads.threads) { - threads[thread.no] = hiddenThreads.threads[thread.no]; - } - } - } - hiddenThreads.threads = threads; - return $.set("hiddenThreads." + g.BOARD, hiddenThreads); - } - }); - }, menu: { init: function() { var apply, div, makeStub; @@ -2111,8 +2078,8 @@ if (g.VIEW === 'catalog' || !Conf['Reply Hiding']) { return; } + Misc.clearThreads("hiddenPosts." + g.BOARD); this.getHiddenPosts(); - this.clean(); return Post.prototype.callbacks.push({ name: 'Reply Hiding', cb: this.node @@ -2142,45 +2109,12 @@ hiddenPosts = $.get("hiddenPosts." + g.BOARD); if (!hiddenPosts) { hiddenPosts = { - threads: {}, - lastChecked: Date.now() + threads: {} }; $.set("hiddenPosts." + g.BOARD, hiddenPosts); } return ReplyHiding.hiddenPosts = hiddenPosts; }, - clean: function() { - var hiddenPosts, lastChecked, now; - hiddenPosts = ReplyHiding.hiddenPosts; - lastChecked = hiddenPosts.lastChecked; - hiddenPosts.lastChecked = now = Date.now(); - if (lastChecked > now - $.DAY) { - return; - } - if (!Object.keys(hiddenPosts.threads).length) { - $.set("hiddenPosts." + g.BOARD, hiddenPosts); - return; - } - return $.ajax("//api.4chan.org/" + g.BOARD + "/catalog.json", { - onload: function() { - var obj, thread, threads, _i, _j, _len, _len1, _ref, _ref1; - threads = {}; - _ref = JSON.parse(this.response); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - obj = _ref[_i]; - _ref1 = obj.threads; - for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { - thread = _ref1[_j]; - if (thread.no in hiddenPosts.threads) { - threads[thread.no] = hiddenPosts.threads[thread.no]; - } - } - } - hiddenPosts.threads = threads; - return $.set("hiddenPosts." + g.BOARD, hiddenPosts); - } - }); - }, menu: { init: function() { var apply, div, makeStub, replies, thisPost; @@ -3582,6 +3516,48 @@ } }; + Misc = { + clearThreads: function(key) { + var data, now; + now = Date.now(); + data = $.get(key, { + threads: {} + }); + if (!data.lastChecked) { + data.lastChecked = now; + $.set(key, data); + return; + } + if (data.lastChecked > now - $.DAY) { + return; + } + data.lastChecked = now; + if (!Object.keys(data.threads).length) { + $.set(key, data); + return; + } + return $.ajax("//api.4chan.org/" + g.BOARD + "/catalog.json", { + onload: function() { + var obj, thread, threads, _i, _j, _len, _len1, _ref, _ref1; + threads = {}; + _ref = JSON.parse(this.response); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + obj = _ref[_i]; + _ref1 = obj.threads; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + thread = _ref1[_j]; + if (thread.no in data.threads) { + threads[thread.no] = data.threads[thread.no]; + } + } + } + data.threads = threads; + return $.set(key, data); + } + }); + } + }; + Quotify = { init: function() { if (g.VIEW === 'catalog' || !Conf['Resurrect Quotes']) { @@ -4939,6 +4915,7 @@ if (g.VIEW !== 'thread' || !Conf['Unread Count'] && !Conf['Unread Tab Icon']) { return; } + Misc.clearThreads("lastReadPosts." + g.BOARD); return Thread.prototype.callbacks.push({ name: 'Unread', cb: this.node diff --git a/src/features.coffee b/src/features.coffee index 8bd2e0332..fb8126163 100644 --- a/src/features.coffee +++ b/src/features.coffee @@ -878,9 +878,9 @@ ThreadHiding = init: -> return if g.VIEW isnt 'index' or !Conf['Thread Hiding'] + Misc.clearThreads "hiddenThreads.#{g.BOARD}" @getHiddenThreads() @syncFromCatalog() - @clean() Thread::callbacks.push name: 'Thread Hiding' cb: @node @@ -894,9 +894,7 @@ ThreadHiding = getHiddenThreads: -> hiddenThreads = $.get "hiddenThreads.#{g.BOARD}" unless hiddenThreads - hiddenThreads = - threads: {} - lastChecked: Date.now() + hiddenThreads = threads: {} $.set "hiddenThreads.#{g.BOARD}", hiddenThreads ThreadHiding.hiddenThreads = hiddenThreads @@ -917,26 +915,6 @@ ThreadHiding = $.set "hiddenThreads.#{g.BOARD}", ThreadHiding.hiddenThreads - clean: -> - {hiddenThreads} = ThreadHiding - {lastChecked} = hiddenThreads - hiddenThreads.lastChecked = now = Date.now() - - return if lastChecked > now - $.DAY - - unless Object.keys(hiddenThreads.threads).length - $.set "hiddenThreads.#{g.BOARD}", hiddenThreads - return - - $.ajax "//api.4chan.org/#{g.BOARD}/catalog.json", onload: -> - threads = {} - for obj in JSON.parse @response - for thread in obj.threads - if thread.no of hiddenThreads.threads - threads[thread.no] = hiddenThreads.threads[thread.no] - hiddenThreads.threads = threads - $.set "hiddenThreads.#{g.BOARD}", hiddenThreads - menu: init: -> return if g.VIEW isnt 'index' or !Conf['Menu'] or !Conf['Thread Hiding'] @@ -1040,8 +1018,8 @@ ReplyHiding = init: -> return if g.VIEW is 'catalog' or !Conf['Reply Hiding'] + Misc.clearThreads "hiddenPosts.#{g.BOARD}" @getHiddenPosts() - @clean() Post::callbacks.push name: 'Reply Hiding' cb: @node @@ -1060,32 +1038,10 @@ ReplyHiding = getHiddenPosts: -> hiddenPosts = $.get "hiddenPosts.#{g.BOARD}" unless hiddenPosts - hiddenPosts = - threads: {} - lastChecked: Date.now() + hiddenPosts = threads: {} $.set "hiddenPosts.#{g.BOARD}", hiddenPosts ReplyHiding.hiddenPosts = hiddenPosts - clean: -> - {hiddenPosts} = ReplyHiding - {lastChecked} = hiddenPosts - hiddenPosts.lastChecked = now = Date.now() - - return if lastChecked > now - $.DAY - - unless Object.keys(hiddenPosts.threads).length - $.set "hiddenPosts.#{g.BOARD}", hiddenPosts - return - - $.ajax "//api.4chan.org/#{g.BOARD}/catalog.json", onload: -> - threads = {} - for obj in JSON.parse @response - for thread in obj.threads - if thread.no of hiddenPosts.threads - threads[thread.no] = hiddenPosts.threads[thread.no] - hiddenPosts.threads = threads - $.set "hiddenPosts.#{g.BOARD}", hiddenPosts - menu: init: -> return if g.VIEW is 'catalog' or !Conf['Menu'] or !Conf['Reply Hiding'] @@ -2285,6 +2241,33 @@ Get = Main.callbackNodes Post, [post] Get.insert post, root, context +Misc = # super semantic + clearThreads: (key) -> + now = Date.now() + data = $.get key, threads: {} + + unless data.lastChecked + data.lastChecked = now + $.set key, data + return + + return if data.lastChecked > now - $.DAY + + data.lastChecked = now + + unless Object.keys(data.threads).length + $.set key, data + return + + $.ajax "//api.4chan.org/#{g.BOARD}/catalog.json", onload: -> + threads = {} + for obj in JSON.parse @response + for thread in obj.threads + if thread.no of data.threads + threads[thread.no] = data.threads[thread.no] + data.threads = threads + $.set key, data + Quotify = init: -> return if g.VIEW is 'catalog' or !Conf['Resurrect Quotes'] @@ -3288,6 +3271,7 @@ Unread = init: -> return if g.VIEW isnt 'thread' or !Conf['Unread Count'] and !Conf['Unread Tab Icon'] + Misc.clearThreads "lastReadPosts.#{g.BOARD}" Thread::callbacks.push name: 'Unread' cb: @node