refactor: remove static routes and stop multiple token api call in sse
This commit is contained in:
@@ -9,6 +9,8 @@ import {
|
||||
} from '@/services/sse';
|
||||
|
||||
const DEFAULT_RETRY_DELAY_MS = 2500;
|
||||
const DEFAULT_MAX_RETRY_DELAY_MS = 30000;
|
||||
const DEFAULT_MAX_RECONNECT_ATTEMPTS = 6;
|
||||
const EMPTY_RECONNECT_EVENTS: ReadonlyArray<PropertyKey> = [];
|
||||
|
||||
interface SseToken {
|
||||
@@ -21,7 +23,10 @@ interface UseSseWithTokenOptions<TEvents extends Record<string, unknown>> {
|
||||
getPath: (token: string) => string;
|
||||
events: SseEventMap<TEvents>;
|
||||
retryDelayMs?: number;
|
||||
maxRetryDelayMs?: number;
|
||||
maxReconnectAttempts?: number;
|
||||
onConnectionError?: () => void;
|
||||
onConnectionOpen?: () => void;
|
||||
reconnectOnEvents?: ReadonlyArray<keyof TEvents>;
|
||||
shouldStop?: (
|
||||
eventName: keyof TEvents,
|
||||
@@ -35,12 +40,16 @@ export function useSseWithToken<TEvents extends Record<string, unknown>>({
|
||||
getPath,
|
||||
events,
|
||||
retryDelayMs = DEFAULT_RETRY_DELAY_MS,
|
||||
maxRetryDelayMs = DEFAULT_MAX_RETRY_DELAY_MS,
|
||||
maxReconnectAttempts = DEFAULT_MAX_RECONNECT_ATTEMPTS,
|
||||
onConnectionError,
|
||||
onConnectionOpen,
|
||||
reconnectOnEvents = EMPTY_RECONNECT_EVENTS as ReadonlyArray<keyof TEvents>,
|
||||
shouldStop,
|
||||
}: UseSseWithTokenOptions<TEvents>) {
|
||||
const connectionRef = useRef<SseConnection | null>(null);
|
||||
const retryTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const reconnectAttemptRef = useRef(0);
|
||||
const stoppedRef = useRef(false);
|
||||
|
||||
const clearRetryTimer = useCallback(() => {
|
||||
@@ -56,6 +65,7 @@ export function useSseWithToken<TEvents extends Record<string, unknown>>({
|
||||
|
||||
const stop = useCallback(() => {
|
||||
stoppedRef.current = true;
|
||||
reconnectAttemptRef.current = 0;
|
||||
clearRetryTimer();
|
||||
closeConnection();
|
||||
}, [clearRetryTimer, closeConnection]);
|
||||
@@ -64,16 +74,33 @@ export function useSseWithToken<TEvents extends Record<string, unknown>>({
|
||||
if (!enabled) return;
|
||||
|
||||
stoppedRef.current = false;
|
||||
reconnectAttemptRef.current = 0;
|
||||
|
||||
const getReconnectDelay = () =>
|
||||
Math.min(
|
||||
retryDelayMs * 2 ** Math.max(reconnectAttemptRef.current - 1, 0),
|
||||
maxRetryDelayMs,
|
||||
);
|
||||
|
||||
const scheduleReconnect = (notify = true) => {
|
||||
if (stoppedRef.current || retryTimerRef.current) return;
|
||||
|
||||
closeConnection();
|
||||
if (notify) onConnectionError?.();
|
||||
|
||||
if (notify) {
|
||||
onConnectionError?.();
|
||||
}
|
||||
|
||||
if (reconnectAttemptRef.current >= maxReconnectAttempts) {
|
||||
stoppedRef.current = true;
|
||||
return;
|
||||
}
|
||||
|
||||
reconnectAttemptRef.current += 1;
|
||||
retryTimerRef.current = setTimeout(() => {
|
||||
retryTimerRef.current = null;
|
||||
connect();
|
||||
}, retryDelayMs);
|
||||
}, getReconnectDelay());
|
||||
};
|
||||
|
||||
const connect = () => {
|
||||
@@ -116,6 +143,10 @@ export function useSseWithToken<TEvents extends Record<string, unknown>>({
|
||||
wrappedEvents,
|
||||
);
|
||||
|
||||
connection.source.onopen = () => {
|
||||
reconnectAttemptRef.current = 0;
|
||||
onConnectionOpen?.();
|
||||
};
|
||||
connection.source.onerror = () => scheduleReconnect();
|
||||
connectionRef.current = connection;
|
||||
})
|
||||
@@ -131,7 +162,10 @@ export function useSseWithToken<TEvents extends Record<string, unknown>>({
|
||||
events,
|
||||
getPath,
|
||||
getToken,
|
||||
maxReconnectAttempts,
|
||||
maxRetryDelayMs,
|
||||
onConnectionError,
|
||||
onConnectionOpen,
|
||||
reconnectOnEvents,
|
||||
retryDelayMs,
|
||||
shouldStop,
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
import { authService } from '@/services/api/auth.service';
|
||||
import { initializeAuthenticatedApp } from '@/services/initializer.service';
|
||||
import { useAppStore } from '@/store/app.store';
|
||||
import { useAuthStore } from '@/store/auth.store';
|
||||
import type { LoginPayload } from '@/types';
|
||||
import { ROUTES } from '@/utils/routes';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { toast } from 'sonner';
|
||||
|
||||
export const useLoginForm = () => {
|
||||
const router = useRouter();
|
||||
const setAccessToken = useAuthStore((state) => state.setAccessToken);
|
||||
const setLoading = useAppStore((state) => state.setLoading);
|
||||
const {
|
||||
@@ -39,10 +35,8 @@ export const useLoginForm = () => {
|
||||
|
||||
setAccessToken(accessToken);
|
||||
setLoading();
|
||||
await initializeAuthenticatedApp();
|
||||
|
||||
toast.success('Login successful');
|
||||
router.replace(ROUTES.DASHBOARD);
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : 'Unable to sign in');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user