Sha256: 025901d4eaba8254cf020a32fcfe23d3f502eb259d7e9e502dc773a03d8d5c08

Contents?: true

Size: 1.58 KB

Versions: 40

Compression:

Stored size: 1.58 KB

Contents

class Chunk
  constructor: (@raw) ->

  rawText: ->
    @raw

  text: ->
    @_text ||= AnsiStream.strip(@raw)

  rawLines: ->
    @_rawLines ||= @splitLines(@raw)

  lines: ->
    @_lines ||= @splitLines(@text())

  splitLines: (text) ->
    lines = text.split(/\r?\n/)
    lines.pop() unless lines[lines.length - 1]
    lines

class @Stream
  INTERVAL = 1000
  MAX_RETRIES = 15

  constructor: ->
    @url = null
    @eventListeners = {}
    @retries = 0
    @status = 'running'

  init: ({url, text, status}) ->
    @status = status
    @broadcastOutput(text)
    @start(url)

  poll: =>
    jQuery.ajax @url,
      success: @success
      error: @error

  success: (response) =>
    @retries = 0
    @broadcastOutput(response.output, response)
    @broadcastStatus(response.status, response)
    @start(response.url || false)

  broadcastStatus: (status, args...) ->
    if status != @status
      @status = status
      for handler in @listeners('status')
        try
          handler(status, args...)
        catch error
          console?.log("Plugin error: #{error}")

  broadcastOutput: (raw, args...) ->
    return unless raw

    chunk = new Chunk(raw)
    for handler in @listeners('chunk')
      try
        handler(chunk, args...)
      catch error
        console?.log("Plugin error: #{error}")

  error: (response) =>
    @start() if 600 > response.status >= 500 && (@retries += 1) < MAX_RETRIES

  start: (url = @url) ->
    if @url = url
      setTimeout(@poll, INTERVAL)

  addEventListener: (type, handler) ->
    @listeners(type).push(handler)

  listeners: (type) ->
    @eventListeners[type] ||= []

Version data entries

40 entries across 40 versions & 1 rubygems

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