Skip to content

Commit 9baca13

Browse files
committed
chore: extract stable cb
1 parent d19fbc0 commit 9baca13

File tree

5 files changed

+21
-27
lines changed

5 files changed

+21
-27
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import {
7474
useTranslationContext,
7575
} from '../../contexts/translationContext/TranslationContext';
7676
import { TypingProvider } from '../../contexts/typingContext/TypingContext';
77+
import { useStableCallback } from '../../hooks';
7778
import { useAppStateListener } from '../../hooks/useAppStateListener';
7879

7980
import {
@@ -2108,10 +2109,3 @@ export const Channel = <
21082109
/>
21092110
);
21102111
};
2111-
2112-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
2113-
const useStableCallback = <T extends Function>(callback: T): T => {
2114-
const ref = useRef<T>(callback);
2115-
ref.current = callback;
2116-
return useCallback(((...args: unknown[]) => ref.current(...args)) as unknown as T, []);
2117-
};

package/src/components/Channel/hooks/useMessageListPagination.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { useCallback, useRef } from 'react';
1+
import { useRef } from 'react';
22

33
import debounce from 'lodash/debounce';
44
import { Channel, ChannelState, MessageResponse } from 'stream-chat';
55

66
import { useChannelMessageDataState } from './useChannelDataState';
77

88
import { ChannelContextValue } from '../../../contexts/channelContext/ChannelContext';
9+
import { useStableCallback } from '../../../hooks';
910
import { DefaultStreamChatGenerics } from '../../../types/types';
1011
import { findInMessagesByDate, findInMessagesById } from '../../../utils/utils';
1112

@@ -80,7 +81,7 @@ export const useMessageListPagination = <
8081
/**
8182
* This function loads more messages before the first message in current channel state.
8283
*/
83-
const loadMore = useStableCallback(async (limit = 20) => {
84+
const loadMore = useStableCallback(async (limit: number = 20) => {
8485
if (!channel.state.messagePagination.hasPrev) {
8586
return;
8687
}
@@ -109,7 +110,7 @@ export const useMessageListPagination = <
109110
/**
110111
* This function loads more messages after the most recent message in current channel state.
111112
*/
112-
const loadMoreRecent = useStableCallback(async (limit = 10) => {
113+
const loadMoreRecent = useStableCallback(async (limit: number = 10) => {
113114
if (!channel.state.messagePagination.hasNext) {
114115
return;
115116
}
@@ -322,10 +323,3 @@ export const useMessageListPagination = <
322323
state,
323324
};
324325
};
325-
326-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
327-
const useStableCallback = <T extends Function>(callback: T): T => {
328-
const ref = useRef<T>(callback);
329-
ref.current = callback;
330-
return useCallback(((...args: unknown[]) => ref.current(...args)) as unknown as T, []);
331-
};

package/src/components/MessageList/MessageList.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
import { mergeThemes, ThemeProvider, useTheme } from '../../contexts/themeContext/ThemeContext';
5454
import { ThreadContextValue, useThreadContext } from '../../contexts/threadContext/ThreadContext';
5555

56+
import { useStableCallback } from '../../hooks';
5657
import { DefaultStreamChatGenerics, FileTypes } from '../../types/types';
5758

5859
// This is just to make sure that the scrolling happens in a different task queue.
@@ -975,7 +976,6 @@ const MessageListWithContext = <
975976
}
976977
});
977978

978-
// @ts-expect-error will fix later
979979
const handleScroll: ScrollViewProps['onScroll'] = useStableCallback((event) => {
980980
const messageListHasMessages = processedMessageList.length > 0;
981981
const offset = event.nativeEvent.contentOffset.y;
@@ -1129,13 +1129,11 @@ const MessageListWithContext = <
11291129
}
11301130
});
11311131

1132-
// @ts-expect-error will fix later
11331132
const onScrollBeginDrag: ScrollViewProps['onScrollBeginDrag'] = useStableCallback((event) => {
11341133
!hasMoved && selectedPicker && setHasMoved(true);
11351134
onUserScrollEvent(event);
11361135
});
11371136

1138-
// @ts-expect-error will fix later
11391137
const onScrollEndDrag: ScrollViewProps['onScrollEndDrag'] = useStableCallback((event) => {
11401138
hasMoved && selectedPicker && setHasMoved(false);
11411139
onUserScrollEvent(event);
@@ -1430,10 +1428,3 @@ export const MessageList = <
14301428
/>
14311429
);
14321430
};
1433-
1434-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
1435-
export const useStableCallback = <T extends Function>(callback: T): T => {
1436-
const ref = useRef<T>(callback);
1437-
ref.current = callback;
1438-
return useCallback(((...args: unknown[]) => ref.current(...args)) as unknown as T, []);
1439-
};

package/src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './useStreami18n';
33
export * from './useViewport';
44
export * from './useScreenDimensions';
55
export * from './useStateStore';
6+
export * from './useStableCallback';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useCallback, useRef } from 'react';
2+
3+
export type StableCallback<A extends unknown[], R> = (...args: A) => R;
4+
5+
export const useStableCallback = <A extends unknown[], R>(
6+
callback: StableCallback<A, R>,
7+
): StableCallback<A, R> => {
8+
const ref = useRef(callback);
9+
ref.current = callback;
10+
11+
return useCallback<StableCallback<A, R>>((...args) => {
12+
return ref.current(...args);
13+
}, []);
14+
};

0 commit comments

Comments
 (0)