Minor optimizations
This commit is contained in:
parent
aaf1e8be93
commit
0f04434b65
@ -1,29 +1,19 @@
|
|||||||
import { useEffect, useMemo } from '../lib/teact/teact';
|
import { useEffect } from '../lib/teact/teact';
|
||||||
import { getActions } from '../global';
|
import { getActions } from '../global';
|
||||||
|
|
||||||
import type { ApiMessage } from '../api/types';
|
import type { ApiMessage } from '../api/types';
|
||||||
|
|
||||||
import { throttle } from '../util/schedulers';
|
export default function useEnsureMessage(
|
||||||
|
|
||||||
const useEnsureMessage = (
|
|
||||||
chatId: string,
|
chatId: string,
|
||||||
messageId?: number,
|
messageId?: number,
|
||||||
message?: ApiMessage,
|
message?: ApiMessage,
|
||||||
replyOriginForId?: number,
|
replyOriginForId?: number,
|
||||||
) => {
|
) {
|
||||||
const { loadMessage } = getActions();
|
const { loadMessage } = getActions();
|
||||||
const loadMessageThrottled = useMemo(() => {
|
|
||||||
const throttled = throttle(loadMessage, 500, true);
|
|
||||||
return () => {
|
|
||||||
throttled({ chatId, messageId: messageId!, replyOriginForId: replyOriginForId! });
|
|
||||||
};
|
|
||||||
}, [loadMessage, chatId, messageId, replyOriginForId]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (messageId && !message) {
|
if (messageId && !message) {
|
||||||
loadMessageThrottled();
|
loadMessage({ chatId, messageId: messageId!, replyOriginForId: replyOriginForId! });
|
||||||
}
|
}
|
||||||
});
|
}, [chatId, message, messageId, replyOriginForId]);
|
||||||
};
|
}
|
||||||
|
|
||||||
export default useEnsureMessage;
|
|
||||||
|
|||||||
@ -1,20 +1,21 @@
|
|||||||
import type React from '../lib/teact/teact';
|
import type React from '../lib/teact/teact';
|
||||||
|
|
||||||
import { IS_TOUCH_ENV, MouseButton } from '../util/windowEnvironment';
|
import { IS_TOUCH_ENV, MouseButton } from '../util/windowEnvironment';
|
||||||
|
import useLastCallback from './useLastCallback';
|
||||||
|
|
||||||
type EventArg<E> = React.MouseEvent<E>;
|
type EventArg<E> = React.MouseEvent<E>;
|
||||||
type EventHandler<E> = (e: EventArg<E>) => void;
|
type EventHandler<E> = (e: EventArg<E>) => void;
|
||||||
|
|
||||||
export function useFastClick<T extends HTMLDivElement | HTMLButtonElement>(callback?: EventHandler<T>) {
|
export function useFastClick<T extends HTMLDivElement | HTMLButtonElement>(callback?: EventHandler<T>) {
|
||||||
const wrapperHandler = callback ? (e: EventArg<T>) => {
|
const handler = useLastCallback((e: EventArg<T>) => {
|
||||||
if (e.type === 'mousedown' && e.button !== MouseButton.Main) {
|
if (e.type === 'mousedown' && e.button !== MouseButton.Main) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(e);
|
callback!(e);
|
||||||
} : undefined;
|
});
|
||||||
|
|
||||||
return IS_TOUCH_ENV
|
return IS_TOUCH_ENV
|
||||||
? { handleClick: wrapperHandler }
|
? { handleClick: callback ? handler : undefined }
|
||||||
: { handleMouseDown: wrapperHandler };
|
: { handleMouseDown: callback ? handler : undefined };
|
||||||
}
|
}
|
||||||
|
|||||||
@ -313,7 +313,7 @@ document.addEventListener('dblclick', () => {
|
|||||||
Object
|
Object
|
||||||
.values(DEBUG_components)
|
.values(DEBUG_components)
|
||||||
.map(({ avgRenderTime, ...state }) => {
|
.map(({ avgRenderTime, ...state }) => {
|
||||||
return { ...state, ...(avgRenderTime && { avgRenderTime: Number(avgRenderTime.toFixed(2)) }) };
|
return { ...state, ...(avgRenderTime !== undefined && { avgRenderTime: Number(avgRenderTime.toFixed(2)) }) };
|
||||||
}),
|
}),
|
||||||
'renders',
|
'renders',
|
||||||
'desc',
|
'desc',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user