[Perf] Don not calculate throttled memo during heavy animation

This commit is contained in:
Alexander Zinchuk 2021-08-04 02:35:19 +03:00
parent e5f4fdb369
commit 4a7882e1ba
2 changed files with 18 additions and 17 deletions

View File

@ -2,8 +2,8 @@ import { useMemo } from '../lib/teact/teact';
import { throttle } from '../util/schedulers'; import { throttle } from '../util/schedulers';
export default (ms: number) => { export default (ms: number, noFirst = false) => {
return useMemo(() => { return useMemo(() => {
return throttle((cb) => cb(), ms); return throttle((cb) => cb(), ms, !noFirst);
}, [ms]); }, [ms, noFirst]);
}; };

View File

@ -1,25 +1,26 @@
import { useRef } from '../lib/teact/teact'; import { useState } from '../lib/teact/teact';
import useThrottle from './useThrottle'; import useThrottle from './useThrottle';
import useOnChange from './useOnChange'; import useOnChange from './useOnChange';
import useForceUpdate from './useForceUpdate'; import useHeavyAnimationCheck from './useHeavyAnimationCheck';
import useFlag from './useFlag';
export default <R extends any, D extends any[]>(resolverFn: () => R, ms: number, dependencies: D) => { export default <R extends any, D extends any[]>(resolverFn: () => R, ms: number, dependencies: D) => {
const valueRef = useRef<R>(); const runThrottled = useThrottle(ms, true);
const runThrottled = useThrottle(ms); const [value, setValue] = useState<R>();
const forceUpdate = useForceUpdate(); const [isFrozen, freeze, unfreeze] = useFlag();
useHeavyAnimationCheck(freeze, unfreeze);
useOnChange(() => { useOnChange(() => {
let isSync = true; if (isFrozen) {
return;
}
runThrottled(() => { runThrottled(() => {
valueRef.current = resolverFn(); setValue(resolverFn());
if (!isSync) {
forceUpdate();
}
}); });
isSync = false; }, dependencies.concat([isFrozen]));
}, dependencies);
return valueRef.current; return value;
}; };