Merge branch 'dont-poll' into 'main'

Don't automatically poll home/notifications when WebSocket disconnects

See merge request soapbox-pub/soapbox!3309
This commit is contained in:
Alex Gleason 2024-12-30 17:31:49 +00:00
commit 4d2dde6783
8 changed files with 2 additions and 55 deletions

View File

@ -99,10 +99,9 @@ interface TimelineStreamOpts {
const connectTimelineStream = ( const connectTimelineStream = (
timelineId: string, timelineId: string,
path: string, path: string,
pollingRefresh: ((dispatch: AppDispatch, done?: () => void) => void) | null = null,
accept: ((status: APIEntity) => boolean) | null = null, accept: ((status: APIEntity) => boolean) | null = null,
opts?: TimelineStreamOpts, opts?: TimelineStreamOpts,
) => connectStream(path, pollingRefresh, (dispatch: AppDispatch, getState: () => RootState) => { ) => connectStream(path, (dispatch: AppDispatch, getState: () => RootState) => {
const locale = getLocale(getState()); const locale = getLocale(getState());
return { return {

View File

@ -10,7 +10,6 @@ function useCommunityStream({ onlyMedia, enabled }: UseCommunityStreamOpts = {})
`community${onlyMedia ? ':media' : ''}`, `community${onlyMedia ? ':media' : ''}`,
`public:local${onlyMedia ? ':media' : ''}`, `public:local${onlyMedia ? ':media' : ''}`,
undefined, undefined,
undefined,
{ enabled }, { enabled },
); );
} }

View File

@ -9,7 +9,6 @@ function useDirectStream() {
'direct', 'direct',
'direct', 'direct',
null, null,
null,
{ enabled: isLoggedIn }, { enabled: isLoggedIn },
); );
} }

View File

@ -9,7 +9,6 @@ function useListStream(listId: string) {
`list:${listId}`, `list:${listId}`,
`list&list=${listId}`, `list&list=${listId}`,
null, null,
null,
{ enabled: isLoggedIn }, { enabled: isLoggedIn },
); );
} }

View File

@ -10,7 +10,6 @@ function usePublicStream({ onlyMedia, language }: UsePublicStreamOpts = {}) {
`public${onlyMedia ? ':media' : ''}`, `public${onlyMedia ? ':media' : ''}`,
`public${onlyMedia ? ':media' : ''}`, `public${onlyMedia ? ':media' : ''}`,
null, null,
null,
{ enabled: !language }, // TODO: support language streaming { enabled: !language }, // TODO: support language streaming
); );
} }

View File

@ -9,7 +9,7 @@ import { getAccessToken } from 'soapbox/utils/auth.ts';
function useTimelineStream(...args: Parameters<typeof connectTimelineStream>) { function useTimelineStream(...args: Parameters<typeof connectTimelineStream>) {
// TODO: get rid of streaming.ts and move the actual opts here. // TODO: get rid of streaming.ts and move the actual opts here.
const [timelineId, path] = args; const [timelineId, path] = args;
const { enabled = true } = args[4] ?? {}; const { enabled = true } = args[3] ?? {};
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const { instance } = useInstance(); const { instance } = useInstance();

View File

@ -1,12 +1,8 @@
import { expandNotifications } from 'soapbox/actions/notifications.ts';
import { expandHomeTimeline } from 'soapbox/actions/timelines.ts';
import { useStatContext } from 'soapbox/contexts/stat-context.tsx'; import { useStatContext } from 'soapbox/contexts/stat-context.tsx';
import { useLoggedIn } from 'soapbox/hooks/useLoggedIn.ts'; import { useLoggedIn } from 'soapbox/hooks/useLoggedIn.ts';
import { useTimelineStream } from './useTimelineStream.ts'; import { useTimelineStream } from './useTimelineStream.ts';
import type { AppDispatch } from 'soapbox/store.ts';
function useUserStream() { function useUserStream() {
const { isLoggedIn } = useLoggedIn(); const { isLoggedIn } = useLoggedIn();
const statContext = useStatContext(); const statContext = useStatContext();
@ -14,16 +10,9 @@ function useUserStream() {
return useTimelineStream( return useTimelineStream(
'home', 'home',
'user', 'user',
refresh,
null, null,
{ statContext, enabled: isLoggedIn }, { statContext, enabled: isLoggedIn },
); );
} }
/** Refresh home timeline and notifications. */
function refresh(dispatch: AppDispatch, done?: () => void) {
return dispatch(expandHomeTimeline({}, () =>
dispatch(expandNotifications({}, done))));
}
export { useUserStream }; export { useUserStream };

View File

@ -4,19 +4,14 @@ import { getAccessToken } from 'soapbox/utils/auth.ts';
import type { AppDispatch, RootState } from 'soapbox/store.ts'; import type { AppDispatch, RootState } from 'soapbox/store.ts';
const randomIntUpTo = (max: number) => Math.floor(Math.random() * Math.floor(max));
interface ConnectStreamCallbacks { interface ConnectStreamCallbacks {
onConnect(): void; onConnect(): void;
onDisconnect(): void; onDisconnect(): void;
onReceive(websocket: Websocket, data: unknown): void; onReceive(websocket: Websocket, data: unknown): void;
} }
type PollingRefreshFn = (dispatch: AppDispatch, done?: () => void) => void
export function connectStream( export function connectStream(
path: string, path: string,
pollingRefresh: PollingRefreshFn | null = null,
callbacks: (dispatch: AppDispatch, getState: () => RootState) => ConnectStreamCallbacks, callbacks: (dispatch: AppDispatch, getState: () => RootState) => ConnectStreamCallbacks,
) { ) {
return (dispatch: AppDispatch, getState: () => RootState) => { return (dispatch: AppDispatch, getState: () => RootState) => {
@ -24,23 +19,6 @@ export function connectStream(
const accessToken = getAccessToken(getState()); const accessToken = getAccessToken(getState());
const { onConnect, onDisconnect, onReceive } = callbacks(dispatch, getState); const { onConnect, onDisconnect, onReceive } = callbacks(dispatch, getState);
let polling: NodeJS.Timeout | null = null;
const setupPolling = () => {
if (pollingRefresh) {
pollingRefresh(dispatch, () => {
polling = setTimeout(() => setupPolling(), 20000 + randomIntUpTo(20000));
});
}
};
const clearPolling = () => {
if (polling) {
clearTimeout(polling);
polling = null;
}
};
let subscription: Websocket; let subscription: Websocket;
// If the WebSocket fails to be created, don't crash the whole page, // If the WebSocket fails to be created, don't crash the whole page,
@ -48,18 +26,10 @@ export function connectStream(
try { try {
subscription = getStream(streamingAPIBaseURL!, accessToken!, path, { subscription = getStream(streamingAPIBaseURL!, accessToken!, path, {
connected() { connected() {
if (pollingRefresh) {
clearPolling();
}
onConnect(); onConnect();
}, },
disconnected() { disconnected() {
if (pollingRefresh) {
polling = setTimeout(() => setupPolling(), randomIntUpTo(40000));
}
onDisconnect(); onDisconnect();
}, },
@ -68,11 +38,6 @@ export function connectStream(
}, },
reconnected() { reconnected() {
if (pollingRefresh) {
clearPolling();
pollingRefresh(dispatch);
}
onConnect(); onConnect();
}, },
@ -85,8 +50,6 @@ export function connectStream(
if (subscription) { if (subscription) {
subscription.close(); subscription.close();
} }
clearPolling();
}; };
return disconnect; return disconnect;