amatriain/openreader

View on GitHub
FeedBunch-app/app/assets/javascripts/ng-services/ng-ExportStateSvc.js.coffee.erb

Summary

Maintainability
Test Coverage
########################################################
# AngularJS service to load data export state data in the scope.
########################################################

angular.module('feedbunch').service 'exportStateSvc',
['$rootScope', '$timeout', 'csrfTokenSvc', 'timerFlagSvc',
($rootScope, $timeout, csrfTokenSvc, timerFlagSvc)->

  # Flag to indicate if a message should appear when the export job finishes running.
  show_finished_message = false

  # Constants for the different operations the web worker can perform
  LOAD_JOB_STATE = 'load_job_state'
  HIDE_JOB_ALERT = 'hide_job_alert'

  # CSRF token
  token = csrfTokenSvc.get_token()

  # Web worker to retrieve state of export job, or permanently hide the export job alert
  worker = new Worker '<%= asset_path 'workers/export_state_worker' %>'
  worker.onmessage = (e) ->
    if (e.data.status == 200 || e.data.status == 304) && e.data.operation == LOAD_JOB_STATE
      export_state_loaded e.data.response
    else if e.data.status == 401 || e.data.status == 422
      $window.location.href = '/login'
    else
      if e.data.operation == LOAD_JOB_STATE
        timerFlagSvc.start 'error_exporting'
    $rootScope.$digest()

  #---------------------------------------------
  # PRIVATE FUNCTION: Operations after the export job state has been loaded
  #---------------------------------------------
  export_state_loaded = (response)->
    $rootScope.show_export_alert = response.show_alert
    $rootScope.export_date = response.export_date
    $rootScope.export_state = response.state
    if response.state == "RUNNING"
      # Update state from the server periodically while export is running
      $timeout ->
        load_export_state true
      , 5000
    else if response.state == "ERROR" && show_finished_message
      timerFlagSvc.start 'error_exporting'
    else if response.state == "SUCCESS" && show_finished_message
      timerFlagSvc.start 'success_exporting'

  #---------------------------------------------
  # PRIVATE FUNCTION: load export process state via AJAX
  #
  # Note.- The first time this is invoked on page load by the angular controller, passing
  # a false to the "show_message" argument. This means that if the export is not running
  # (error, success or none, doesn't matter), no message alert will be shown.
  #
  # However if the export is found to be running, the function will be called every 5 seconds, with
  # a true to the "show_message" argument.
  #
  # Basically this means that if when the page is loaded the export is running, and it finishes
  # afterwards, then and only then will a message be displayed.
  #---------------------------------------------
  load_export_state = (show_message=false)->
    show_finished_message = show_message
    worker.postMessage {operation: LOAD_JOB_STATE, token: token}

  service =

    #---------------------------------------------
    # Load export process state via AJAX into the root scope
    #---------------------------------------------
    load_data: load_export_state

    #---------------------------------------------
    # Hide the export state alert and notify the server via AJAX that it should not be displayed again.
    #---------------------------------------------
    hide_alert: ->
      $rootScope.show_export_alert = false
      worker.postMessage {operation: HIDE_JOB_ALERT, token: token}

  return service
]