From 99dafcda64a401d8b089e0ed1e66515c450bdb72 Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Tue, 24 Sep 2019 09:55:07 +0200 Subject: [PATCH] Reduce complexity of notifications watcher - Co-authored-by: @Tirokk --- .../NotificationMenu/NotificationMenu.spec.js | 4 ++-- .../NotificationMenu/NotificationMenu.vue | 22 +++++-------------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/webapp/components/notifications/NotificationMenu/NotificationMenu.spec.js b/webapp/components/notifications/NotificationMenu/NotificationMenu.spec.js index 85384977e..93b56b8a2 100644 --- a/webapp/components/notifications/NotificationMenu/NotificationMenu.spec.js +++ b/webapp/components/notifications/NotificationMenu/NotificationMenu.spec.js @@ -84,7 +84,7 @@ describe('NotificationMenu.vue', () => { read: true, post: { id: 'post-3', - title: 'red post title', + title: 'read post title', contentExcerpt: 'this is yet another post content', author: { id: 'john-1', @@ -98,7 +98,7 @@ describe('NotificationMenu.vue', () => { } }) - it('displays the unread number of notifications', () => { + it('displays the number of unread notifications', () => { wrapper = Wrapper() expect(wrapper.find('ds-button-stub').text()).toEqual('2') }) diff --git a/webapp/components/notifications/NotificationMenu/NotificationMenu.vue b/webapp/components/notifications/NotificationMenu/NotificationMenu.vue index ecc89ff41..366148885 100644 --- a/webapp/components/notifications/NotificationMenu/NotificationMenu.vue +++ b/webapp/components/notifications/NotificationMenu/NotificationMenu.vue @@ -50,32 +50,20 @@ export default { }, watch: { notifications: { + immediate: true, handler(lastQueriedNotifications) { if (lastQueriedNotifications && lastQueriedNotifications.length > 0) { - // set this to be empty to get always called if a query comes with results from the backend - // has the sideeffect the handler is encouraged to be called again, but only once with no effect, because of the if above - this.notifications = [] - - let oldNotifications = this.displayedNotifications - const equalNotification = this.equalNotification // because we can not use 'this.equalNotification' in callback - - // add all the new notifications to the oldNotifications at top of the list lastQueriedNotifications.forEach(updatedListNotification => { - const sameNotification = oldNotifications.find(function(oldListNotification) { - return equalNotification(oldListNotification, updatedListNotification) + const sameNotification = this.displayedNotifications.find(oldListNotification => { + return this.equalNotification(oldListNotification, updatedListNotification) }) - if (sameNotification === undefined) { - oldNotifications.unshift(updatedListNotification) - } + if (!sameNotification) this.displayedNotifications.unshift(updatedListNotification) }) - - this.displayedNotifications = oldNotifications } }, - deep: true, - immediate: true, }, }, + methods: { async updateNotifications() { try {