Skip to content

Commit cf2fe4b

Browse files
committed
fix: add fetch lock
Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent 2d4e259 commit cf2fe4b

1 file changed

Lines changed: 38 additions & 30 deletions

File tree

src/renderer/hooks/useNotifications.ts

Lines changed: 38 additions & 30 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,52 @@ export const useNotifications = (): NotificationsState => {
9494
[notifications],
9595
);
9696

97+
const isFetchingRef = useRef(false);
9798
const fetchNotifications = useCallback(
9899
async (state: GitifyState) => {
99-
setStatus('loading');
100-
setGlobalError(null);
101-
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);
100+
if (isFetchingRef.current) {
101+
// Prevent overlapping fetches
116102
return;
117103
}
104+
isFetchingRef.current = true;
105+
setStatus('loading');
106+
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+
}
118124

119-
const diffNotifications = getNewNotifications(
120-
previousNotifications,
121-
fetchedNotifications,
122-
);
125+
const diffNotifications = getNewNotifications(
126+
previousNotifications,
127+
fetchedNotifications,
128+
);
123129

124-
if (diffNotifications.length > 0) {
125-
if (state.settings.playSound) {
126-
raiseSoundNotification(state.settings.notificationVolume);
127-
}
130+
if (diffNotifications.length > 0) {
131+
if (state.settings.playSound) {
132+
raiseSoundNotification(state.settings.notificationVolume);
133+
}
128134

129-
if (state.settings.showNotifications) {
130-
raiseNativeNotification(diffNotifications);
135+
if (state.settings.showNotifications) {
136+
raiseNativeNotification(diffNotifications);
137+
}
131138
}
139+
} finally {
140+
setStatus('success');
141+
isFetchingRef.current = false;
132142
}
133-
134-
setStatus('success');
135143
},
136144
[notifications],
137145
);

0 commit comments

Comments
 (0)