Skip to content

FIX: cancel $timeout promises when calling clearAll (related to #78) #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions demo/clear_all.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<html ng-app="notificationTest">
<head>
<title>Notification demo (kill message)</title>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css">
<link rel="stylesheet" href="angular-csp.css">
<link rel="stylesheet" href="angular-ui-notification.min.css">
</head>
<body>
<div class="container">
<h1>Angular ui-notification demo (clear all messages)</h1>
</div>

<div ng-controller="notificationController">

<div class="container">
<h3>Clear All messages</h3>
<div class="btn-group-vertical">
<button class="btn btn-success clear-all" ng-click="clearAllMessages()">Clear all message with fadeout</button>
</div>
</div>

</div>

<script src="angular.min.js"></script>
<script src="angular-ui-notification.min.js"></script>
<script type="text/javascript">
angular.module('notificationTest', ['ui-notification'])
.config(function(NotificationProvider) {
NotificationProvider.setOptions({
delay: 10000,
});
});

angular.module('notificationTest')

.controller('notificationController', function($scope, Notification, $timeout) {

$scope.clearAllMessages = function() {
Notification.warning("Message should be cleared");
var test = Notification.primary("Message should be cleared too...");
Notification.info("This message also should be cleared");

$timeout(function() {
Notification.clearAll();
}, 1000);
};

});
</script>
</body>
</html>
13 changes: 12 additions & 1 deletion dist/angular-ui-notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ angular.module('ui-notification',[]);

angular.module('ui-notification').provider('Notification', function() {

var $timeoutPromises = [];

this.options = {
delay: 5000,
startTop: 10,
Expand Down Expand Up @@ -136,9 +138,14 @@ angular.module('ui-notification').provider('Notification', function() {
templateElement.bind('webkitTransitionEnd oTransitionEnd otransitionend transitionend msTransitionEnd', closeEvent);

if (angular.isNumber(args.delay)) {
$timeout(function() {
var $timeoutPromise = $timeout(function() {
templateElement.addClass('killed');
var promiseIndex = $timeoutPromises.indexOf($timeoutPromise);
if(promiseIndex !== -1) {
$timeoutPromises.splice(promiseIndex, 1);
}
}, args.delay);
$timeoutPromises.push($timeoutPromise);
}

setCssTransitions('none');
Expand Down Expand Up @@ -218,6 +225,10 @@ angular.module('ui-notification').provider('Notification', function() {
angular.forEach(messageElements, function(element) {
element.addClass('killed');
});
angular.forEach($timeoutPromises, function($timeoutPromise) {
$timeout.cancel($timeoutPromise);
});
$timeoutPromises = [];
};

return notify;
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-ui-notification.min.js

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

13 changes: 12 additions & 1 deletion src/angular-ui-notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ angular.module('ui-notification',[]);

angular.module('ui-notification').provider('Notification', function() {

var $timeoutPromises = [];

this.options = {
delay: 5000,
startTop: 10,
Expand Down Expand Up @@ -129,9 +131,14 @@ angular.module('ui-notification').provider('Notification', function() {
templateElement.bind('webkitTransitionEnd oTransitionEnd otransitionend transitionend msTransitionEnd', closeEvent);

if (angular.isNumber(args.delay)) {
$timeout(function() {
var $timeoutPromise = $timeout(function() {
templateElement.addClass('killed');
var promiseIndex = $timeoutPromises.indexOf($timeoutPromise);
if(promiseIndex !== -1) {
$timeoutPromises.splice(promiseIndex, 1);
}
}, args.delay);
$timeoutPromises.push($timeoutPromise);
}

setCssTransitions('none');
Expand Down Expand Up @@ -211,6 +218,10 @@ angular.module('ui-notification').provider('Notification', function() {
angular.forEach(messageElements, function(element) {
element.addClass('killed');
});
angular.forEach($timeoutPromises, function($timeoutPromise) {
$timeout.cancel($timeoutPromise);
});
$timeoutPromises = [];
};

return notify;
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,22 @@ describe("E2E: Max count", function() {
});

});
});

describe("E2E: check if protractor don't wait $timeout after calling clearAll", function() {
beforeEach(function() {
browser.ignoreSynchronization = true;
browser.driver.get('http://localhost:8080/clear_all.html');
});

describe('Click on button should generate several notifications and kill them al after 1 sec', function() {

it("should click on button and check if all message disappears", function() {
element(by.css('button.btn-success.clear-all')).click();
expect(element.all(by.css('.ui-notification')).count()).toBe(3);
browser.sleep(1200);
browser.ignoreSynchronization = false; // protractor shouldn't wait 10 secs
});

})
});