Move If-Modified-Since code to separate function $.whenModified
This commit is contained in:
parent
5a49280be4
commit
e77fa69c53
@ -594,11 +594,11 @@ Index =
|
|||||||
location.reload()
|
location.reload()
|
||||||
return
|
return
|
||||||
|
|
||||||
Index.req = $.ajax "#{location.protocol}//a.4cdn.org/#{g.BOARD}/catalog.json",
|
Index.req = $.whenModified(
|
||||||
onloadend: Index.load
|
Site.urls.catalogJSON({boardID: g.BOARD.ID}),
|
||||||
,
|
'Index',
|
||||||
whenModified: 'Index'
|
Index.load
|
||||||
bypassCache: true
|
)
|
||||||
$.addClass Index.button, 'fa-spin'
|
$.addClass Index.button, 'fa-spin'
|
||||||
|
|
||||||
load: ->
|
load: ->
|
||||||
|
|||||||
@ -75,9 +75,11 @@ ThreadStats =
|
|||||||
$.addClass ThreadStats.pageCountEl, 'warning'
|
$.addClass ThreadStats.pageCountEl, 'warning'
|
||||||
return
|
return
|
||||||
ThreadStats.timeout = setTimeout ThreadStats.fetchPage, 2 * $.MINUTE
|
ThreadStats.timeout = setTimeout ThreadStats.fetchPage, 2 * $.MINUTE
|
||||||
$.ajax "#{location.protocol}//a.4cdn.org/#{ThreadStats.thread.board}/threads.json", onload: ThreadStats.onThreadsLoad,
|
$.whenModified(
|
||||||
whenModified: 'ThreadStats'
|
Site.urls.threadsListJSON({boardID: ThreadStats.thread.board}),
|
||||||
bypassCache: true
|
'ThreadStats',
|
||||||
|
ThreadStats.onThreadsLoad
|
||||||
|
)
|
||||||
|
|
||||||
onThreadsLoad: ->
|
onThreadsLoad: ->
|
||||||
if @status is 200
|
if @status is 200
|
||||||
|
|||||||
@ -231,12 +231,12 @@ ThreadUpdater =
|
|||||||
clearTimeout ThreadUpdater.timeoutID
|
clearTimeout ThreadUpdater.timeoutID
|
||||||
ThreadUpdater.set 'timer', '...', 'loading'
|
ThreadUpdater.set 'timer', '...', 'loading'
|
||||||
ThreadUpdater.req?.abort()
|
ThreadUpdater.req?.abort()
|
||||||
ThreadUpdater.req = $.ajax "#{location.protocol}//a.4cdn.org/#{ThreadUpdater.thread.board}/thread/#{ThreadUpdater.thread}.json",
|
ThreadUpdater.req = $.whenModified(
|
||||||
onloadend: ThreadUpdater.cb.load
|
Site.urls.threadJSON({boardID: ThreadUpdater.thread.board.ID, threadID: ThreadUpdater.thread.ID}),
|
||||||
timeout: $.MINUTE
|
'ThreadUpdater',
|
||||||
,
|
ThreadUpdater.cb.load,
|
||||||
whenModified: 'ThreadUpdater'
|
{timeout: $.MINUTE}
|
||||||
bypassCache: true
|
)
|
||||||
|
|
||||||
updateThreadStatus: (type, status) ->
|
updateThreadStatus: (type, status) ->
|
||||||
return if not (hasChanged = ThreadUpdater.thread["is#{type}"] isnt status)
|
return if not (hasChanged = ThreadUpdater.thread["is#{type}"] isnt status)
|
||||||
|
|||||||
@ -174,7 +174,6 @@ ThreadWatcher =
|
|||||||
if ThreadWatcher.requests.length is 0
|
if ThreadWatcher.requests.length is 0
|
||||||
ThreadWatcher.status.textContent = '...'
|
ThreadWatcher.status.textContent = '...'
|
||||||
$.addClass ThreadWatcher.refreshButton, 'fa-spin'
|
$.addClass ThreadWatcher.refreshButton, 'fa-spin'
|
||||||
ajax = if (siteID is Site.hostname) then $.ajax else CrossOrigin.ajax
|
|
||||||
onloadend = ->
|
onloadend = ->
|
||||||
return if @finished
|
return if @finished
|
||||||
@finished = true
|
@finished = true
|
||||||
@ -184,11 +183,19 @@ ThreadWatcher =
|
|||||||
else
|
else
|
||||||
ThreadWatcher.status.textContent = "#{Math.round(ThreadWatcher.fetched / ThreadWatcher.requests.length * 100)}%"
|
ThreadWatcher.status.textContent = "#{Math.round(ThreadWatcher.fetched / ThreadWatcher.requests.length * 100)}%"
|
||||||
cb.apply @, args
|
cb.apply @, args
|
||||||
req = ajax url,
|
if siteID is Site.hostname
|
||||||
onloadend: onloadend
|
if force
|
||||||
timeout: $.MINUTE
|
delete $.lastModified.ThreadWatcher?[url]
|
||||||
,
|
req = $.whenModified(
|
||||||
whenModified: if force then false else 'ThreadWatcher'
|
url,
|
||||||
|
'ThreadWatcher',
|
||||||
|
onloadend,
|
||||||
|
{timeout: $.MINUTE}
|
||||||
|
)
|
||||||
|
else
|
||||||
|
req = CrossOrigin.ajax url,
|
||||||
|
onloadend: onloadend
|
||||||
|
timeout: $.MINUTE
|
||||||
ThreadWatcher.requests.push req
|
ThreadWatcher.requests.push req
|
||||||
|
|
||||||
clearRequests: ->
|
clearRequests: ->
|
||||||
|
|||||||
@ -41,34 +41,22 @@ $.extend = (object, properties) ->
|
|||||||
return
|
return
|
||||||
|
|
||||||
$.ajax = do ->
|
$.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 = {}
|
|
||||||
if window.wrappedJSObject and not XMLHttpRequest.wrappedJSObject
|
if window.wrappedJSObject and not XMLHttpRequest.wrappedJSObject
|
||||||
pageXHR = XPCNativeWrapper window.wrappedJSObject.XMLHttpRequest
|
pageXHR = XPCNativeWrapper window.wrappedJSObject.XMLHttpRequest
|
||||||
else
|
else
|
||||||
pageXHR = XMLHttpRequest
|
pageXHR = XMLHttpRequest
|
||||||
|
|
||||||
(url, options={}, extra={}) ->
|
(url, options={}, extra={}) ->
|
||||||
{type, whenModified, bypassCache, upCallbacks, form} = extra
|
{type, upCallbacks, form, headers} = extra
|
||||||
options.responseType ?= 'json'
|
options.responseType ?= 'json'
|
||||||
# XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310
|
# XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310
|
||||||
url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?4c(?:ha|d)n\.org)\/adv\//, '$1//adv/'
|
url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?4c(?:ha|d)n\.org)\/adv\//, '$1//adv/'
|
||||||
if whenModified
|
|
||||||
params = []
|
|
||||||
# XXX https://bugs.chromium.org/p/chromium/issues/detail?id=643659
|
|
||||||
params.push "s=#{whenModified}" if $.engine is 'blink'
|
|
||||||
params.push "t=#{Date.now()}" if Site.software is 'yotsuba' and bypassCache
|
|
||||||
url0 = url
|
|
||||||
url += '?' + params.join('&') if params.length
|
|
||||||
r = new pageXHR()
|
r = new pageXHR()
|
||||||
type or= form and 'post' or 'get'
|
type or= form and 'post' or 'get'
|
||||||
try
|
try
|
||||||
r.open type, url, true
|
r.open type, url, true
|
||||||
if whenModified
|
for key, value of (headers or {})
|
||||||
r.setRequestHeader 'If-Modified-Since', lastModified[whenModified][url0] if lastModified[whenModified]?[url0]?
|
r.setRequestHeader key, value
|
||||||
$.on r, 'load', -> (lastModified[whenModified] or= {})[url0] = r.getResponseHeader 'Last-Modified'
|
|
||||||
$.extend r, options
|
$.extend r, options
|
||||||
$.extend r.upload, upCallbacks
|
$.extend r.upload, upCallbacks
|
||||||
# connection error or content blocker
|
# connection error or content blocker
|
||||||
@ -89,6 +77,29 @@ $.ajax = do ->
|
|||||||
$.queueTask $.event, event, null, r
|
$.queueTask $.event, event, null, r
|
||||||
r
|
r
|
||||||
|
|
||||||
|
# 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 = {}
|
||||||
|
$.whenModified = (url, bucket, cb, options={}) ->
|
||||||
|
{timeout} = options
|
||||||
|
params = []
|
||||||
|
# XXX https://bugs.chromium.org/p/chromium/issues/detail?id=643659
|
||||||
|
params.push "s=#{bucket}" if $.engine is 'blink'
|
||||||
|
params.push "t=#{Date.now()}" if Site.software is 'yotsuba'
|
||||||
|
url0 = url
|
||||||
|
url += '?' + params.join('&') if params.length
|
||||||
|
headers = {}
|
||||||
|
if (t = $.lastModified[bucket]?[url0])?
|
||||||
|
headers['If-Modified-Since'] = t
|
||||||
|
r = $.ajax url, {
|
||||||
|
onloadend: ->
|
||||||
|
($.lastModified[bucket] or= {})[url0] = @getResponseHeader('Last-Modified')
|
||||||
|
cb.call @
|
||||||
|
timeout
|
||||||
|
}, {headers}
|
||||||
|
r
|
||||||
|
|
||||||
do ->
|
do ->
|
||||||
reqs = {}
|
reqs = {}
|
||||||
$.cache = (url, cb, options={}) ->
|
$.cache = (url, cb, options={}) ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user