mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 08:23:35 +00:00
added cashu context to prevent repeated invocation
This commit is contained in:
1 parent
669a911d59
commit
565305a592
3 files changed
+110
-8
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import { createContext, useContext, ReactNode } from 'react';
|
||||
import { useCashu as useCashuHook, type UseCashuReturn } from '@/hooks/useCashu';
|
||||
|
||||
const CashuContext = createContext<UseCashuReturn | null>(null);
|
||||
|
||||
export function CashuProvider({ children }: { children: ReactNode }) {
|
||||
const cashu = useCashuHook();
|
||||
|
||||
return (
|
||||
<CashuContext.Provider value={cashu}>
|
||||
{children}
|
||||
</CashuContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useCashu(): UseCashuReturn {
|
||||
const context = useContext(CashuContext);
|
||||
|
||||
if (!context) {
|
||||
throw new Error('useCashu must be used within a CashuProvider');
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
@@ -132,17 +132,26 @@ export function useCashu(): UseCashuReturn {
|
||||
|
||||
// Utility operations - defined first so they can be used in other callbacks
|
||||
const lastRefreshTimeRef = useRef<number>(0);
|
||||
const isRefreshingRef = useRef<boolean>(false);
|
||||
const REFRESH_THROTTLE_MS = 500; // Prevent refreshing more than once every 500ms
|
||||
|
||||
const refreshBalances = useCallback(async () => {
|
||||
if (!managerRef.current) return;
|
||||
|
||||
// Prevent concurrent calls
|
||||
if (isRefreshingRef.current) {
|
||||
console.log('⏭️ refreshBalances: Skipping (already in progress)');
|
||||
return;
|
||||
}
|
||||
|
||||
// Throttle rapid successive calls
|
||||
const now = Date.now();
|
||||
if (now - lastRefreshTimeRef.current < REFRESH_THROTTLE_MS) {
|
||||
console.log('⏭️ refreshBalances: Skipping (throttled)');
|
||||
return;
|
||||
}
|
||||
|
||||
isRefreshingRef.current = true;
|
||||
lastRefreshTimeRef.current = now;
|
||||
|
||||
try {
|
||||
@@ -166,6 +175,8 @@ export function useCashu(): UseCashuReturn {
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('❌ refreshBalances: Failed to refresh balances:', err);
|
||||
} finally {
|
||||
isRefreshingRef.current = false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
|
||||
Reference in new issue
Block a user