feat : 6 astra improve it
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useEffectEvent, useState } from "react";
|
||||
import { useEffect, useEffectEvent, useRef, useState } from "react";
|
||||
|
||||
import { PRESENCE_HEARTBEAT_MS } from "@/lib/presence-constants";
|
||||
|
||||
@@ -49,7 +49,10 @@ function useHeartbeat<T>({
|
||||
identifyBy: "browser" | "tab";
|
||||
}) {
|
||||
const [value, setValue] = useState<T | null>(null);
|
||||
const pending = useRef(false);
|
||||
const send = useEffectEvent(async (active = true) => {
|
||||
if (active && (document.visibilityState !== "visible" || pending.current)) return;
|
||||
pending.current = true;
|
||||
try {
|
||||
const response = await fetch(endpoint, {
|
||||
method: "POST",
|
||||
@@ -67,6 +70,8 @@ function useHeartbeat<T>({
|
||||
if (active) setValue(await response.json() as T);
|
||||
} catch {
|
||||
if (active) setValue(null);
|
||||
} finally {
|
||||
pending.current = false;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user