Skip to content

Commit 76555db

Browse files
committed
fix:
1 parent 21970a6 commit 76555db

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/providers/SettingsProvider.jsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ export default function SettingsProvider({ children }) {
5050
const toast = useToast();
5151
const toastSyncRef = useRef();
5252
const [updating, setUpdating] = useState(false);
53-
const [syncing, setSyncing] = useState(false);
5453
const [adding, setAdding] = useState(false);
5554
const {
5655
isOpen: isOpenMenu,
5756
onOpen: onOpenMenu,
5857
onClose: onCloseMenu,
5958
} = useDisclosure();
59+
const syncRef = useRef(null);
6060

6161
const { isOpen, onOpen, onClose } = useDisclosure();
6262

@@ -83,7 +83,10 @@ export default function SettingsProvider({ children }) {
8383
if (!authUsername) return;
8484
const localSettings = await getSettingIDB(authUsername);
8585
if (!localSettings) return;
86-
setSyncing(true);
86+
if (syncRef.current) return;
87+
88+
syncRef.current = true;
89+
8790
toastSyncRef.current = toast({
8891
title: "Syncing settings...",
8992
status: "loading",
@@ -163,7 +166,9 @@ export default function SettingsProvider({ children }) {
163166
throw new Error("Something went wrong when syncing your settings.");
164167
}
165168
}
166-
setSyncing(false);
169+
170+
syncRef.current = false;
171+
167172
toast.close(toastSyncRef.current);
168173
}, [authUsername, editSettingIDB, getSettingIDB, toast]);
169174

@@ -310,8 +315,8 @@ export default function SettingsProvider({ children }) {
310315
);
311316

312317
const handleSync = useCallback(() => {
313-
if (!syncing && authUsername && navigator.onLine) syncSettings();
314-
}, [syncing, authUsername, syncSettings]);
318+
if (authUsername && navigator.onLine) syncSettings();
319+
}, [authUsername, syncSettings]);
315320

316321
useEffect(() => {
317322
fetchSettings();
@@ -352,7 +357,6 @@ export default function SettingsProvider({ children }) {
352357
updateAlarm,
353358
updateFocusMusic,
354359
updating,
355-
syncing,
356360
adding,
357361
isOpenMenu,
358362
onOpenMenu,

src/providers/TasksProvider.jsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,22 @@ export default function TasksProvider({ children }) {
3333
deleteRecord: deleteTaskIDB,
3434
getByID: getTaskIDB,
3535
} = useIndexedDB("tasks");
36-
const [syncing, setSyncing] = useState(false);
3736

3837
const [tasks, setTasks] = useState([]);
3938
const [_selectedTask, setSelectedTask] = useState();
4039
const { authUsername, afterSignUp } = useAuth();
4140
const [updating, setUpdating] = useState(false);
4241
const [deleting, setDeleting] = useState(false);
4342
const [adding, setAdding] = useState(false);
43+
const syncRef = useRef(null);
4444

4545
const syncTasks = useCallback(
4646
async (username) => {
47+
if (syncRef.current) return;
4748
if (!username) return;
48-
setSyncing(true);
49+
50+
syncRef.current = true;
51+
4952
toastSyncRef.current = toast({
5053
title: "Syncing tasks...",
5154
status: "loading",
@@ -174,7 +177,8 @@ export default function TasksProvider({ children }) {
174177
});
175178
}
176179

177-
setSyncing(false);
180+
syncRef.current = false;
181+
178182
toast.close(toastSyncRef.current);
179183
},
180184
[addTaskIDB, deleteTaskIDB, editTaskIDB, getAllTasksIDB, toast]
@@ -200,15 +204,13 @@ export default function TasksProvider({ children }) {
200204
.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt))
201205
);
202206

203-
if (!syncing && username !== GUEST_USERNAME && navigator.onLine)
204-
syncTasks(username);
207+
if (username !== GUEST_USERNAME && navigator.onLine) syncTasks(username);
205208
},
206-
[getAllTasksIDB, syncTasks, syncing]
209+
[getAllTasksIDB, syncTasks]
207210
);
208211

209212
const syncAfterSignUp = useCallback(
210213
async (username) => {
211-
setSyncing(true);
212214
toastSyncRef.current = toast({
213215
title: "Syncing tasks...",
214216
status: "loading",
@@ -259,7 +261,6 @@ export default function TasksProvider({ children }) {
259261
.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt))
260262
);
261263

262-
setSyncing(false);
263264
toast.close(toastSyncRef.current);
264265
},
265266
[addTaskIDB, deleteTaskIDB, getAllTasksIDB, toast]
@@ -447,8 +448,8 @@ export default function TasksProvider({ children }) {
447448
}, [toast]);
448449

449450
const handleSync = useCallback(() => {
450-
if (!syncing && authUsername && navigator.onLine) syncTasks(authUsername);
451-
}, [authUsername, syncing, syncTasks]);
451+
if (authUsername && navigator.onLine) syncTasks(authUsername);
452+
}, [authUsername, syncTasks]);
452453

453454
useEffect(() => {
454455
window.addEventListener("online", handleSync);

0 commit comments

Comments
 (0)