From e0cb19ac53f625cd6433ba73ea98d31660f58239 Mon Sep 17 00:00:00 2001 From: Mayhem Date: Sun, 11 Aug 2013 18:02:22 +0200 Subject: [PATCH] $.xhr will now deal with `Last-Modified` and `If-Modified-Since` headers itself. --- lib/$.coffee | 28 +++++++++++++++++----------- src/Monitoring/ThreadStats.coffee | 4 +--- src/Monitoring/ThreadUpdater.coffee | 11 ++--------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/$.coffee b/lib/$.coffee index 7e25bb5f2..0fc239340 100644 --- a/lib/$.coffee +++ b/lib/$.coffee @@ -36,17 +36,23 @@ $.extend = (object, properties) -> for key, val of properties object[key] = val return -$.ajax = (url, options, extra={}) -> - {type, headers, upCallbacks, form, sync} = extra - r = new XMLHttpRequest() - type or= form and 'post' or 'get' - r.open type, url, !sync - for key, val of headers - r.setRequestHeader key, val - $.extend r, options - $.extend r.upload, upCallbacks - r.send form - r +$.ajax = do -> + # Status Code 304: Not modified + # With the `If-Modified-Since` header we only receive the HTTP headers and no body for 304 responses. + # This saves a lot of bandwidth and CPU time for both the users and the servers. + lastModified = {} + (url, options, extra={}) -> + {type, whenModified, upCallbacks, form, sync} = extra + r = new XMLHttpRequest() + type or= form and 'post' or 'get' + r.open type, url, !sync + if whenModified + r.setRequestHeader 'If-Modified-Since', lastModified[url] or '0' + $.on r, 'load', -> lastModified[url] = r.getResponseHeader 'Last-Modified' + $.extend r, options + $.extend r.upload, upCallbacks + r.send form + r $.cache = do -> reqs = {} (url, cb, options) -> diff --git a/src/Monitoring/ThreadStats.coffee b/src/Monitoring/ThreadStats.coffee index cfe1658a7..6f35f6224 100644 --- a/src/Monitoring/ThreadStats.coffee +++ b/src/Monitoring/ThreadStats.coffee @@ -8,7 +8,6 @@ ThreadStats = @postCountEl = $ '#post-count', @dialog @fileCountEl = $ '#file-count', @dialog @pageCountEl = $ '#page-count', @dialog - @lastModified = '0' Thread::callbacks.push name: 'Thread Stats' @@ -41,9 +40,8 @@ ThreadStats = return setTimeout ThreadStats.fetchPage, 2 * $.MINUTE $.ajax "//api.4chan.org/#{ThreadStats.thread.board}/threads.json", onload: ThreadStats.onThreadsLoad, - headers: 'If-Modified-Since': ThreadStats.lastModified + whenModified: true onThreadsLoad: -> - ThreadStats.lastModified = @getResponseHeader 'Last-Modified' return if @status isnt 200 pages = JSON.parse @response for page in pages diff --git a/src/Monitoring/ThreadUpdater.coffee b/src/Monitoring/ThreadUpdater.coffee index ceb1a68bd..146027c65 100644 --- a/src/Monitoring/ThreadUpdater.coffee +++ b/src/Monitoring/ThreadUpdater.coffee @@ -25,7 +25,6 @@ ThreadUpdater = ThreadUpdater.root = @OP.nodes.root.parentNode ThreadUpdater.lastPost = +ThreadUpdater.root.lastElementChild.id.match(/\d+/)[0] ThreadUpdater.outdateCount = 0 - ThreadUpdater.lastModified = '0' for input in $$ 'input', ThreadUpdater.dialog if input.type is 'checkbox' @@ -95,7 +94,6 @@ ThreadUpdater = when 200 g.DEAD = false ThreadUpdater.parse JSON.parse(req.response).posts - ThreadUpdater.lastModified = req.getResponseHeader 'Last-Modified' ThreadUpdater.set 'timer', ThreadUpdater.getInterval() when 404 g.DEAD = true @@ -108,12 +106,7 @@ ThreadUpdater = thread: ThreadUpdater.thread else ThreadUpdater.outdateCount++ - ThreadUpdater.set 'timer', ThreadUpdater.getInterval() - ### - Status Code 304: Not modified - By sending the `If-Modified-Since` header we get a proper status code, and no response. - This saves bandwidth for both the user and the servers and avoid unnecessary computation. - ### + ThreadUpdater.set 'timer', ThreadUpdater.getInterval() [text, klass] = if req.status is 304 [null, null] else @@ -159,7 +152,7 @@ ThreadUpdater = ThreadUpdater.req.abort() url = "//api.4chan.org/#{ThreadUpdater.thread.board}/res/#{ThreadUpdater.thread}.json" ThreadUpdater.req = $.ajax url, onloadend: ThreadUpdater.cb.load, - headers: 'If-Modified-Since': ThreadUpdater.lastModified + whenModified: true updateThreadStatus: (title, OP) -> titleLC = title.toLowerCase()