79226583

Date: 2024-11-26 11:54:39
Score: 1.5
Natty:
Report link

@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]);
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Looks like a comment (1):
  • Low reputation (1):
Posted by: Claudiu Stelian