UNPKG

acud

Version:

acg react 组件库

42 lines • 1.25 kB
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; /** * @file updators throttled by every frame * @author chenqiang(chenqiang03) */ import * as React from 'react'; import raf from "rc-util/es/raf"; export default function useFrameState(defaultValue) { var _React$useState = React.useState(defaultValue), _React$useState2 = _slicedToArray(_React$useState, 2), value = _React$useState2[0], setValue = _React$useState2[1]; var frameRef = React.useRef(null); var batchRef = React.useRef([]); var destoryRef = React.useRef(false); React.useEffect(function () { return function () { destoryRef.current = true; raf.cancel(frameRef.current); }; }, []); function setFrameValue(updater) { if (destoryRef.current) { return; } if (frameRef.current === null) { batchRef.current = []; frameRef.current = raf(function () { frameRef.current = null; setValue(function (prevValue) { var current = prevValue; batchRef.current.forEach(function (func) { current = func(current); }); return current; }); }); } batchRef.current.push(updater); } return [value, setFrameValue]; }