Skip to content

Commit

Permalink
Merge 20ff874 into 5ef20b5
Browse files Browse the repository at this point in the history
  • Loading branch information
gugiserman committed Feb 27, 2015
2 parents 5ef20b5 + 20ff874 commit d866728
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,17 @@ myModule.config(function (pollerConfig) {
});
```

### Automatically stop all pollers when navigating between views
In order to automatically stop all pollers when navigating between views with multiple controllers, you can use `pollerConfig`.
```javascript
var myModule = angular.module('myApp', ['emguo.poller']);

myModule.config(function (pollerConfig) {
pollerConfig.stopOnStateChange = true; // If you use $stateProvider from ui-router.
pollerConfig.stopOnRouteChange = true; // If you use $routeProvider from ngRoute.
});
```

## Supported Angular versions

Angular Poller supports Angular 1.2.0 - 1.3.x.
Expand All @@ -341,4 +352,4 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
62 changes: 58 additions & 4 deletions angular-poller.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@
stopOnStateChange: false,
resetOnRouteChange: false,
resetOnStateChange: false,
delayOnVisibilityChange: false,
neverOverwrite: false
})

.run(function ($rootScope, poller, pollerConfig) {
.run(function ($rootScope, $document, poller, pollerConfig) {

/**
* Automatically stop or reset all pollers before route change ($routeProvider) or state change ($stateProvider).
Expand Down Expand Up @@ -72,6 +73,28 @@
poller.reset();
});
}

if (pollerConfig.delayOnVisibilityChange) {
var handleVisibility = function(isHidden) {
if (isHidden || document.hidden || document.webkitHidden || document.mozHidden || document.msHidden) {
poller.delayAll();
}
else {
poller.resetDelay();
}
};

$document.on('visibilitychange', function() {
$rootScope.$broadcast('$visibilityChange');
});

$rootScope.$on('$visibilityChange', function(event, isHidden) {
handleVisibility(isHidden);
});

handleVisibility();
}

})

.factory('poller', function ($interval, $q, $http, pollerConfig) {
Expand All @@ -82,6 +105,7 @@
action: 'get',
argumentsArray: [],
delay: 5000,
idleDelay: 0,
smart: false,
catchError: false
},
Expand Down Expand Up @@ -140,10 +164,11 @@
* @param options
*/
set: function (options) {

angular.forEach(['action', 'argumentsArray', 'delay', 'smart', 'catchError'], function (prop) {
angular.forEach(['action', 'argumentsArray', 'delay', 'normalDelay', 'idleDelay', 'smart', 'catchError'], function (prop) {
if (options && options[prop]) {
this[prop] = options[prop];
} else if (prop === 'normalDelay') {
this[prop] = this.delay;
} else if (!this[prop]) {
this[prop] = defaults[prop];
}
Expand All @@ -165,6 +190,10 @@
current,
timestamp;

if (!delay) {
return;
}

this.deferred = this.deferred || $q.defer();

/**
Expand Down Expand Up @@ -308,8 +337,33 @@
reset: function () {
this.stopAll();
pollers = [];
},

/**
* Switch all poller services delay to idleDelay
*/
delayAll: function() {
angular.forEach(pollers, function (p) {
if (p.idleDelay !== false && angular.isDefined(p.idleDelay)) {
p.delay = p.idleDelay;
p.restart();
}
});
},

/**
* Switch all poller services delay back to normalDelay
*/
resetDelay: function() {
angular.forEach(pollers, function(p) {
if (p.idleDelay !== false && angular.isDefined(p.idleDelay)) {
p.delay = p.normalDelay;
p.restart();
}
});
}

};
}
);
})(window, window.angular);
})(window, window.angular);
4 changes: 2 additions & 2 deletions angular-poller.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 34 additions & 1 deletion test/angular-poller-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,16 @@ describe('emguo.poller', function () {
expect(anotherPoller.delay).to.equal(1000);
});

it('should always set (and overwrite) normalDelay same as delay.', function() {
anotherPoller = poller.get(target, {delay: 1000});
expect(anotherPoller.normalDelay).to.exist();
expect(anotherPoller.normalDelay).to.equal(anotherPoller.delay);

poller.get(target, {delay: 3000});
expect(anotherPoller.normalDelay).to.exist();
expect(anotherPoller.normalDelay).to.equal(anotherPoller.delay);
});

it('should not modify delay property if it is not re-defined.', function () {
anotherPoller = poller.get(target);
expect(anotherPoller.delay).to.equal(8000);
Expand Down Expand Up @@ -639,4 +649,27 @@ describe('emguo.poller PollerConfig', function () {
expect(anotherPoller.argumentsArray.length).to.equal(0);
expect(anotherPoller.delay).to.equal(5000);
});
});

it('should switch delay of all pollers that have idleDelay on visibilitychange.', function () {
module(function ($provide) {
$provide.constant('pollerConfig', {
delayOnVisibilityChange: true
});
});

inject(function (_$rootScope_, _$resource_, _poller_) {
$rootScope = _$rootScope_;
$resource = _$resource_;
poller = _poller_;

var delaySpy = sinon.spy(poller, 'delayAll');
$rootScope.$broadcast('$visibilityChange', true);

var hasteSpy = sinon.spy(poller, 'resetDelay');
$rootScope.$broadcast('$visibilityChange', false);

expect(delaySpy).to.have.callCount(1);
expect(hasteSpy).to.have.callCount(1);
});
});
});

0 comments on commit d866728

Please sign in to comment.