stop onebox rapid fire

This commit is contained in:
Sam Saffron 2013-02-19 10:33:07 +11:00
parent 82132ab48b
commit 3be7bde401
2 changed files with 27 additions and 4 deletions

View File

@ -13,17 +13,26 @@ Discourse.Onebox = (->
null null
lookupCache = (url) -> lookupCache = (url) ->
localCache[url] cached = localCache[url]
if cached && cached.then # its a promise
null
else
cached
lookup = (url, refresh, callback) -> lookup = (url, refresh, callback) ->
cached = lookupCache(url) unless refresh cached = localCache[url]
cached = null if refresh && cached && !cached.then
if cached if cached
callback(cached) if cached.then
cached.then(callback(lookupCache(url)))
else
callback(cached)
return false return false
else else
$.get "/onebox", url: url, refresh: refresh, (html) -> cache(url, $.get "/onebox", url: url, refresh: refresh, (html) ->
cache(url,html) cache(url,html)
callback(html) callback(html)
)
return true return true
load = (e, refresh=false) -> load = (e, refresh=false) ->

View File

@ -0,0 +1,14 @@
describe "Discourse.Onebox", ->
beforeEach ->
spyOn($, 'ajax').andCallThrough()
it "Stops rapid calls with cache true", ->
Discourse.Onebox.lookup('http://bla.com', true, (c) -> c)
Discourse.Onebox.lookup('http://bla.com', true, (c) -> c)
expect($.ajax.calls.length).toBe(1)
it "Stops rapid calls with cache false", ->
Discourse.Onebox.lookup('http://bla.com/a', false, (c) -> c)
Discourse.Onebox.lookup('http://bla.com/a', false, (c) -> c)
expect($.ajax.calls.length).toBe(1)