Skip to content

Commit 62ad036

Browse files
authored
fix: add fetch lock (#2570)
* fix: add fetch lock Signed-off-by: Adam Setch <adam.setch@outlook.com> * fix: add fetch lock Signed-off-by: Adam Setch <adam.setch@outlook.com> --------- Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent 2d4e259 commit 62ad036

1 file changed

Lines changed: 38 additions & 29 deletions

File tree

src/renderer/hooks/useNotifications.ts

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useMemo, useState } from 'react';
1+
import { useCallback, useMemo, useRef, useState } from 'react';
22

33
import type {
44
Account,
@@ -94,44 +94,53 @@ export const useNotifications = (): NotificationsState => {
9494
[notifications],
9595
);
9696

97+
const isFetchingRef = useRef(false);
9798
const fetchNotifications = useCallback(
9899
async (state: GitifyState) => {
100+
if (isFetchingRef.current) {
101+
// Prevent overlapping fetches
102+
return;
103+
}
104+
isFetchingRef.current = true;
99105
setStatus('loading');
100106
setGlobalError(null);
107+
try {
108+
const previousNotifications = notifications;
109+
const fetchedNotifications = await getAllNotifications(state);
110+
setNotifications(fetchedNotifications);
111+
112+
// Set Global Error if all accounts have the same error
113+
const allAccountsHaveErrors =
114+
doesAllAccountsHaveErrors(fetchedNotifications);
115+
const allAccountErrorsAreSame =
116+
areAllAccountErrorsSame(fetchedNotifications);
117+
118+
if (allAccountsHaveErrors) {
119+
const accountError = fetchedNotifications[0].error;
120+
setStatus('error');
121+
setGlobalError(allAccountErrorsAreSame ? accountError : null);
122+
return;
123+
}
101124

102-
const previousNotifications = notifications;
103-
const fetchedNotifications = await getAllNotifications(state);
104-
setNotifications(fetchedNotifications);
105-
106-
// Set Global Error if all accounts have the same error
107-
const allAccountsHaveErrors =
108-
doesAllAccountsHaveErrors(fetchedNotifications);
109-
const allAccountErrorsAreSame =
110-
areAllAccountErrorsSame(fetchedNotifications);
111-
112-
if (allAccountsHaveErrors) {
113-
const accountError = fetchedNotifications[0].error;
114-
setStatus('error');
115-
setGlobalError(allAccountErrorsAreSame ? accountError : null);
116-
return;
117-
}
125+
const diffNotifications = getNewNotifications(
126+
previousNotifications,
127+
fetchedNotifications,
128+
);
118129

119-
const diffNotifications = getNewNotifications(
120-
previousNotifications,
121-
fetchedNotifications,
122-
);
130+
if (diffNotifications.length > 0) {
131+
if (state.settings.playSound) {
132+
raiseSoundNotification(state.settings.notificationVolume);
133+
}
123134

124-
if (diffNotifications.length > 0) {
125-
if (state.settings.playSound) {
126-
raiseSoundNotification(state.settings.notificationVolume);
135+
if (state.settings.showNotifications) {
136+
raiseNativeNotification(diffNotifications);
137+
}
127138
}
128139

129-
if (state.settings.showNotifications) {
130-
raiseNativeNotification(diffNotifications);
131-
}
140+
setStatus('success');
141+
} finally {
142+
isFetchingRef.current = false;
132143
}
133-
134-
setStatus('success');
135144
},
136145
[notifications],
137146
);

0 commit comments

Comments
 (0)