Sha256: 43bfecaf8c3e6f25799b1856c7b6131154415c7aed6859b280728ba19f917c01

Contents?: true

Size: 1.77 KB

Versions: 32

Compression:

Stored size: 1.77 KB

Contents

class PageUpdater
  DEBOUNCE = 100
  RETRY_DELAY = 5000
  MAX_RETRIES = 5

  @callbacks: []
  @afterUpdate: (callback) ->
    @callbacks.push(callback)

  constructor: (@channel, @selectors) ->
    @parser = new DOMParser()
    @source = @listen()
    @previousLastModified = null

  requestUpdate: =>
    @updateRequested = true
    @scheduleUpdate()

  scheduleUpdate: =>
    return if @updateScheduled
    return unless @updateRequested
    setTimeout(@fetchPage, DEBOUNCE)
    @updateScheduled = true

  fetchPage: (message) =>
    @updateRequested = false
    jQuery.get(window.location.toString()).done(@updatePage).fail(=> @updateScheduled = false)

  updatePage: (html, status, response) =>
    lastModified = response.getResponseHeader('last-modified')
    if lastModified? and lastModified != @previousLastModified
      @previousLastModified = lastModified

      newDocument = @parser.parseFromString(html, 'text/html')
      for selector in @selectors
        $(selector).html(newDocument.querySelectorAll("#{selector} > *"))
      for callback in PageUpdater.callbacks
        callback()

    @updateScheduled = false

  listen: ->
    @source = new EventSource(@channel)
    @source.addEventListener('update', @requestUpdate)
    @retries = MAX_RETRIES
    @interval = setInterval =>
      switch @source.readyState
        when @source.CLOSED
          clearInterval(@interval)
          if @retries > 0
            @retries -= 1
            @listen()
        else
          @retries = MAX_RETRIES
    , RETRY_DELAY

jQuery ($) ->
  PageUpdater.afterUpdate -> $('time[data-time-ago]').timeago()

  channel = $('meta[name=subscription-channel]').attr('content')
  selectors = (e.content for e in $('meta[name=subscription-selector]'))
  if channel and selectors
    new PageUpdater(channel, selectors)

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
shipit-engine-0.39.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.38.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.37.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.36.1 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.36.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.35.1 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.35.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.34.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.33.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.32.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.31.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.30.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.29.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.28.1 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.28.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.27.1 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.27.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.26.0 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.25.1 app/assets/javascripts/shipit/page_updater.js.coffee
shipit-engine-0.25.0 app/assets/javascripts/shipit/page_updater.js.coffee