main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[d565449] | 1 | import { useEffect, useRef, useState } from 'react';
|
---|
| 2 | import useUnmount from './useUnmount';
|
---|
| 3 | var useThrottle = function (value, ms) {
|
---|
| 4 | if (ms === void 0) { ms = 200; }
|
---|
| 5 | var _a = useState(value), state = _a[0], setState = _a[1];
|
---|
| 6 | var timeout = useRef();
|
---|
| 7 | var nextValue = useRef(null);
|
---|
| 8 | var hasNextValue = useRef(0);
|
---|
| 9 | useEffect(function () {
|
---|
| 10 | if (!timeout.current) {
|
---|
| 11 | setState(value);
|
---|
| 12 | var timeoutCallback_1 = function () {
|
---|
| 13 | if (hasNextValue.current) {
|
---|
| 14 | hasNextValue.current = false;
|
---|
| 15 | setState(nextValue.current);
|
---|
| 16 | timeout.current = setTimeout(timeoutCallback_1, ms);
|
---|
| 17 | }
|
---|
| 18 | else {
|
---|
| 19 | timeout.current = undefined;
|
---|
| 20 | }
|
---|
| 21 | };
|
---|
| 22 | timeout.current = setTimeout(timeoutCallback_1, ms);
|
---|
| 23 | }
|
---|
| 24 | else {
|
---|
| 25 | nextValue.current = value;
|
---|
| 26 | hasNextValue.current = true;
|
---|
| 27 | }
|
---|
| 28 | }, [value]);
|
---|
| 29 | useUnmount(function () {
|
---|
| 30 | timeout.current && clearTimeout(timeout.current);
|
---|
| 31 | });
|
---|
| 32 | return state;
|
---|
| 33 | };
|
---|
| 34 | export default useThrottle;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.