Skip to content

Commit

Permalink
Refactor: Use animationend event to remove notification
Browse files Browse the repository at this point in the history
Much more stable than setTimeout and no longer blocks tests
  • Loading branch information
pichfl committed Dec 15, 2021
1 parent e563265 commit b4cf56a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
16 changes: 15 additions & 1 deletion addon/components/notification-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export default class NotificationMessage extends Component {
removeNotification(event) {
event.preventDefault();
event.stopPropagation();
this.notifications.removeNotification(this.notification);

this.notifications.dismissNotification(this.notification);
}

@action
Expand All @@ -110,4 +111,17 @@ export default class NotificationMessage extends Component {
this.notifications.setupAutoClear(notification);
}
}

@action
handleAnimationEnd({ animationName }) {
let notification = this.notification;

if (!notification.dismiss) {
return;
}

if (animationName.endsWith('-out')) {
this.notifications.removeNotification(notification);
}
}
}
19 changes: 7 additions & 12 deletions addon/services/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,16 @@ export default class NotificationsService extends Service {
});
}

dismissNotification(notification) {
set(notification, 'dismiss', true);
}

removeNotification(notification) {
if (!notification) {
return;
}

notification.set('dismiss', true);

// Delay removal from DOM for dismissal animation
later(
this,
() => {
this.content.removeObject(notification);
},
500
);
this.content.removeObject(notification);
}

setupAutoClear(notification) {
Expand All @@ -96,7 +91,7 @@ export default class NotificationsService extends Service {
() => {
// Hasn't been closed manually
if (this.content.indexOf(notification) >= 0) {
this.removeNotification(notification);
this.dismissNotification(notification);
}
},
notification.remaining
Expand All @@ -116,7 +111,7 @@ export default class NotificationsService extends Service {

clearAll() {
this.content.forEach((notification) => {
this.removeNotification(notification);
this.dismissNotification(notification);
});

return this;
Expand Down
1 change: 1 addition & 0 deletions addon/templates/components/notification-message.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{{@notification.cssClasses}}"
data-test-notification-message={{@notification.type}}
role="alert"
{{on "animationend" this.handleAnimationEnd}}
{{on "mouseenter" this.handleMouseEnter}}
{{on "mouseleave" this.handleMouseLeave}}
>
Expand Down

0 comments on commit b4cf56a

Please sign in to comment.