all files / app/components/waitIndicator/ waitingState.service.js

100% Statements 30/30
100% Branches 6/6
100% Functions 7/7
100% Lines 30/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                              486× 486× 486× 486×   486×                     470× 470× 470× 319× 470×     463× 463× 463×   463× 313×                       1878×     486×                           10× 10×     632×        
/**
 * @ngdoc service
 * @name app.components.waitingState
 * @description
 * Service to manage the waiting state of the application.
 */
 
(function () {
  'use strict';
 
  angular
    .module('app.components')
    .service('waitingState', Service);
 
 
  /** @ngInject */
  function Service($log, $rootScope) {
 
    var vm = this;
    vm.beginWait = beginWait;
    vm.waiting = waiting;
    vm.subscribeChanged = subscribeChanged;
 
    var waitingCount = 0;
 
    /**
     * @ngdoc function
     * @name beginWait
     * @methodOf app.components.waitingState
     * @description
     * Begin waiting
     * @returns {Function}
     * Function to end waiting
     */
    function beginWait() {
      var last = waiting();
      waitingCount++;
      if (last != waiting())
        changed();
      return endWait;
    }
 
    function endWait() {
      var last = waiting();
      waitingCount--;
      if (waitingCount < 0) {
        waitingCount = 0;
        $log.error('waitingCount < 0');
      }
      if (last != waiting())
        changed();
    }
 
    /**
     * @ngdoc function
     * @name waiting
     * @methodOf app.components.waitingState
     * @description
     * Indicate if the application is waiting or not.
     * @returns {Boolean}
     * Truthy if waiting
     */
    function waiting() {
      return waitingCount > 0;
    }
 
    var EVENT_NAME = 'wait-indicator:change';
 
    /**
     * @ngdoc function
     * @name subscribeChanged
     * @methodOf app.components.waitingState
     * @description
     * Provide a callback for a controller to be informed of a change to the
     * waiting state
     * @param {Object} scope
     * Controller scope
     * @param {Function} callback
     * Callback to inform of a change
     */
    function subscribeChanged(scope, callback) {
      var handler = $rootScope.$on(EVENT_NAME, callback);
      scope.$on('$destroy', handler);
    }
 
    function changed() {
      $rootScope.$emit(EVENT_NAME);
    }
  }
})();