main
Last change
on this file since d565449 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 useThrottleFn = function (fn, ms, args) {
|
---|
| 4 | if (ms === void 0) { ms = 200; }
|
---|
| 5 | var _a = useState(null), state = _a[0], setState = _a[1];
|
---|
| 6 | var timeout = useRef();
|
---|
| 7 | var nextArgs = useRef();
|
---|
| 8 | useEffect(function () {
|
---|
| 9 | if (!timeout.current) {
|
---|
| 10 | setState(fn.apply(void 0, args));
|
---|
| 11 | var timeoutCallback_1 = function () {
|
---|
| 12 | if (nextArgs.current) {
|
---|
| 13 | setState(fn.apply(void 0, nextArgs.current));
|
---|
| 14 | nextArgs.current = undefined;
|
---|
| 15 | timeout.current = setTimeout(timeoutCallback_1, ms);
|
---|
| 16 | }
|
---|
| 17 | else {
|
---|
| 18 | timeout.current = undefined;
|
---|
| 19 | }
|
---|
| 20 | };
|
---|
| 21 | timeout.current = setTimeout(timeoutCallback_1, ms);
|
---|
| 22 | }
|
---|
| 23 | else {
|
---|
| 24 | nextArgs.current = args;
|
---|
| 25 | }
|
---|
| 26 | }, args);
|
---|
| 27 | useUnmount(function () {
|
---|
| 28 | timeout.current && clearTimeout(timeout.current);
|
---|
| 29 | });
|
---|
| 30 | return state;
|
---|
| 31 | };
|
---|
| 32 | export default useThrottleFn;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.