Use better types for the thunk action, and let return types be inferred when possible

This commit is contained in:
Renaud Chaput 2025-03-05 11:10:06 +01:00 committed by Eugen Rochko
parent e22ad363b9
commit 5ac48bcac2
3 changed files with 13 additions and 17 deletions

View File

@ -40,7 +40,6 @@ const randomUpTo = max =>
* @param {function(Function, Function): Promise<void>} [options.fallback]
* @param {function(): void} [options.fillGaps]
* @param {function(import("mastodon/api_types/statuses").ApiStatusJSON): boolean} [options.accept]
* @returns {function(): void}
*/
export const connectTimelineStream = (timelineId, channelName, params = {}, options = {}) => {
const { messages } = getLocale();
@ -147,16 +146,12 @@ async function refreshHomeTimelineAndNotification(dispatch) {
await dispatch(fetchAnnouncements());
}
/**
* @returns {function(): void}
*/
export const connectUserStream = () =>
connectTimelineStream('home', 'user', {}, { fallback: refreshHomeTimelineAndNotification, fillGaps: fillHomeTimelineGaps });
/**
* @param {Object} options
* @param {boolean} [options.onlyMedia]
* @returns {function(): void}
*/
export const connectCommunityStream = ({ onlyMedia } = {}) =>
connectTimelineStream(`community${onlyMedia ? ':media' : ''}`, `public:local${onlyMedia ? ':media' : ''}`, {}, { fillGaps: () => (fillCommunityTimelineGaps({ onlyMedia })) });
@ -165,7 +160,6 @@ export const connectCommunityStream = ({ onlyMedia } = {}) =>
* @param {Object} options
* @param {boolean} [options.onlyMedia]
* @param {boolean} [options.onlyRemote]
* @returns {function(): void}
*/
export const connectPublicStream = ({ onlyMedia, onlyRemote } = {}) =>
connectTimelineStream(`public${onlyRemote ? ':remote' : ''}${onlyMedia ? ':media' : ''}`, `public${onlyRemote ? ':remote' : ''}${onlyMedia ? ':media' : ''}`, {}, { fillGaps: () => fillPublicTimelineGaps({ onlyMedia, onlyRemote }) });
@ -175,20 +169,15 @@ export const connectPublicStream = ({ onlyMedia, onlyRemote } = {}) =>
* @param {string} tagName
* @param {boolean} onlyLocal
* @param {function(object): boolean} accept
* @returns {function(): void}
*/
export const connectHashtagStream = (columnId, tagName, onlyLocal, accept) =>
connectTimelineStream(`hashtag:${columnId}${onlyLocal ? ':local' : ''}`, `hashtag${onlyLocal ? ':local' : ''}`, { tag: tagName }, { accept });
/**
* @returns {function(): void}
*/
export const connectDirectStream = () =>
connectTimelineStream('direct', 'direct');
/**
* @param {string} listId
* @returns {function(): void}
*/
export const connectListStream = listId =>
connectTimelineStream(`list:${listId}`, 'list', { list: listId }, { fillGaps: () => fillListTimelineGaps(listId) });
@ -199,7 +188,6 @@ export const connectListStream = listId =>
* @param {boolean} [options.withReplies]
* @param {string} [options.tagged]
* @param {boolean} [options.onlyMedia]
* @returns {function(): void}
*/
export const connectProfileStream = (accountId, { withReplies, tagged, onlyMedia }) =>
connectTimelineStream(`account:${accountId}${onlyMedia ? ':media' : ''}${withReplies ? ':with_replies' : ''}${tagged ? `:${tagged}` : ''}`, 'profile', { account_id: accountId }, {

View File

@ -4,6 +4,7 @@ import { FormattedMessage } from 'react-intl';
import { useParams } from 'react-router-dom';
import type { ThunkAction, UnknownAction } from '@reduxjs/toolkit';
import { createSelector } from '@reduxjs/toolkit';
import type { Map as ImmutableMap } from 'immutable';
import { List as ImmutableList } from 'immutable';
@ -160,8 +161,16 @@ export const AccountGallery: React.FC<{
if (signedIn) {
disconnect = dispatch(
connectProfileStream(accountId, { onlyMedia: true }),
) as unknown as () => void;
// The `as` is needed because `typescript-eslint` is confused by the types defined
// in JSDoc, it forces the type to be correct here, otherwise we get a
// `@typescript-eslint/no-confusing-void-expression` error
connectProfileStream(accountId, { onlyMedia: true }) as ThunkAction<
() => void,
RootState,
undefined,
UnknownAction
>,
);
}
}

View File

@ -142,11 +142,10 @@ const channelNameWithInlineParams = (channelName, params) => {
* @param {string} channelName
* @param {Object.<string, string>} params
* @param {function(Function, Function): { onConnect: (function(): void), onReceive: (function(StreamEvent): void), onDisconnect: (function(): void) }} callbacks
* @returns {function(): void}
* @returns {import('@reduxjs/toolkit').ThunkAction<() => void, import('./store').RootState, undefined, import('@reduxjs/toolkit').UnknownAction>}
*/
// @ts-expect-error
export const connectStream = (channelName, params, callbacks) => (dispatch, getState) => {
const streamingAPIBaseURL = getState().getIn(['meta', 'streaming_api_base_url']);
const streamingAPIBaseURL = getState().meta.get('streaming_api_base_url');
const accessToken = getAccessToken();
const { onConnect, onReceive, onDisconnect } = callbacks(dispatch, getState);