Sha256: cd2b757b6f2123ca7329f3b0c554a5ca63912478612789d28407d401036b1b7f

Contents?: true

Size: 1.33 KB

Versions: 32

Compression:

Stored size: 1.33 KB

Contents

(angular => {
  'use strict';

  /* utilize lock service to ensure we don't make http request unless
     previous one has returned */

  class LockingPollService {

    constructor($lock, $interval) {
      this._intervalPromises = {};
      this._$lock = $lock;
      this._$interval = $interval;
      this.poll = _.wrap(this.pollWithFunction.bind(this), (func, resource, key, stopPolling) => {
        return func(() => resource.$get(), key, stopPolling);
      });
    }

    unPoll(key) {
      this._$interval.cancel(this._intervalPromises[key]);
      delete this._intervalPromises[key];
    }

    pollWithFunction(pollFunction, key, stopPolling) {
      this._intervalPromises[key] = this._$interval(() => {
        if (stopPolling()) {
          this.unPoll(key);
        } else {
          /* request http get for a resource inside a lock
              1) lock on key
              2) $get makes an http request for the resource
              3) (async) 'unlock' = callback function which releases the lock when the http request finishes */
          this._$lock.tryWithLockAsync(key, unlock => {
            pollFunction().then(unlock);
          });
        }
      }, 5000);
    }
  }

  LockingPollService.$inject = ['$lock', '$interval'];
  angular.module('alephServices.pollService', []).service('PollService', LockingPollService);
}(angular));

Version data entries

32 entries across 16 versions & 1 rubygems

Version Path
aleph_analytics-0.0.5 app/assets/javascripts/angular/services/lib/locking_poll_service.js.es6
aleph_analytics-0.0.5 public/assets/angular/services/lib/locking_poll_service.js-d0271cc07d20ec73291e7175e1714d42.es6
aleph_analytics-0.0.4 app/assets/javascripts/angular/services/lib/locking_poll_service.js.es6
aleph_analytics-0.0.4 public/assets/angular/services/lib/locking_poll_service.js-d0271cc07d20ec73291e7175e1714d42.es6
aleph_analytics-0.0.3 public/assets/angular/services/lib/locking_poll_service.js-d0271cc07d20ec73291e7175e1714d42.es6
aleph_analytics-0.0.3 app/assets/javascripts/angular/services/lib/locking_poll_service.js.es6
aleph_analytics-0.0.2 public/assets/angular/services/lib/locking_poll_service.js-d0271cc07d20ec73291e7175e1714d42.es6
aleph_analytics-0.0.2 app/assets/javascripts/angular/services/lib/locking_poll_service.js.es6
aleph_analytics-0.0.1.alpha app/assets/javascripts/angular/services/lib/locking_poll_service.js.es6
aleph_analytics-0.0.1.alpha public/assets/angular/services/lib/locking_poll_service.js-d0271cc07d20ec73291e7175e1714d42.es6
aleph_analytics-0.0.0.alpha public/assets/angular/services/lib/locking_poll_service.js-d0271cc07d20ec73291e7175e1714d42.es6
aleph_analytics-0.0.0.alpha app/assets/javascripts/angular/services/lib/locking_poll_service.js.es6