amatriain/feedbunch

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

Summary

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

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

  # CSRF token
  token = csrfTokenSvc.get_token()

  # Web worker to retrieve user data.
  worker = new Worker '<%= asset_path 'workers/load_user_data_worker' %>'
  worker.onmessage = (e) ->
    if e.data.status == 200 || e.data.status == 304
      $rootScope.subscribed_feeds_count = e.data.response.subscribed_feeds_count
    else if e.data.status == 401 || e.data.status == 422
      $window.location.href = '/login'
    else
      timerFlagSvc.start 'error_loading_user_data'
    $rootScope.$digest()

  service =

    #---------------------------------------------
    # Load user configuration into the root scope
    #---------------------------------------------
    load_data: ->
      worker.postMessage {token: token}

  return service
]