@PhantomSpooks It is a stress test so it was like 10 times per second but I added a 0 delay in executing it and it seems to work so the users will not be able to spam the button and flood the database with requests.
useEffect(() => {
let timeoutId;
const codRef = ref(db, "COD");
const codListener = onValue(codRef, (snapshot) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
const loadouts = snapshot.exists() ? snapshot.val() : {};
const sortedLoadouts = Object.values(loadouts)
.sort((a, b) => (b.likes?.length || 0) - (a.likes?.length || 0))
.reduce((acc, loadout) => {
acc[loadout.id] = loadout;
return acc;
}, {});
setList(sortedLoadouts);
}, 0); // Debounce for 0ms
});
return () => {
clearTimeout(timeoutId);
codListener();
};
}, [db]);