From 76388a408c7623352a91122bb86fc31c0c1cfd08 Mon Sep 17 00:00:00 2001 From: Fredrik Burmester Date: Wed, 18 Sep 2024 08:42:23 +0200 Subject: [PATCH] fix: throttle --- hooks/useTrickplay.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hooks/useTrickplay.ts b/hooks/useTrickplay.ts index 264d6a0a..502faefa 100644 --- a/hooks/useTrickplay.ts +++ b/hooks/useTrickplay.ts @@ -1,6 +1,6 @@ // hooks/useTrickplay.ts -import { useState, useCallback, useMemo } from "react"; +import { useState, useCallback, useMemo, useRef } from "react"; import { Api } from "@jellyfin/sdk"; import { SharedValue } from "react-native-reanimated"; import { CurrentlyPlayingState } from "@/providers/PlaybackProvider"; @@ -33,6 +33,8 @@ export const useTrickplay = ( ) => { const [api] = useAtom(apiAtom); const [trickPlayUrl, setTrickPlayUrl] = useState(null); + const lastCalculationTime = useRef(0); + const throttleDelay = 100; // 200ms throttle const trickplayInfo = useMemo(() => { if (!currentlyPlaying?.item.Id || !currentlyPlaying?.item.Trickplay) { @@ -61,6 +63,12 @@ export const useTrickplay = ( const calculateTrickplayUrl = useCallback( (progress: SharedValue) => { + const now = Date.now(); + if (now - lastCalculationTime.current < throttleDelay) { + return null; + } + lastCalculationTime.current = now; + if (!trickplayInfo || !api || !currentlyPlaying?.item.Id) { return null; }