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:
commit
4d2dde6783
|
@ -99,10 +99,9 @@ interface TimelineStreamOpts {
|
|||
const connectTimelineStream = (
|
||||
timelineId: string,
|
||||
path: string,
|
||||
pollingRefresh: ((dispatch: AppDispatch, done?: () => void) => void) | null = null,
|
||||
accept: ((status: APIEntity) => boolean) | null = null,
|
||||
opts?: TimelineStreamOpts,
|
||||
) => connectStream(path, pollingRefresh, (dispatch: AppDispatch, getState: () => RootState) => {
|
||||
) => connectStream(path, (dispatch: AppDispatch, getState: () => RootState) => {
|
||||
const locale = getLocale(getState());
|
||||
|
||||
return {
|
||||
|
|
|
@ -10,7 +10,6 @@ function useCommunityStream({ onlyMedia, enabled }: UseCommunityStreamOpts = {})
|
|||
`community${onlyMedia ? ':media' : ''}`,
|
||||
`public:local${onlyMedia ? ':media' : ''}`,
|
||||
undefined,
|
||||
undefined,
|
||||
{ enabled },
|
||||
);
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ function useDirectStream() {
|
|||
'direct',
|
||||
'direct',
|
||||
null,
|
||||
null,
|
||||
{ enabled: isLoggedIn },
|
||||
);
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ function useListStream(listId: string) {
|
|||
`list:${listId}`,
|
||||
`list&list=${listId}`,
|
||||
null,
|
||||
null,
|
||||
{ enabled: isLoggedIn },
|
||||
);
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ function usePublicStream({ onlyMedia, language }: UsePublicStreamOpts = {}) {
|
|||
`public${onlyMedia ? ':media' : ''}`,
|
||||
`public${onlyMedia ? ':media' : ''}`,
|
||||
null,
|
||||
null,
|
||||
{ enabled: !language }, // TODO: support language streaming
|
||||
);
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ import { getAccessToken } from 'soapbox/utils/auth.ts';
|
|||
function useTimelineStream(...args: Parameters<typeof connectTimelineStream>) {
|
||||
// TODO: get rid of streaming.ts and move the actual opts here.
|
||||
const [timelineId, path] = args;
|
||||
const { enabled = true } = args[4] ?? {};
|
||||
const { enabled = true } = args[3] ?? {};
|
||||
|
||||
const dispatch = useAppDispatch();
|
||||
const { instance } = useInstance();
|
||||
|
|
|
@ -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 { useLoggedIn } from 'soapbox/hooks/useLoggedIn.ts';
|
||||
|
||||
import { useTimelineStream } from './useTimelineStream.ts';
|
||||
|
||||
import type { AppDispatch } from 'soapbox/store.ts';
|
||||
|
||||
function useUserStream() {
|
||||
const { isLoggedIn } = useLoggedIn();
|
||||
const statContext = useStatContext();
|
||||
|
@ -14,16 +10,9 @@ function useUserStream() {
|
|||
return useTimelineStream(
|
||||
'home',
|
||||
'user',
|
||||
refresh,
|
||||
null,
|
||||
{ statContext, enabled: isLoggedIn },
|
||||
);
|
||||
}
|
||||
|
||||
/** Refresh home timeline and notifications. */
|
||||
function refresh(dispatch: AppDispatch, done?: () => void) {
|
||||
return dispatch(expandHomeTimeline({}, () =>
|
||||
dispatch(expandNotifications({}, done))));
|
||||
}
|
||||
|
||||
export { useUserStream };
|
|
@ -4,19 +4,14 @@ import { getAccessToken } from 'soapbox/utils/auth.ts';
|
|||
|
||||
import type { AppDispatch, RootState } from 'soapbox/store.ts';
|
||||
|
||||
const randomIntUpTo = (max: number) => Math.floor(Math.random() * Math.floor(max));
|
||||
|
||||
interface ConnectStreamCallbacks {
|
||||
onConnect(): void;
|
||||
onDisconnect(): void;
|
||||
onReceive(websocket: Websocket, data: unknown): void;
|
||||
}
|
||||
|
||||
type PollingRefreshFn = (dispatch: AppDispatch, done?: () => void) => void
|
||||
|
||||
export function connectStream(
|
||||
path: string,
|
||||
pollingRefresh: PollingRefreshFn | null = null,
|
||||
callbacks: (dispatch: AppDispatch, getState: () => RootState) => ConnectStreamCallbacks,
|
||||
) {
|
||||
return (dispatch: AppDispatch, getState: () => RootState) => {
|
||||
|
@ -24,23 +19,6 @@ export function connectStream(
|
|||
const accessToken = getAccessToken(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;
|
||||
|
||||
// If the WebSocket fails to be created, don't crash the whole page,
|
||||
|
@ -48,18 +26,10 @@ export function connectStream(
|
|||
try {
|
||||
subscription = getStream(streamingAPIBaseURL!, accessToken!, path, {
|
||||
connected() {
|
||||
if (pollingRefresh) {
|
||||
clearPolling();
|
||||
}
|
||||
|
||||
onConnect();
|
||||
},
|
||||
|
||||
disconnected() {
|
||||
if (pollingRefresh) {
|
||||
polling = setTimeout(() => setupPolling(), randomIntUpTo(40000));
|
||||
}
|
||||
|
||||
onDisconnect();
|
||||
},
|
||||
|
||||
|
@ -68,11 +38,6 @@ export function connectStream(
|
|||
},
|
||||
|
||||
reconnected() {
|
||||
if (pollingRefresh) {
|
||||
clearPolling();
|
||||
pollingRefresh(dispatch);
|
||||
}
|
||||
|
||||
onConnect();
|
||||
},
|
||||
|
||||
|
@ -85,8 +50,6 @@ export function connectStream(
|
|||
if (subscription) {
|
||||
subscription.close();
|
||||
}
|
||||
|
||||
clearPolling();
|
||||
};
|
||||
|
||||
return disconnect;
|
||||
|
|
Loading…
Reference in New Issue