Move If-Modified-Since code to separate function $.whenModified

This commit is contained in:
ccd0 2019-03-23 13:03:45 -07:00
parent 5a49280be4
commit e77fa69c53
5 changed files with 55 additions and 35 deletions

View File

@ -594,11 +594,11 @@ Index =
location.reload()
return
Index.req = $.ajax "#{location.protocol}//a.4cdn.org/#{g.BOARD}/catalog.json",
onloadend: Index.load
,
whenModified: 'Index'
bypassCache: true
Index.req = $.whenModified(
Site.urls.catalogJSON({boardID: g.BOARD.ID}),
'Index',
Index.load
)
$.addClass Index.button, 'fa-spin'
load: ->

View File

@ -75,9 +75,11 @@ ThreadStats =
$.addClass ThreadStats.pageCountEl, 'warning'
return
ThreadStats.timeout = setTimeout ThreadStats.fetchPage, 2 * $.MINUTE
$.ajax "#{location.protocol}//a.4cdn.org/#{ThreadStats.thread.board}/threads.json", onload: ThreadStats.onThreadsLoad,
whenModified: 'ThreadStats'
bypassCache: true
$.whenModified(
Site.urls.threadsListJSON({boardID: ThreadStats.thread.board}),
'ThreadStats',
ThreadStats.onThreadsLoad
)
onThreadsLoad: ->
if @status is 200

View File

@ -231,12 +231,12 @@ ThreadUpdater =
clearTimeout ThreadUpdater.timeoutID
ThreadUpdater.set 'timer', '...', 'loading'
ThreadUpdater.req?.abort()
ThreadUpdater.req = $.ajax "#{location.protocol}//a.4cdn.org/#{ThreadUpdater.thread.board}/thread/#{ThreadUpdater.thread}.json",
onloadend: ThreadUpdater.cb.load
timeout: $.MINUTE
,
whenModified: 'ThreadUpdater'
bypassCache: true
ThreadUpdater.req = $.whenModified(
Site.urls.threadJSON({boardID: ThreadUpdater.thread.board.ID, threadID: ThreadUpdater.thread.ID}),
'ThreadUpdater',
ThreadUpdater.cb.load,
{timeout: $.MINUTE}
)
updateThreadStatus: (type, status) ->
return if not (hasChanged = ThreadUpdater.thread["is#{type}"] isnt status)

View File

@ -174,7 +174,6 @@ ThreadWatcher =
if ThreadWatcher.requests.length is 0
ThreadWatcher.status.textContent = '...'
$.addClass ThreadWatcher.refreshButton, 'fa-spin'
ajax = if (siteID is Site.hostname) then $.ajax else CrossOrigin.ajax
onloadend = ->
return if @finished
@finished = true
@ -184,11 +183,19 @@ ThreadWatcher =
else
ThreadWatcher.status.textContent = "#{Math.round(ThreadWatcher.fetched / ThreadWatcher.requests.length * 100)}%"
cb.apply @, args
req = ajax url,
onloadend: onloadend
timeout: $.MINUTE
,
whenModified: if force then false else 'ThreadWatcher'
if siteID is Site.hostname
if force
delete $.lastModified.ThreadWatcher?[url]
req = $.whenModified(
url,
'ThreadWatcher',
onloadend,
{timeout: $.MINUTE}
)
else
req = CrossOrigin.ajax url,
onloadend: onloadend
timeout: $.MINUTE
ThreadWatcher.requests.push req
clearRequests: ->

View File

@ -41,34 +41,22 @@ $.extend = (object, properties) ->
return
$.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
pageXHR = XPCNativeWrapper window.wrappedJSObject.XMLHttpRequest
else
pageXHR = XMLHttpRequest
(url, options={}, extra={}) ->
{type, whenModified, bypassCache, upCallbacks, form} = extra
{type, upCallbacks, form, headers} = extra
options.responseType ?= 'json'
# 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/'
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()
type or= form and 'post' or 'get'
try
r.open type, url, true
if whenModified
r.setRequestHeader 'If-Modified-Since', lastModified[whenModified][url0] if lastModified[whenModified]?[url0]?
$.on r, 'load', -> (lastModified[whenModified] or= {})[url0] = r.getResponseHeader 'Last-Modified'
for key, value of (headers or {})
r.setRequestHeader key, value
$.extend r, options
$.extend r.upload, upCallbacks
# connection error or content blocker
@ -89,6 +77,29 @@ $.ajax = do ->
$.queueTask $.event, event, null, 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 ->
reqs = {}
$.cache = (url, cb, options={}) ->