[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';
export default (ms: number) => {
export default (ms: number, noFirst = false) => {
return useMemo(() => {
return throttle((cb) => cb(), ms);
}, [ms]);
return throttle((cb) => cb(), ms, !noFirst);
}, [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 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) => {
const valueRef = useRef<R>();
const runThrottled = useThrottle(ms);
const forceUpdate = useForceUpdate();
const runThrottled = useThrottle(ms, true);
const [value, setValue] = useState<R>();
const [isFrozen, freeze, unfreeze] = useFlag();
useHeavyAnimationCheck(freeze, unfreeze);
useOnChange(() => {
let isSync = true;
if (isFrozen) {
return;
}
runThrottled(() => {
valueRef.current = resolverFn();
if (!isSync) {
forceUpdate();
}
setValue(resolverFn());
});
isSync = false;
}, dependencies);
}, dependencies.concat([isFrozen]));
return valueRef.current;
return value;
};