about summary refs log tree commit diff
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2022-08-25 17:36:54 +0200
committerGitHub <noreply@github.com>2022-08-25 17:36:54 +0200
commit55bef1e34fc3b07ed7f762d565a161e74e128016 (patch)
treee3720e53dc87a98fcc9b034626ecef1c7fa2a15f
parent2cae5f5b8d4d0702fe9835a0ce2782f238755d0c (diff)
Fix quickly switching notification filters resulting in empty or incorrect list (#19052)
Follow-up to #18960

The aforementioned PR fixed an issue in which switching notification filters
while notifications were loading prevented the query for the new filter from
running, but another issue remained: if the first query completed after the
second one, its results would override the second one, thus leading to the
same issue.

This commit cancels the first request if it is still running, before issuing
the second one.
-rw-r--r--app/javascript/mastodon/actions/notifications.js17
1 files changed, 12 insertions, 5 deletions
diff --git a/app/javascript/mastodon/actions/notifications.js b/app/javascript/mastodon/actions/notifications.js
index 7f62e6c04..d4588db2c 100644
--- a/app/javascript/mastodon/actions/notifications.js
+++ b/app/javascript/mastodon/actions/notifications.js
@@ -141,15 +141,22 @@ const excludeTypesFromFilter = filter => {
 
 const noOp = () => {};
 
+let expandNotificationsController = new AbortController();
+
 export function expandNotifications({ maxId, forceLoad } = {}, done = noOp) {
   return (dispatch, getState) => {
     const activeFilter = getState().getIn(['settings', 'notifications', 'quickFilter', 'active']);
     const notifications = getState().get('notifications');
     const isLoadingMore = !!maxId;
 
-    if (notifications.get('isLoading') && !forceLoad) {
-      done();
-      return;
+    if (notifications.get('isLoading')) {
+      if (forceLoad) {
+        expandNotificationsController.abort();
+        expandNotificationsController = new AbortController();
+      } else {
+        done();
+        return;
+      }
     }
 
     const params = {
@@ -174,7 +181,7 @@ export function expandNotifications({ maxId, forceLoad } = {}, done = noOp) {
 
     dispatch(expandNotificationsRequest(isLoadingMore));
 
-    api(getState).get('/api/v1/notifications', { params }).then(response => {
+    api(getState).get('/api/v1/notifications', { params, signal: expandNotificationsController.signal }).then(response => {
       const next = getLinks(response).refs.find(link => link.rel === 'next');
 
       dispatch(importFetchedAccounts(response.data.map(item => item.account)));
@@ -215,7 +222,7 @@ export function expandNotificationsFail(error, isLoadingMore) {
     type: NOTIFICATIONS_EXPAND_FAIL,
     error,
     skipLoading: !isLoadingMore,
-    skipAlert: !isLoadingMore,
+    skipAlert: !isLoadingMore || error.name === 'AbortError',
   };
 };