Skip to content

Commit de57a67

Browse files
fixed race condition bug
1 parent 5ac6415 commit de57a67

1 file changed

Lines changed: 38 additions & 6 deletions

File tree

app/lib/hooks/useSolidStorages.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,13 @@ export function useSolidStorages(): UseSolidStoragesResult {
155155
const [error, setError] = useState<Error | null>(null);
156156

157157
useEffect(() => {
158+
let isMounted = true;
159+
let checkInterval: NodeJS.Timeout | null = null;
160+
158161
async function fetchStorages() {
159162
try {
163+
if (!isMounted) return;
164+
160165
setIsLoading(true);
161166
setError(null);
162167

@@ -171,19 +176,37 @@ export function useSolidStorages(): UseSolidStoragesResult {
171176
console.log("=========================");
172177

173178
// Set up a polling mechanism to check when authentication completes
174-
const checkInterval = setInterval(() => {
179+
// Keep isLoading as true while waiting for authentication
180+
checkInterval = setInterval(() => {
181+
if (!isMounted) {
182+
if (checkInterval) clearInterval(checkInterval);
183+
return;
184+
}
185+
175186
const currentSession = getDefaultSession();
176187
if (currentSession.info.isLoggedIn && currentSession.info.webId) {
177-
clearInterval(checkInterval);
178-
// Trigger re-fetch by updating state
188+
if (checkInterval) clearInterval(checkInterval);
189+
// Trigger re-fetch by calling fetchStorages again
179190
fetchStorages();
180191
}
181192
}, 500);
182193

183194
// Clear interval after 10 seconds to avoid infinite polling
184-
setTimeout(() => clearInterval(checkInterval), 10000);
195+
setTimeout(() => {
196+
if (checkInterval) {
197+
clearInterval(checkInterval);
198+
checkInterval = null;
199+
}
200+
// Only set loading to false if we've given up waiting and component is still mounted
201+
if (isMounted) {
202+
const finalSession = getDefaultSession();
203+
if (!finalSession.info.isLoggedIn || !finalSession.info.webId) {
204+
setIsLoading(false);
205+
}
206+
}
207+
}, 10000);
185208

186-
setIsLoading(false);
209+
// Don't set isLoading to false here - keep it true while waiting
187210
return;
188211
}
189212

@@ -581,14 +604,23 @@ export function useSolidStorages(): UseSolidStoragesResult {
581604

582605
// Also set up a listener for session changes
583606
const checkSession = setInterval(() => {
607+
if (!isMounted) {
608+
clearInterval(checkSession);
609+
return;
610+
}
611+
584612
const session = getDefaultSession();
585613
if (session.info.isLoggedIn && session.info.webId && storages.length === 0 && !isLoading) {
586614
console.log("Session state changed, re-fetching storages...");
587615
fetchStorages();
588616
}
589617
}, 1000);
590618

591-
return () => clearInterval(checkSession);
619+
return () => {
620+
isMounted = false;
621+
if (checkInterval) clearInterval(checkInterval);
622+
clearInterval(checkSession);
623+
};
592624
}, []);
593625

594626
return { storages, isLoading, error };

0 commit comments

Comments
 (0)