Skip to content

Commit

Permalink
Resolved #17 - Added resetOnRouteChange, resetOnStateChange and never…
Browse files Browse the repository at this point in the history
…Overwrite to pollerConfig.
  • Loading branch information
emmaguo committed Nov 13, 2014
1 parent f1bc700 commit d42d62c
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 16 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ Demo site: http://emmaguo.github.io/angular-poller/
- [Multiple pollers](#multiple-pollers)
- [Multiple controllers](#multiple-controllers)
- [Only send new request if the previous one is resolved](#only-send-new-request-if-the-previous-one-is-resolved)
- [Always create new poller on calling poller.get](#always-create-new-poller-on-calling-poller-get)
- [Automatically stop all pollers when navigating between views](#automatically-stop-all-pollers-when-navigating-between-views)
- [Automatically reset all pollers when navigating between views](#automatically-reset-all-pollers-when-navigating-between-views)
- [Supported Angular versions](#supported-angular-versions)
- [License](#license)

Expand Down Expand Up @@ -275,6 +277,22 @@ var myPoller = poller.get(myTarget, {
});
```

### Always create new poller on calling poller.get
By default `poller.get(target, ...)` looks for any existing poller by `target` in poller registry. If found, it overwrites
existing poller with new parameters such as `action`, `delay`, `argumentsArray` etc if specified, and then restarts the poller.
If not found, it creates and starts a new poller. It means you will never have two pollers running against the same target.

But if you do want to have more than one poller running against the same target, you can force poller to always create new
poller on calling `poller.get` like so:

```javascript
var myModule = angular.module('myApp', ['emguo.poller']);

myModule.config(function (pollerConfig) {
pollerConfig.neverOverwrite = true;
});
```

### 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
Expand All @@ -286,6 +304,18 @@ myModule.config(function (pollerConfig) {
});
```

### Automatically reset all pollers when navigating between views
You can also use `pollerConfig` to automatically reset all pollers when navigating between views with multiple controllers.
It empties poller registry in addition to stopping all pollers. It means `poller.get` will always create a new poller.
```javascript
var myModule = angular.module('myApp', ['emguo.poller']);

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

## Supported Angular versions

Angular Poller supports Angular 1.2.0 - 1.3.x.
Expand Down
40 changes: 29 additions & 11 deletions angular-poller.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,43 @@

.constant('pollerConfig', {
stopOnRouteChange: false,
stopOnStateChange: false
stopOnStateChange: false,
resetOnRouteChange: false,
resetOnStateChange: false,
neverOverwrite: false
})

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

/**
* Automatically stop all pollers before route change ($routeProvider) or state change ($stateProvider).
* Automatically stop or reset all pollers before route change ($routeProvider) or state change ($stateProvider).
*/
if (pollerConfig.stopOnRouteChange) {

$rootScope.$on('$routeChangeStart', function () {
poller.stopAll();
});
} else if (pollerConfig.stopOnStateChange) {
}

if (pollerConfig.stopOnStateChange) {
$rootScope.$on('$stateChangeStart', function () {
poller.stopAll();
});
}

if (pollerConfig.resetOnRouteChange) {
$rootScope.$on('$routeChangeStart', function () {
poller.reset();
});
}

if (pollerConfig.resetOnStateChange) {
$rootScope.$on('$stateChangeStart', function () {
poller.reset();
});
}
})

.factory('poller', function ($interval, $q, $http) {
.factory('poller', function ($interval, $q, $http, pollerConfig) {

var pollers = [], // Poller registry

Expand Down Expand Up @@ -92,7 +107,8 @@
},

/**
* Find poller by target in poller registry.
* Find poller by target in poller registry if pollerConfig.neverOverwrite is set to false (default).
* Otherwise return null to prevent overwriting existing pollers.
*
* @param target
* @returns {object}
Expand All @@ -101,11 +117,13 @@

var poller = null;

angular.forEach(pollers, function (item) {
if (angular.equals(item.target, target)) {
poller = item;
}
});
if (!pollerConfig.neverOverwrite) {
angular.forEach(pollers, function (item) {
if (angular.equals(item.target, target)) {
poller = item;
}
});
}

return poller;
};
Expand Down
73 changes: 68 additions & 5 deletions test/angular-poller-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,17 +535,16 @@ describe('emguo.poller', function () {

describe('emguo.poller PollerConfig', function () {

var $rootScope, poller, spy;
var $rootScope, $resource, poller, spy;

beforeEach(function () {
module('emguo.poller');
module('emguo.poller', 'ngResource');
});

it('should stop all pollers on $routeChangeStart if pollerConfig.stopOnRouteChange is true.', function () {
module(function ($provide) {
$provide.constant('pollerConfig', {
stopOnRouteChange: true,
stopOnStateChange: false
stopOnRouteChange: true
});
});

Expand All @@ -562,7 +561,6 @@ describe('emguo.poller PollerConfig', function () {
it('should stop all pollers on $stateChangeStart if pollerConfig.stopOnStateChange is true.', function () {
module(function ($provide) {
$provide.constant('pollerConfig', {
stopOnRouteChange: false,
stopOnStateChange: true
});
});
Expand All @@ -576,4 +574,69 @@ describe('emguo.poller PollerConfig', function () {
$rootScope.$broadcast('$stateChangeStart');
expect(spy).to.have.callCount(1);
});

it('should reset all pollers on $routeChangeStart if pollerConfig.resetOnRouteChange is true.', function () {
module(function ($provide) {
$provide.constant('pollerConfig', {
resetOnRouteChange: true
});
});

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

spy = sinon.spy(poller, 'reset');
$rootScope.$broadcast('$routeChangeStart');
expect(spy).to.have.callCount(1);
});

it('should reset all pollers on $stateChangeStart if pollerConfig.resetOnStateChange is true.', function () {
module(function ($provide) {
$provide.constant('pollerConfig', {
resetOnStateChange: true
});
});

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

spy = sinon.spy(poller, 'reset');
$rootScope.$broadcast('$stateChangeStart');
expect(spy).to.have.callCount(1);
});

it('should always create new poller if pollerConfig.neverOverwrite is true.', function () {
module(function ($provide) {
$provide.constant('pollerConfig', {
neverOverwrite: true
});
});

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

var target = $resource('/users');
var myPoller = poller.get(target, {
action: 'query',
argumentsArray: [
{
group: 1
}
],
delay: 8000
});
var anotherPoller = poller.get(target);

expect(anotherPoller).to.not.equal(myPoller);
expect(anotherPoller.action).to.equal('get');
expect(anotherPoller.argumentsArray.length).to.equal(0);
expect(anotherPoller.delay).to.equal(5000);
});
});

0 comments on commit d42d62c

Please sign in to comment.