From f7d21807a4552d0cfc1161c64dd7854d488ae7c5 Mon Sep 17 00:00:00 2001 From: enes Date: Thu, 14 Nov 2024 13:56:42 +0100 Subject: [PATCH] refactor(fetch): add 1min timeout on reactions, 10sec timeout on user relay list --- src/contexts/NDKContext.tsx | 10 +++++++--- src/hooks/useReactions.ts | 8 ++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/contexts/NDKContext.tsx b/src/contexts/NDKContext.tsx index 23f672c..ed80c1f 100644 --- a/src/contexts/NDKContext.tsx +++ b/src/contexts/NDKContext.tsx @@ -21,7 +21,8 @@ import { log, LogType, npubToHex, - orderEventsChronologically + orderEventsChronologically, + timeout } from 'utils' type FetchModsOptions = { @@ -241,8 +242,11 @@ export const NDKContextProvider = ({ children }: { children: ReactNode }) => { hexKey: string, userRelaysType: UserRelaysType ): Promise => { - // Find the user's relays. - const relayUrls = await getRelayListForUser(hexKey, ndk) + // Find the user's relays (10s timeout). + const relayUrls = await Promise.race([ + getRelayListForUser(hexKey, ndk), + timeout(10000) + ]) .then((ndkRelayList) => { if (ndkRelayList) return ndkRelayList[userRelaysType] return [] // Return an empty array if ndkRelayList is undefined diff --git a/src/hooks/useReactions.ts b/src/hooks/useReactions.ts index 574c3eb..0f029bb 100644 --- a/src/hooks/useReactions.ts +++ b/src/hooks/useReactions.ts @@ -5,7 +5,7 @@ import { Event, kinds, UnsignedEvent } from 'nostr-tools' import { useMemo, useState } from 'react' import { toast } from 'react-toastify' import { UserRelaysType } from 'types' -import { abbreviateNumber, log, LogType, now } from 'utils' +import { abbreviateNumber, log, LogType, now, timeout } from 'utils' type UseReactionsParams = { pubkey: string @@ -32,7 +32,11 @@ export const useReactions = (params: UseReactionsParams) => { filter['#e'] = [params.eTag] } - fetchEventsFromUserRelays(filter, params.pubkey, UserRelaysType.Read) + // 1 minute timeout + Promise.race([ + fetchEventsFromUserRelays(filter, params.pubkey, UserRelaysType.Read), + timeout(60000) + ]) .then((events) => { setReactionEvents(events) })