mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 08:23:35 +00:00
balance refresh infinite loop example
This commit is contained in:
1 parent
39796c293a
commit
669a911d59
2 files changed
+116
-8
No files matched your search
+71
-8
@@ -131,15 +131,39 @@ export function useCashu(): UseCashuReturn {
|
||||
const repositoriesRef = useRef<unknown>(null);
|
||||
|
||||
// Utility operations - defined first so they can be used in other callbacks
|
||||
const lastRefreshTimeRef = useRef<number>(0);
|
||||
const REFRESH_THROTTLE_MS = 500; // Prevent refreshing more than once every 500ms
|
||||
|
||||
const refreshBalances = useCallback(async () => {
|
||||
if (!managerRef.current) return;
|
||||
|
||||
// Throttle rapid successive calls
|
||||
const now = Date.now();
|
||||
if (now - lastRefreshTimeRef.current < REFRESH_THROTTLE_MS) {
|
||||
console.log('⏭️ refreshBalances: Skipping (throttled)');
|
||||
return;
|
||||
}
|
||||
lastRefreshTimeRef.current = now;
|
||||
|
||||
try {
|
||||
console.log('🔄 refreshBalances: Getting balances from wallet...');
|
||||
const newBalances = await (managerRef.current as { wallet: { getBalances: () => Promise<CashuBalance> } }).wallet.getBalances();
|
||||
console.log('💰 refreshBalances: Raw balances from wallet:', newBalances);
|
||||
setBalances(newBalances);
|
||||
console.log('✅ refreshBalances: State updated with balances:', Object.keys(newBalances).length, 'mints');
|
||||
|
||||
// Only update state if balances actually changed (deep comparison)
|
||||
setBalances(prevBalances => {
|
||||
const hasChanged =
|
||||
Object.keys(newBalances).length !== Object.keys(prevBalances).length ||
|
||||
Object.keys(newBalances).some(key => newBalances[key] !== prevBalances[key]);
|
||||
|
||||
if (hasChanged) {
|
||||
console.log('✅ refreshBalances: Balances changed, updating state:', Object.keys(newBalances).length, 'mints');
|
||||
return newBalances;
|
||||
} else {
|
||||
console.log('⏭️ refreshBalances: Balances unchanged, skipping state update');
|
||||
return prevBalances;
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('❌ refreshBalances: Failed to refresh balances:', err);
|
||||
}
|
||||
@@ -208,8 +232,21 @@ export function useCashu(): UseCashuReturn {
|
||||
console.log('🔄 refreshBalances: Getting balances from wallet...');
|
||||
const newBalances = await (managerRef.current as { wallet: { getBalances: () => Promise<CashuBalance> } }).wallet.getBalances();
|
||||
console.log('💰 refreshBalances: Raw balances from wallet:', newBalances);
|
||||
setBalances(newBalances);
|
||||
console.log('✅ refreshBalances: State updated with balances:', Object.keys(newBalances).length, 'mints');
|
||||
|
||||
// Only update state if balances actually changed (deep comparison)
|
||||
setBalances(prevBalances => {
|
||||
const hasChanged =
|
||||
Object.keys(newBalances).length !== Object.keys(prevBalances).length ||
|
||||
Object.keys(newBalances).some(key => newBalances[key] !== prevBalances[key]);
|
||||
|
||||
if (hasChanged) {
|
||||
console.log('✅ refreshBalances: Balances changed, updating state:', Object.keys(newBalances).length, 'mints');
|
||||
return newBalances;
|
||||
} else {
|
||||
console.log('⏭️ refreshBalances: Balances unchanged, skipping state update');
|
||||
return prevBalances;
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('❌ refreshBalances: Failed to refresh balances:', err);
|
||||
}
|
||||
@@ -265,8 +302,21 @@ export function useCashu(): UseCashuReturn {
|
||||
console.log('🔄 refreshBalances: Getting balances from wallet...');
|
||||
const newBalances = await manager.wallet.getBalances();
|
||||
console.log('💰 refreshBalances: Raw balances from wallet:', newBalances);
|
||||
setBalances(newBalances);
|
||||
console.log('✅ refreshBalances: State updated with balances:', Object.keys(newBalances).length, 'mints');
|
||||
|
||||
// Only update state if balances actually changed (deep comparison)
|
||||
setBalances(prevBalances => {
|
||||
const hasChanged =
|
||||
Object.keys(newBalances).length !== Object.keys(prevBalances).length ||
|
||||
Object.keys(newBalances).some(key => newBalances[key] !== prevBalances[key]);
|
||||
|
||||
if (hasChanged) {
|
||||
console.log('✅ refreshBalances: Balances changed, updating state:', Object.keys(newBalances).length, 'mints');
|
||||
return newBalances;
|
||||
} else {
|
||||
console.log('⏭️ refreshBalances: Balances unchanged, skipping state update');
|
||||
return prevBalances;
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('❌ refreshBalances: Failed to refresh balances:', err);
|
||||
}
|
||||
@@ -323,8 +373,21 @@ export function useCashu(): UseCashuReturn {
|
||||
console.log('🔄 refreshBalances: Getting balances from wallet...');
|
||||
const newBalances = await manager.wallet.getBalances();
|
||||
console.log('💰 refreshBalances: Raw balances from wallet:', newBalances);
|
||||
setBalances(newBalances);
|
||||
console.log('✅ refreshBalances: State updated with balances:', Object.keys(newBalances).length, 'mints');
|
||||
|
||||
// Only update state if balances actually changed (deep comparison)
|
||||
setBalances(prevBalances => {
|
||||
const hasChanged =
|
||||
Object.keys(newBalances).length !== Object.keys(prevBalances).length ||
|
||||
Object.keys(newBalances).some(key => newBalances[key] !== prevBalances[key]);
|
||||
|
||||
if (hasChanged) {
|
||||
console.log('✅ refreshBalances: Balances changed, updating state:', Object.keys(newBalances).length, 'mints');
|
||||
return newBalances;
|
||||
} else {
|
||||
console.log('⏭️ refreshBalances: Balances unchanged, skipping state update');
|
||||
return prevBalances;
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('❌ refreshBalances: Failed to refresh balances:', err);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user