Increase the notes polling timeout over time

A file called notes is loaded every ~15 seconds which checks for
updates to content on the page. This commit increases the polling
timeout over time (15, 30, 60, 120 seconds) and resets
it to 15 seconds if the AJAX call returns new notes

Fixes issue #13300
This commit is contained in:
Roberto Dip 2016-02-15 16:09:55 -03:00 committed by Alfredo Sumaran
parent ba869ae50d
commit 3fbdca214c

View file

@ -16,10 +16,12 @@ class @Notes
@view = view @view = view
@noteable_url = document.URL @noteable_url = document.URL
@notesCountBadge ||= $(".issuable-details").find(".notes-tab .badge") @notesCountBadge ||= $(".issuable-details").find(".notes-tab .badge")
@base_polling_interval = 15000
@limit_polling_interval = 120000
@cleanBinding() @cleanBinding()
@addBinding() @addBinding()
@initRefresh() @setPollingInterval()
@setupMainTargetNoteForm() @setupMainTargetNoteForm()
@initTaskList() @initTaskList()
@ -91,7 +93,7 @@ class @Notes
clearInterval(Notes.interval) clearInterval(Notes.interval)
Notes.interval = setInterval => Notes.interval = setInterval =>
@refresh() @refresh()
, 15000 , @polling_interval
refresh: -> refresh: ->
if not document.hidden and document.URL.indexOf(@noteable_url) is 0 if not document.hidden and document.URL.indexOf(@noteable_url) is 0
@ -105,12 +107,28 @@ class @Notes
success: (data) => success: (data) =>
notes = data.notes notes = data.notes
@last_fetched_at = data.last_fetched_at @last_fetched_at = data.last_fetched_at
@setPollingInterval(data.notes.length)
$.each notes, (i, note) => $.each notes, (i, note) =>
if note.discussion_with_diff_html? if note.discussion_with_diff_html?
@renderDiscussionNote(note) @renderDiscussionNote(note)
else else
@renderNote(note) @renderNote(note)
###
Increase @polling_interval up to 120 seconds on every function call,
if `shouldReset` has a truthy value, 'null' or 'undefined' the variable
will reset to @base_polling_interval.
Note: this function is used to gradually increase the polling interval
if there aren't new notes coming from the server
###
setPollingInterval: (shouldReset = true) ->
if shouldReset
@polling_interval = @base_polling_interval
else if @polling_interval < @limit_polling_interval
@polling_interval *= 2
@initRefresh()
### ###
Render note in main comments area. Render note in main comments area.